From 82727b0c0f7973864c3990853447def2ab3059af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Miku=C5=82a?= Date: Sun, 8 Mar 2026 22:36:30 +0100 Subject: [PATCH 01/10] Refactor DllImport to better differentiate types --- .../src/back/link/raw_dylib.rs | 20 +++++++++++-------- compiler/rustc_codegen_ssa/src/common.rs | 8 ++++---- compiler/rustc_metadata/src/native_libs.rs | 18 +++++++++-------- compiler/rustc_session/src/cstore.rs | 10 ++++++++-- 4 files changed, 34 insertions(+), 22 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/link/raw_dylib.rs b/compiler/rustc_codegen_ssa/src/back/link/raw_dylib.rs index 477c9478c3609..ec1c013205622 100644 --- a/compiler/rustc_codegen_ssa/src/back/link/raw_dylib.rs +++ b/compiler/rustc_codegen_ssa/src/back/link/raw_dylib.rs @@ -9,7 +9,7 @@ use rustc_data_structures::stable_hasher::StableHasher; use rustc_hashes::Hash128; use rustc_hir::attrs::NativeLibKind; use rustc_session::Session; -use rustc_session::cstore::DllImport; +use rustc_session::cstore::{DllImport, DllImportSymbolType}; use rustc_span::Symbol; use rustc_target::spec::Arch; @@ -95,14 +95,14 @@ pub(super) fn create_raw_dylib_dll_import_libs<'a>( true, ) }), - is_data: !import.is_fn, + is_data: import.symbol_type != DllImportSymbolType::Function, } } else { ImportLibraryItem { name: import.name.to_string(), ordinal: import.ordinal(), symbol_name: None, - is_data: !import.is_fn, + is_data: import.symbol_type != DllImportSymbolType::Function, } } }) @@ -271,10 +271,10 @@ fn create_elf_raw_dylib_stub(sess: &Session, soname: &str, symbols: &[DllImport] vers.push((version_name, dynstr)); id }; - syms.push((name, dynstr, Some(ver), symbol.is_fn)); + syms.push((name, dynstr, Some(ver), symbol.symbol_type)); } else { let dynstr = stub.add_dynamic_string(symbol_name.as_bytes()); - syms.push((symbol_name, dynstr, None, symbol.is_fn)); + syms.push((symbol_name, dynstr, None, symbol.symbol_type)); } } @@ -398,8 +398,12 @@ fn create_elf_raw_dylib_stub(sess: &Session, soname: &str, symbols: &[DllImport] // .dynsym stub.write_null_dynamic_symbol(); - for (_name, dynstr, _ver, is_fn) in syms.iter().copied() { - let sym_type = if is_fn { elf::STT_FUNC } else { elf::STT_NOTYPE }; + for (_name, dynstr, _ver, symbol_type) in syms.iter().copied() { + let sym_type = match symbol_type { + DllImportSymbolType::Function => elf::STT_FUNC, + DllImportSymbolType::Static => elf::STT_OBJECT, + DllImportSymbolType::ThreadLocal => elf::STT_TLS, + }; stub.write_dynamic_symbol(&write::Sym { name: Some(dynstr), st_info: (elf::STB_GLOBAL << 4) | sym_type, @@ -418,7 +422,7 @@ fn create_elf_raw_dylib_stub(sess: &Session, soname: &str, symbols: &[DllImport] if !vers.is_empty() { // .gnu_version stub.write_null_gnu_versym(); - for (_name, _dynstr, ver, _is_fn) in syms.iter().copied() { + for (_name, _dynstr, ver, _symbol_type) in syms.iter().copied() { stub.write_gnu_versym(if let Some(ver) = ver { assert!((2 + ver as u16) < elf::VERSYM_HIDDEN); elf::VERSYM_HIDDEN | (2 + ver as u16) diff --git a/compiler/rustc_codegen_ssa/src/common.rs b/compiler/rustc_codegen_ssa/src/common.rs index 89a3f8061a8c1..a0f46a2d4f5ef 100644 --- a/compiler/rustc_codegen_ssa/src/common.rs +++ b/compiler/rustc_codegen_ssa/src/common.rs @@ -5,7 +5,7 @@ use rustc_hir::attrs::PeImportNameType; use rustc_middle::ty::layout::TyAndLayout; use rustc_middle::ty::{self, Instance, TyCtxt}; use rustc_middle::{bug, mir, span_bug}; -use rustc_session::cstore::{DllCallingConvention, DllImport}; +use rustc_session::cstore::{DllCallingConvention, DllImport, DllImportSymbolType}; use rustc_span::Span; use rustc_target::spec::{Abi, Env, Os, Target}; @@ -199,7 +199,7 @@ pub fn i686_decorated_name( decorated_name.push('\x01'); } - let prefix = if add_prefix && dll_import.is_fn { + let prefix = if add_prefix && dll_import.symbol_type == DllImportSymbolType::Function { match dll_import.calling_convention { DllCallingConvention::C | DllCallingConvention::Vectorcall(_) => None, DllCallingConvention::Stdcall(_) => (!mingw @@ -207,7 +207,7 @@ pub fn i686_decorated_name( .then_some('_'), DllCallingConvention::Fastcall(_) => Some('@'), } - } else if !dll_import.is_fn && !mingw { + } else if dll_import.symbol_type != DllImportSymbolType::Function && !mingw { // For static variables, prefix with '_' on MSVC. Some('_') } else { @@ -219,7 +219,7 @@ pub fn i686_decorated_name( decorated_name.push_str(name); - if add_suffix && dll_import.is_fn { + if add_suffix && dll_import.symbol_type == DllImportSymbolType::Function { use std::fmt::Write; match dll_import.calling_convention { diff --git a/compiler/rustc_metadata/src/native_libs.rs b/compiler/rustc_metadata/src/native_libs.rs index 52b11615c76f0..1bd6a896b4e25 100644 --- a/compiler/rustc_metadata/src/native_libs.rs +++ b/compiler/rustc_metadata/src/native_libs.rs @@ -10,7 +10,9 @@ use rustc_middle::query::LocalCrate; use rustc_middle::ty::{self, List, Ty, TyCtxt}; use rustc_session::Session; use rustc_session::config::CrateType; -use rustc_session::cstore::{DllCallingConvention, DllImport, ForeignModule, NativeLib}; +use rustc_session::cstore::{ + DllCallingConvention, DllImport, DllImportSymbolType, ForeignModule, NativeLib, +}; use rustc_session::search_paths::PathKind; use rustc_span::Symbol; use rustc_span::def_id::{DefId, LOCAL_CRATE}; @@ -451,12 +453,12 @@ impl<'tcx> Collector<'tcx> { } } - DllImport { - name, - import_name_type, - calling_convention, - span, - is_fn: self.tcx.def_kind(item).is_fn_like(), - } + let symbol_type = if self.tcx.def_kind(item).is_fn_like() { + DllImportSymbolType::Function + } else { + bug!("Unexpected type for raw-dylib: {}", def_kind.descr(item)); + }; + + DllImport { name, import_name_type, calling_convention, span, symbol_type } } } diff --git a/compiler/rustc_session/src/cstore.rs b/compiler/rustc_session/src/cstore.rs index 2f969aefda182..41e8dfbfcefbc 100644 --- a/compiler/rustc_session/src/cstore.rs +++ b/compiler/rustc_session/src/cstore.rs @@ -97,8 +97,14 @@ pub struct DllImport { pub calling_convention: DllCallingConvention, /// Span of import's "extern" declaration; used for diagnostics. pub span: Span, - /// Is this for a function (rather than a static variable). - pub is_fn: bool, + pub symbol_type: DllImportSymbolType, +} + +#[derive(Copy, Clone, Debug, Encodable, Decodable, HashStable_Generic, PartialEq)] +pub enum DllImportSymbolType { + Function, + Static, + ThreadLocal, } impl DllImport { From 2fad36b93d0bd964f3b99af23e9abe0f2cc0a301 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Miku=C5=82a?= Date: Sun, 8 Mar 2026 23:13:26 +0100 Subject: [PATCH 02/10] Extend raw-dylib to handle statics and TLS for ELF --- compiler/rustc_metadata/src/native_libs.rs | 11 ++++++++++- tests/run-make/raw-dylib-elf/library.c | 3 +++ tests/run-make/raw-dylib-elf/main.rs | 12 ++++++++++-- tests/run-make/raw-dylib-elf/output.txt | 2 +- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_metadata/src/native_libs.rs b/compiler/rustc_metadata/src/native_libs.rs index 1bd6a896b4e25..a60c030b9c0b6 100644 --- a/compiler/rustc_metadata/src/native_libs.rs +++ b/compiler/rustc_metadata/src/native_libs.rs @@ -5,7 +5,9 @@ use rustc_abi::ExternAbi; use rustc_attr_parsing::eval_config_entry; use rustc_data_structures::fx::FxHashSet; use rustc_hir::attrs::{NativeLibKind, PeImportNameType}; +use rustc_hir::def::DefKind; use rustc_hir::find_attr; +use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags; use rustc_middle::query::LocalCrate; use rustc_middle::ty::{self, List, Ty, TyCtxt}; use rustc_session::Session; @@ -453,8 +455,15 @@ impl<'tcx> Collector<'tcx> { } } - let symbol_type = if self.tcx.def_kind(item).is_fn_like() { + let def_kind = self.tcx.def_kind(item); + let symbol_type = if def_kind.is_fn_like() { DllImportSymbolType::Function + } else if matches!(def_kind, DefKind::Static { .. }) { + if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL) { + DllImportSymbolType::ThreadLocal + } else { + DllImportSymbolType::Static + } } else { bug!("Unexpected type for raw-dylib: {}", def_kind.descr(item)); }; diff --git a/tests/run-make/raw-dylib-elf/library.c b/tests/run-make/raw-dylib-elf/library.c index 2e3a95b7edeca..943a1382e3405 100644 --- a/tests/run-make/raw-dylib-elf/library.c +++ b/tests/run-make/raw-dylib-elf/library.c @@ -1,3 +1,6 @@ +int global_variable = 3; +__thread int tls_variable = 33; + int this_is_a_library_function() { return 42; } diff --git a/tests/run-make/raw-dylib-elf/main.rs b/tests/run-make/raw-dylib-elf/main.rs index 3be944d295148..ec8951da88f03 100644 --- a/tests/run-make/raw-dylib-elf/main.rs +++ b/tests/run-make/raw-dylib-elf/main.rs @@ -1,11 +1,19 @@ #![feature(raw_dylib_elf)] +#![feature(thread_local)] #![allow(incomplete_features)] +use std::ffi::c_int; + #[link(name = "library", kind = "raw-dylib")] unsafe extern "C" { - safe fn this_is_a_library_function() -> core::ffi::c_int; + safe fn this_is_a_library_function() -> c_int; + static mut global_variable: c_int; + #[thread_local] + static mut tls_variable: c_int; } fn main() { - println!("{}", this_is_a_library_function()) + unsafe { + println!("{} {} {}", this_is_a_library_function(), global_variable, tls_variable); + } } diff --git a/tests/run-make/raw-dylib-elf/output.txt b/tests/run-make/raw-dylib-elf/output.txt index d81cc0710eb6c..53383c3b86453 100644 --- a/tests/run-make/raw-dylib-elf/output.txt +++ b/tests/run-make/raw-dylib-elf/output.txt @@ -1 +1 @@ -42 +42 3 33 From f3654324e6da3fd388035418c9db1912a0d8c4cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Miku=C5=82a?= Date: Mon, 9 Mar 2026 18:17:07 +0100 Subject: [PATCH 03/10] Set symbol size in raw-dylib for ELF --- .../rustc_codegen_ssa/src/back/link/raw_dylib.rs | 10 +++++----- compiler/rustc_metadata/src/native_libs.rs | 16 +++++++++++++++- compiler/rustc_session/src/cstore.rs | 1 + 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/link/raw_dylib.rs b/compiler/rustc_codegen_ssa/src/back/link/raw_dylib.rs index ec1c013205622..3017e5fc567d3 100644 --- a/compiler/rustc_codegen_ssa/src/back/link/raw_dylib.rs +++ b/compiler/rustc_codegen_ssa/src/back/link/raw_dylib.rs @@ -271,10 +271,10 @@ fn create_elf_raw_dylib_stub(sess: &Session, soname: &str, symbols: &[DllImport] vers.push((version_name, dynstr)); id }; - syms.push((name, dynstr, Some(ver), symbol.symbol_type)); + syms.push((name, dynstr, Some(ver), symbol.symbol_type, symbol.size)); } else { let dynstr = stub.add_dynamic_string(symbol_name.as_bytes()); - syms.push((symbol_name, dynstr, None, symbol.symbol_type)); + syms.push((symbol_name, dynstr, None, symbol.symbol_type, symbol.size)); } } @@ -398,7 +398,7 @@ fn create_elf_raw_dylib_stub(sess: &Session, soname: &str, symbols: &[DllImport] // .dynsym stub.write_null_dynamic_symbol(); - for (_name, dynstr, _ver, symbol_type) in syms.iter().copied() { + for (_name, dynstr, _ver, symbol_type, size) in syms.iter().copied() { let sym_type = match symbol_type { DllImportSymbolType::Function => elf::STT_FUNC, DllImportSymbolType::Static => elf::STT_OBJECT, @@ -411,7 +411,7 @@ fn create_elf_raw_dylib_stub(sess: &Session, soname: &str, symbols: &[DllImport] section: Some(text_section), st_shndx: 0, // ignored by object in favor of the `section` field st_value: 0, - st_size: 0, + st_size: size.bytes(), }); } @@ -422,7 +422,7 @@ fn create_elf_raw_dylib_stub(sess: &Session, soname: &str, symbols: &[DllImport] if !vers.is_empty() { // .gnu_version stub.write_null_gnu_versym(); - for (_name, _dynstr, ver, _symbol_type) in syms.iter().copied() { + for (_name, _dynstr, ver, _symbol_type, _size) in syms.iter().copied() { stub.write_gnu_versym(if let Some(ver) = ver { assert!((2 + ver as u16) < elf::VERSYM_HIDDEN); elf::VERSYM_HIDDEN | (2 + ver as u16) diff --git a/compiler/rustc_metadata/src/native_libs.rs b/compiler/rustc_metadata/src/native_libs.rs index a60c030b9c0b6..17b16b3cfe2d9 100644 --- a/compiler/rustc_metadata/src/native_libs.rs +++ b/compiler/rustc_metadata/src/native_libs.rs @@ -7,6 +7,7 @@ use rustc_data_structures::fx::FxHashSet; use rustc_hir::attrs::{NativeLibKind, PeImportNameType}; use rustc_hir::def::DefKind; use rustc_hir::find_attr; +use rustc_middle::bug; use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags; use rustc_middle::query::LocalCrate; use rustc_middle::ty::{self, List, Ty, TyCtxt}; @@ -468,6 +469,19 @@ impl<'tcx> Collector<'tcx> { bug!("Unexpected type for raw-dylib: {}", def_kind.descr(item)); }; - DllImport { name, import_name_type, calling_convention, span, symbol_type } + let size = match symbol_type { + // We cannot determine the size of a function at compile time, but it shouldn't matter anyway. + DllImportSymbolType::Function => rustc_abi::Size::ZERO, + DllImportSymbolType::Static | DllImportSymbolType::ThreadLocal => { + let ty = self.tcx.type_of(item).instantiate_identity(); + self.tcx + .layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(ty)) + .ok() + .map(|layout| layout.size) + .unwrap_or_else(|| bug!("Non-function symbols must have a size")) + } + }; + + DllImport { name, import_name_type, calling_convention, span, symbol_type, size } } } diff --git a/compiler/rustc_session/src/cstore.rs b/compiler/rustc_session/src/cstore.rs index 41e8dfbfcefbc..c186557ccaa49 100644 --- a/compiler/rustc_session/src/cstore.rs +++ b/compiler/rustc_session/src/cstore.rs @@ -98,6 +98,7 @@ pub struct DllImport { /// Span of import's "extern" declaration; used for diagnostics. pub span: Span, pub symbol_type: DllImportSymbolType, + pub size: rustc_abi::Size, } #[derive(Copy, Clone, Debug, Encodable, Decodable, HashStable_Generic, PartialEq)] From 36381555bb78f41f178032dbabbf9ebd602c3530 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Miku=C5=82a?= Date: Fri, 13 Mar 2026 18:54:40 +0100 Subject: [PATCH 04/10] Put statics in .data section in raw-dylib for ELF This fixes issues with statics when using static relocation model and linking with GNU ld. Other linkers don't work yet. --- .../src/back/link/raw_dylib.rs | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/link/raw_dylib.rs b/compiler/rustc_codegen_ssa/src/back/link/raw_dylib.rs index 3017e5fc567d3..06dd7e7eb2bdf 100644 --- a/compiler/rustc_codegen_ssa/src/back/link/raw_dylib.rs +++ b/compiler/rustc_codegen_ssa/src/back/link/raw_dylib.rs @@ -296,6 +296,8 @@ fn create_elf_raw_dylib_stub(sess: &Session, soname: &str, symbols: &[DllImport] stub.reserve_shstrtab_section_index(); let text_section_name = stub.add_section_name(".text".as_bytes()); let text_section = stub.reserve_section_index(); + let data_section_name = stub.add_section_name(".data".as_bytes()); + let data_section = stub.reserve_section_index(); stub.reserve_dynsym_section_index(); stub.reserve_dynstr_section_index(); if !vers.is_empty() { @@ -375,7 +377,7 @@ fn create_elf_raw_dylib_stub(sess: &Session, soname: &str, symbols: &[DllImport] // Section headers stub.write_null_section_header(); stub.write_shstrtab_section_header(); - // Create a dummy .text section for our dummy symbols. + // Create a dummy .text section for our dummy non-data symbols. stub.write_section_header(&write::SectionHeader { name: Some(text_section_name), sh_type: elf::SHT_PROGBITS, @@ -388,6 +390,19 @@ fn create_elf_raw_dylib_stub(sess: &Session, soname: &str, symbols: &[DllImport] sh_addralign: 1, sh_entsize: 0, }); + // And also a dummy .data section for our dummy data symbols. + stub.write_section_header(&write::SectionHeader { + name: Some(data_section_name), + sh_type: elf::SHT_PROGBITS, + sh_flags: (elf::SHF_WRITE | elf::SHF_ALLOC) as u64, + sh_addr: 0, + sh_offset: 0, + sh_size: 0, + sh_link: 0, + sh_info: 0, + sh_addralign: 16, + sh_entsize: 0, + }); stub.write_dynsym_section_header(0, 1); stub.write_dynstr_section_header(0); if !vers.is_empty() { @@ -404,11 +419,13 @@ fn create_elf_raw_dylib_stub(sess: &Session, soname: &str, symbols: &[DllImport] DllImportSymbolType::Static => elf::STT_OBJECT, DllImportSymbolType::ThreadLocal => elf::STT_TLS, }; + let section = + if symbol_type == DllImportSymbolType::Static { data_section } else { text_section }; stub.write_dynamic_symbol(&write::Sym { name: Some(dynstr), st_info: (elf::STB_GLOBAL << 4) | sym_type, st_other: elf::STV_DEFAULT, - section: Some(text_section), + section: Some(section), st_shndx: 0, // ignored by object in favor of the `section` field st_value: 0, st_size: size.bytes(), From ed4afa598928a239725a7862589145359dbf9b9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Miku=C5=82a?= Date: Fri, 13 Mar 2026 19:46:32 +0100 Subject: [PATCH 05/10] Set symbols value in raw-dylib for ELF With this change LLD finally produces working binaries with static relocation model. --- compiler/rustc_codegen_ssa/src/back/link/raw_dylib.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/link/raw_dylib.rs b/compiler/rustc_codegen_ssa/src/back/link/raw_dylib.rs index 06dd7e7eb2bdf..ddc30ee9f3ac2 100644 --- a/compiler/rustc_codegen_ssa/src/back/link/raw_dylib.rs +++ b/compiler/rustc_codegen_ssa/src/back/link/raw_dylib.rs @@ -387,7 +387,7 @@ fn create_elf_raw_dylib_stub(sess: &Session, soname: &str, symbols: &[DllImport] sh_size: 0, sh_link: 0, sh_info: 0, - sh_addralign: 1, + sh_addralign: 16, sh_entsize: 0, }); // And also a dummy .data section for our dummy data symbols. @@ -413,6 +413,10 @@ fn create_elf_raw_dylib_stub(sess: &Session, soname: &str, symbols: &[DllImport] // .dynsym stub.write_null_dynamic_symbol(); + // Linkers like LLD require at least somewhat reasonable symbol values rather than zero, + // otherwise all the symbols might get put at the same address. Thus we increment the value + // every time we write a symbol. + let mut st_value = 0; for (_name, dynstr, _ver, symbol_type, size) in syms.iter().copied() { let sym_type = match symbol_type { DllImportSymbolType::Function => elf::STT_FUNC, @@ -427,9 +431,10 @@ fn create_elf_raw_dylib_stub(sess: &Session, soname: &str, symbols: &[DllImport] st_other: elf::STV_DEFAULT, section: Some(section), st_shndx: 0, // ignored by object in favor of the `section` field - st_value: 0, + st_value, st_size: size.bytes(), }); + st_value += 8; } // .dynstr From 3531ed9c1d7e866cf2b2c238efaca7e5fc3f4b0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Miku=C5=82a?= Date: Mon, 9 Mar 2026 18:17:07 +0100 Subject: [PATCH 06/10] Extend raw-dylib test for ELF with reloc models --- tests/run-make/raw-dylib-elf/library.c | 2 ++ tests/run-make/raw-dylib-elf/main.rs | 14 ++++++++++-- tests/run-make/raw-dylib-elf/output.txt | 2 +- tests/run-make/raw-dylib-elf/rmake.rs | 30 ++++++++++++++++--------- 4 files changed, 34 insertions(+), 14 deletions(-) diff --git a/tests/run-make/raw-dylib-elf/library.c b/tests/run-make/raw-dylib-elf/library.c index 943a1382e3405..060cab6ee02f7 100644 --- a/tests/run-make/raw-dylib-elf/library.c +++ b/tests/run-make/raw-dylib-elf/library.c @@ -1,5 +1,7 @@ int global_variable = 3; __thread int tls_variable = 33; +const long const_array[] = {795, 906}; +const char *const_string = "sums of three cubes"; int this_is_a_library_function() { return 42; diff --git a/tests/run-make/raw-dylib-elf/main.rs b/tests/run-make/raw-dylib-elf/main.rs index ec8951da88f03..b55cde766a375 100644 --- a/tests/run-make/raw-dylib-elf/main.rs +++ b/tests/run-make/raw-dylib-elf/main.rs @@ -2,7 +2,7 @@ #![feature(thread_local)] #![allow(incomplete_features)] -use std::ffi::c_int; +use std::ffi::{CStr, c_char, c_int, c_long}; #[link(name = "library", kind = "raw-dylib")] unsafe extern "C" { @@ -10,10 +10,20 @@ unsafe extern "C" { static mut global_variable: c_int; #[thread_local] static mut tls_variable: c_int; + safe static const_array: [c_long; 2]; + safe static const_string: *const c_char; } fn main() { unsafe { - println!("{} {} {}", this_is_a_library_function(), global_variable, tls_variable); + println!( + "{} {} {} {} {} {}", + this_is_a_library_function(), + global_variable, + tls_variable, + const_array[0], + const_array[1], + CStr::from_ptr(const_string).to_str().unwrap(), + ); } } diff --git a/tests/run-make/raw-dylib-elf/output.txt b/tests/run-make/raw-dylib-elf/output.txt index 53383c3b86453..6a9770dcb39b4 100644 --- a/tests/run-make/raw-dylib-elf/output.txt +++ b/tests/run-make/raw-dylib-elf/output.txt @@ -1 +1 @@ -42 3 33 +42 3 33 795 906 sums of three cubes diff --git a/tests/run-make/raw-dylib-elf/rmake.rs b/tests/run-make/raw-dylib-elf/rmake.rs index 59f901ac1ed72..d8a10fcb7f838 100644 --- a/tests/run-make/raw-dylib-elf/rmake.rs +++ b/tests/run-make/raw-dylib-elf/rmake.rs @@ -11,19 +11,27 @@ use run_make_support::{build_native_dynamic_lib, cwd, diff, run, rustc}; fn main() { - // We compile the binary without having the library present. + // We compile the binaries without having the library present with different relocation + // models. // We also set the rpath to the current directory so we can pick up the library at runtime. - rustc() - .crate_type("bin") - .input("main.rs") - .arg(&format!("-Wl,-rpath={}", cwd().display())) - .run(); + let relocation_models = ["static", "pic", "pie"]; + for relocation_model in relocation_models { + rustc() + .crate_type("bin") + .input("main.rs") + .arg(&format!("-Wl,-rpath={}", cwd().display())) + .arg(&format!("-Crelocation-model={}", relocation_model)) + .output(&format!("main-{}", relocation_model)) + .run(); + } - // Now, *after* building the binary, we build the library... + // Now, *after* building the binaries, we build the library... build_native_dynamic_lib("library"); - // ... and run with this library, ensuring it was linked correctly at runtime. - let output = run("main").stdout_utf8(); - - diff().expected_file("output.txt").actual_text("actual", output).run(); + for relocation_model in relocation_models { + // ... and run with this library, ensuring it was linked correctly at runtime. + // The output here should be the same regardless of the relocation model. + let output = run(&format!("main-{}", relocation_model)).stdout_utf8(); + diff().expected_file("output.txt").actual_text("actual", output).run(); + } } From a9838dd1ea9252723ed3a450c8e5b5c65672e477 Mon Sep 17 00:00:00 2001 From: Aryan Dubey Date: Tue, 17 Mar 2026 18:16:09 -0400 Subject: [PATCH 07/10] Moved issue-23981 to tests/ui/macros --- tests/ui/{issues/issue-23891.rs => macros/macro-in-or-pattern.rs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/ui/{issues/issue-23891.rs => macros/macro-in-or-pattern.rs} (100%) diff --git a/tests/ui/issues/issue-23891.rs b/tests/ui/macros/macro-in-or-pattern.rs similarity index 100% rename from tests/ui/issues/issue-23891.rs rename to tests/ui/macros/macro-in-or-pattern.rs From fb885759e00987f4dbc9fa77edce3bfb963264aa Mon Sep 17 00:00:00 2001 From: Aryan Dubey Date: Tue, 17 Mar 2026 18:24:54 -0400 Subject: [PATCH 08/10] Added test description and a link to github page for the relevant issue to the test --- tests/ui/macros/macro-in-or-pattern.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/ui/macros/macro-in-or-pattern.rs b/tests/ui/macros/macro-in-or-pattern.rs index 131139470f9fc..c1dcd6efeaa4a 100644 --- a/tests/ui/macros/macro-in-or-pattern.rs +++ b/tests/ui/macros/macro-in-or-pattern.rs @@ -1,3 +1,6 @@ +// Regression test for https://github.com/rust-lang/rust/issues/23891 +// Tests that a macro expanding to a pattern works correctly inside of or patterns + //@ run-pass macro_rules! id { ($s: pat) => ($s); From 4c16ba1a4de3516d39fa2bac96cfeb8110a365c0 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Wed, 18 Mar 2026 12:50:33 +1100 Subject: [PATCH 09/10] Move trait `IntoQueryParam` into its own file Removing coherent and self-contained chunks of code from a catch-all "plumbing" module is typically an improvement. Giving the trait its own file also gets rid of the unhelpful `sealed` module that it was previously in. --- .../rustc_middle/src/query/into_query_key.rs | 59 +++++++++++++++++ compiler/rustc_middle/src/query/mod.rs | 6 +- compiler/rustc_middle/src/query/plumbing.rs | 66 ------------------- 3 files changed, 63 insertions(+), 68 deletions(-) create mode 100644 compiler/rustc_middle/src/query/into_query_key.rs diff --git a/compiler/rustc_middle/src/query/into_query_key.rs b/compiler/rustc_middle/src/query/into_query_key.rs new file mode 100644 index 0000000000000..00acce60c8336 --- /dev/null +++ b/compiler/rustc_middle/src/query/into_query_key.rs @@ -0,0 +1,59 @@ +use rustc_hir::OwnerId; +use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId, ModDefId}; + +/// An analogue of the `Into` trait that's intended only for query parameters. +/// +/// This exists to allow queries to accept either `DefId` or `LocalDefId` while requiring that the +/// user call `to_def_id` to convert between them everywhere else. +pub trait IntoQueryParam

{ + fn into_query_param(self) -> P; +} + +impl

IntoQueryParam

for P { + #[inline(always)] + fn into_query_param(self) -> P { + self + } +} + +impl IntoQueryParam for OwnerId { + #[inline(always)] + fn into_query_param(self) -> LocalDefId { + self.def_id + } +} + +impl IntoQueryParam for LocalDefId { + #[inline(always)] + fn into_query_param(self) -> DefId { + self.to_def_id() + } +} + +impl IntoQueryParam for OwnerId { + #[inline(always)] + fn into_query_param(self) -> DefId { + self.to_def_id() + } +} + +impl IntoQueryParam for ModDefId { + #[inline(always)] + fn into_query_param(self) -> DefId { + self.to_def_id() + } +} + +impl IntoQueryParam for LocalModDefId { + #[inline(always)] + fn into_query_param(self) -> DefId { + self.to_def_id() + } +} + +impl IntoQueryParam for LocalModDefId { + #[inline(always)] + fn into_query_param(self) -> LocalDefId { + self.into() + } +} diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 1723a853ea750..1df7aed31a5a6 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -1,11 +1,12 @@ use rustc_hir::def_id::LocalDefId; pub use self::caches::{DefIdCache, DefaultCache, QueryCache, SingleCache, VecCache}; +pub use self::into_query_key::IntoQueryParam; pub use self::job::{QueryJob, QueryJobId, QueryLatch, QueryWaiter}; pub use self::keys::{AsLocalQueryKey, LocalCrate, QueryKey}; pub use self::plumbing::{ - ActiveKeyStatus, CycleError, EnsureMode, IntoQueryParam, QueryMode, QueryState, QuerySystem, - QueryVTable, TyCtxtAt, TyCtxtEnsureDone, TyCtxtEnsureOk, TyCtxtEnsureResult, + ActiveKeyStatus, CycleError, EnsureMode, QueryMode, QueryState, QuerySystem, QueryVTable, + TyCtxtAt, TyCtxtEnsureDone, TyCtxtEnsureOk, TyCtxtEnsureResult, }; pub use self::stack::QueryStackFrame; pub use crate::queries::Providers; @@ -15,6 +16,7 @@ pub(crate) mod arena_cached; mod caches; pub mod erase; pub(crate) mod inner; +mod into_query_key; mod job; mod keys; pub(crate) mod modifiers; diff --git a/compiler/rustc_middle/src/query/plumbing.rs b/compiler/rustc_middle/src/query/plumbing.rs index e8e2c8dbd2249..c00aae61fea80 100644 --- a/compiler/rustc_middle/src/query/plumbing.rs +++ b/compiler/rustc_middle/src/query/plumbing.rs @@ -6,10 +6,7 @@ use rustc_data_structures::hash_table::HashTable; use rustc_data_structures::sharded::Sharded; use rustc_data_structures::sync::{AtomicU64, WorkerLocal}; use rustc_errors::Diag; -use rustc_hir::def_id::{DefId, LocalDefId}; -use rustc_hir::hir_id::OwnerId; use rustc_span::Span; -pub use sealed::IntoQueryParam; use crate::dep_graph::{DepKind, DepNodeIndex, SerializedDepNodeIndex}; use crate::ich::StableHashingContext; @@ -647,69 +644,6 @@ macro_rules! define_callbacks { pub(crate) use define_callbacks; pub(crate) use query_helper_param_ty; -mod sealed { - use rustc_hir::def_id::{LocalModDefId, ModDefId}; - - use super::{DefId, LocalDefId, OwnerId}; - - /// An analogue of the `Into` trait that's intended only for query parameters. - /// - /// This exists to allow queries to accept either `DefId` or `LocalDefId` while requiring that the - /// user call `to_def_id` to convert between them everywhere else. - pub trait IntoQueryParam

{ - fn into_query_param(self) -> P; - } - - impl

IntoQueryParam

for P { - #[inline(always)] - fn into_query_param(self) -> P { - self - } - } - - impl IntoQueryParam for OwnerId { - #[inline(always)] - fn into_query_param(self) -> LocalDefId { - self.def_id - } - } - - impl IntoQueryParam for LocalDefId { - #[inline(always)] - fn into_query_param(self) -> DefId { - self.to_def_id() - } - } - - impl IntoQueryParam for OwnerId { - #[inline(always)] - fn into_query_param(self) -> DefId { - self.to_def_id() - } - } - - impl IntoQueryParam for ModDefId { - #[inline(always)] - fn into_query_param(self) -> DefId { - self.to_def_id() - } - } - - impl IntoQueryParam for LocalModDefId { - #[inline(always)] - fn into_query_param(self) -> DefId { - self.to_def_id() - } - } - - impl IntoQueryParam for LocalModDefId { - #[inline(always)] - fn into_query_param(self) -> LocalDefId { - self.into() - } - } -} - #[cold] pub(crate) fn default_query(name: &str, key: &dyn std::fmt::Debug) -> ! { bug!( From 38aaeefb94e54877d30ba2d596f7916b7bd02b96 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Wed, 18 Mar 2026 13:04:36 +1100 Subject: [PATCH 10/10] Rename `IntoQueryParam` to `IntoQueryKey` and tweak some occurrences All methods that accept `impl IntoQueryKey<_>` have been adjusted to consistently call `into_query_key` before doing anything else. When a function with a conversion trait calls another function with the same conversion trait, doing the conversion eagerly can reduce the overall number of instantiations. --- .../rustc_middle/src/query/into_query_key.rs | 49 +++++++++++------- compiler/rustc_middle/src/query/mod.rs | 2 +- compiler/rustc_middle/src/query/plumbing.rs | 16 +++--- compiler/rustc_middle/src/ty/context.rs | 22 ++++---- .../src/ty/context/impl_interner.rs | 2 - compiler/rustc_middle/src/ty/mod.rs | 50 +++++++++++-------- compiler/rustc_middle/src/ty/print/pretty.rs | 13 ++--- 7 files changed, 84 insertions(+), 70 deletions(-) diff --git a/compiler/rustc_middle/src/query/into_query_key.rs b/compiler/rustc_middle/src/query/into_query_key.rs index 00acce60c8336..04bbfd5c3a8ab 100644 --- a/compiler/rustc_middle/src/query/into_query_key.rs +++ b/compiler/rustc_middle/src/query/into_query_key.rs @@ -1,59 +1,70 @@ use rustc_hir::OwnerId; use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId, ModDefId}; -/// An analogue of the `Into` trait that's intended only for query parameters. +/// Argument-conversion trait used by some queries and other `TyCtxt` methods. /// -/// This exists to allow queries to accept either `DefId` or `LocalDefId` while requiring that the -/// user call `to_def_id` to convert between them everywhere else. -pub trait IntoQueryParam

{ - fn into_query_param(self) -> P; +/// A function that accepts an `impl IntoQueryKey` argument can be thought +/// of as taking a [`DefId`], except that callers can also pass a [`LocalDefId`] +/// or values of other narrower ID types, as long as they have a trivial conversion +/// to `DefId`. +/// +/// Using a dedicated trait instead of [`Into`] makes the purpose of the conversion +/// more explicit, and makes occurrences easier to search for. +pub trait IntoQueryKey { + /// Argument conversion from `Self` to `K`. + /// This should always be a very cheap conversion, e.g. [`LocalDefId::to_def_id`]. + fn into_query_key(self) -> K; } -impl

IntoQueryParam

for P { +/// Any type can be converted to itself. +/// +/// This is useful in generic or macro-generated code where we don't know whether +/// conversion is actually needed, so that we can do a conversion unconditionally. +impl IntoQueryKey for K { #[inline(always)] - fn into_query_param(self) -> P { + fn into_query_key(self) -> K { self } } -impl IntoQueryParam for OwnerId { +impl IntoQueryKey for OwnerId { #[inline(always)] - fn into_query_param(self) -> LocalDefId { + fn into_query_key(self) -> LocalDefId { self.def_id } } -impl IntoQueryParam for LocalDefId { +impl IntoQueryKey for LocalDefId { #[inline(always)] - fn into_query_param(self) -> DefId { + fn into_query_key(self) -> DefId { self.to_def_id() } } -impl IntoQueryParam for OwnerId { +impl IntoQueryKey for OwnerId { #[inline(always)] - fn into_query_param(self) -> DefId { + fn into_query_key(self) -> DefId { self.to_def_id() } } -impl IntoQueryParam for ModDefId { +impl IntoQueryKey for ModDefId { #[inline(always)] - fn into_query_param(self) -> DefId { + fn into_query_key(self) -> DefId { self.to_def_id() } } -impl IntoQueryParam for LocalModDefId { +impl IntoQueryKey for LocalModDefId { #[inline(always)] - fn into_query_param(self) -> DefId { + fn into_query_key(self) -> DefId { self.to_def_id() } } -impl IntoQueryParam for LocalModDefId { +impl IntoQueryKey for LocalModDefId { #[inline(always)] - fn into_query_param(self) -> LocalDefId { + fn into_query_key(self) -> LocalDefId { self.into() } } diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 1df7aed31a5a6..9c012f2da4dfc 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -1,7 +1,7 @@ use rustc_hir::def_id::LocalDefId; pub use self::caches::{DefIdCache, DefaultCache, QueryCache, SingleCache, VecCache}; -pub use self::into_query_key::IntoQueryParam; +pub use self::into_query_key::IntoQueryKey; pub use self::job::{QueryJob, QueryJobId, QueryLatch, QueryWaiter}; pub use self::keys::{AsLocalQueryKey, LocalCrate, QueryKey}; pub use self::plumbing::{ diff --git a/compiler/rustc_middle/src/query/plumbing.rs b/compiler/rustc_middle/src/query/plumbing.rs index c00aae61fea80..d4e7bb52be974 100644 --- a/compiler/rustc_middle/src/query/plumbing.rs +++ b/compiler/rustc_middle/src/query/plumbing.rs @@ -268,8 +268,8 @@ impl<'tcx> TyCtxt<'tcx> { } macro_rules! query_helper_param_ty { - (DefId) => { impl $crate::query::IntoQueryParam }; - (LocalDefId) => { impl $crate::query::IntoQueryParam }; + (DefId) => { impl $crate::query::IntoQueryKey }; + (LocalDefId) => { impl $crate::query::IntoQueryKey }; ($K:ty) => { $K }; } @@ -410,7 +410,7 @@ macro_rules! define_callbacks { crate::query::inner::query_ensure_ok_or_done( self.tcx, &self.tcx.query_system.query_vtables.$name, - $crate::query::IntoQueryParam::into_query_param(key), + $crate::query::IntoQueryKey::into_query_key(key), $crate::query::EnsureMode::Ok, ) } @@ -430,7 +430,7 @@ macro_rules! define_callbacks { crate::query::inner::query_ensure_result( self.tcx, &self.tcx.query_system.query_vtables.$name, - $crate::query::IntoQueryParam::into_query_param(key), + $crate::query::IntoQueryKey::into_query_key(key), ) } )* @@ -444,7 +444,7 @@ macro_rules! define_callbacks { crate::query::inner::query_ensure_ok_or_done( self.tcx, &self.tcx.query_system.query_vtables.$name, - $crate::query::IntoQueryParam::into_query_param(key), + $crate::query::IntoQueryKey::into_query_key(key), $crate::query::EnsureMode::Done, ); } @@ -473,7 +473,7 @@ macro_rules! define_callbacks { self.tcx, self.span, &self.tcx.query_system.query_vtables.$name, - $crate::query::IntoQueryParam::into_query_param(key), + $crate::query::IntoQueryKey::into_query_key(key), )) } )* @@ -481,13 +481,13 @@ macro_rules! define_callbacks { $( #[cfg($feedable)] - impl<'tcx, K: $crate::query::IntoQueryParam<$name::Key<'tcx>> + Copy> + impl<'tcx, K: $crate::query::IntoQueryKey<$name::Key<'tcx>> + Copy> TyCtxtFeed<'tcx, K> { $(#[$attr])* #[inline(always)] pub fn $name(self, value: $name::ProvidedValue<'tcx>) { - let key = self.key().into_query_param(); + let key = self.key().into_query_key(); let erased_value = $name::provided_to_erased(self.tcx, value); $crate::query::inner::query_feed( self.tcx, diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 037ee835c382f..56428780d22da 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -61,7 +61,7 @@ use crate::middle::codegen_fn_attrs::{CodegenFnAttrs, TargetFeature}; use crate::middle::resolve_bound_vars; use crate::mir::interpret::{self, Allocation, ConstAllocation}; use crate::mir::{Body, Local, Place, PlaceElem, ProjectionKind, Promoted}; -use crate::query::{IntoQueryParam, LocalCrate, Providers, QuerySystem, TyCtxtAt}; +use crate::query::{IntoQueryKey, LocalCrate, Providers, QuerySystem, TyCtxtAt}; use crate::thir::Thir; use crate::traits; use crate::traits::solve::{ExternalConstraints, ExternalConstraintsData, PredefinedOpaques}; @@ -1111,10 +1111,9 @@ impl<'tcx> TyCtxt<'tcx> { } /// Check if the given `def_id` is a `type const` (mgca) - pub fn is_type_const>(self, def_id: I) -> bool { - // No need to call the query directly in this case always false. - let def_kind = self.def_kind(def_id.into_query_param()); - match def_kind { + pub fn is_type_const(self, def_id: impl IntoQueryKey) -> bool { + let def_id = def_id.into_query_key(); + match self.def_kind(def_id) { DefKind::Const { is_type_const } | DefKind::AssocConst { is_type_const } => { is_type_const } @@ -1168,8 +1167,8 @@ impl<'tcx> TyCtxt<'tcx> { self.features_query(()) } - pub fn def_key(self, id: impl IntoQueryParam) -> rustc_hir::definitions::DefKey { - let id = id.into_query_param(); + pub fn def_key(self, id: impl IntoQueryKey) -> rustc_hir::definitions::DefKey { + let id = id.into_query_key(); // Accessing the DefKey is ok, since it is part of DefPathHash. if let Some(id) = id.as_local() { self.definitions_untracked().def_key(id) @@ -2705,7 +2704,8 @@ impl<'tcx> TyCtxt<'tcx> { self.sess.opts.unstable_opts.build_sdylib_interface } - pub fn intrinsic(self, def_id: impl IntoQueryParam + Copy) -> Option { + pub fn intrinsic(self, def_id: impl IntoQueryKey) -> Option { + let def_id = def_id.into_query_key(); match self.def_kind(def_id) { DefKind::Fn | DefKind::AssocFn => self.intrinsic_raw(def_id), _ => None, @@ -2774,10 +2774,8 @@ impl<'tcx> TyCtxt<'tcx> { find_attr!(self, def_id, DoNotRecommend { .. }) } - pub fn is_trivial_const

(self, def_id: P) -> bool - where - P: IntoQueryParam, - { + pub fn is_trivial_const(self, def_id: impl IntoQueryKey) -> bool { + let def_id = def_id.into_query_key(); self.trivial_const(def_id).is_some() } diff --git a/compiler/rustc_middle/src/ty/context/impl_interner.rs b/compiler/rustc_middle/src/ty/context/impl_interner.rs index 5179a77d6cb6f..7bbfb11698fe1 100644 --- a/compiler/rustc_middle/src/ty/context/impl_interner.rs +++ b/compiler/rustc_middle/src/ty/context/impl_interner.rs @@ -14,7 +14,6 @@ use rustc_type_ir::{CollectAndApply, Interner, TypeFoldable, search_graph}; use crate::dep_graph::{DepKind, DepNodeIndex}; use crate::infer::canonical::CanonicalVarKinds; -use crate::query::IntoQueryParam; use crate::traits::cache::WithDepNode; use crate::traits::solve::{ self, CanonicalInput, ExternalConstraints, ExternalConstraintsData, QueryResult, inspect, @@ -720,7 +719,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> { } fn item_name(self, id: DefId) -> Symbol { - let id = id.into_query_param(); self.opt_item_name(id).unwrap_or_else(|| { bug!("item_name: no name for {:?}", self.def_path(id)); }) diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 2b02d42890aa9..d35b755b3193f 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -117,7 +117,7 @@ use crate::ich::StableHashingContext; use crate::metadata::{AmbigModChild, ModChild}; use crate::middle::privacy::EffectiveVisibilities; use crate::mir::{Body, CoroutineLayout, CoroutineSavedLocal, SourceInfo}; -use crate::query::{IntoQueryParam, Providers}; +use crate::query::{IntoQueryKey, Providers}; use crate::ty; use crate::ty::codec::{TyDecoder, TyEncoder}; pub use crate::ty::diagnostics::*; @@ -1031,12 +1031,14 @@ impl<'tcx> TypingEnv<'tcx> { /// converted to use proper canonical inputs instead. pub fn non_body_analysis( tcx: TyCtxt<'tcx>, - def_id: impl IntoQueryParam, + def_id: impl IntoQueryKey, ) -> TypingEnv<'tcx> { + let def_id = def_id.into_query_key(); TypingEnv { typing_mode: TypingMode::non_body_analysis(), param_env: tcx.param_env(def_id) } } - pub fn post_analysis(tcx: TyCtxt<'tcx>, def_id: impl IntoQueryParam) -> TypingEnv<'tcx> { + pub fn post_analysis(tcx: TyCtxt<'tcx>, def_id: impl IntoQueryKey) -> TypingEnv<'tcx> { + let def_id = def_id.into_query_key(); tcx.typing_env_normalized_for_post_analysis(def_id) } @@ -1528,8 +1530,8 @@ impl<'tcx> TyCtxt<'tcx> { } /// Look up the name of a definition across crates. This does not look at HIR. - pub fn opt_item_name(self, def_id: impl IntoQueryParam) -> Option { - let def_id = def_id.into_query_param(); + pub fn opt_item_name(self, def_id: impl IntoQueryKey) -> Option { + let def_id = def_id.into_query_key(); if let Some(cnum) = def_id.as_crate_root() { Some(self.crate_name(cnum)) } else { @@ -1549,8 +1551,8 @@ impl<'tcx> TyCtxt<'tcx> { /// [`opt_item_name`] instead. /// /// [`opt_item_name`]: Self::opt_item_name - pub fn item_name(self, id: impl IntoQueryParam) -> Symbol { - let id = id.into_query_param(); + pub fn item_name(self, id: impl IntoQueryKey) -> Symbol { + let id = id.into_query_key(); self.opt_item_name(id).unwrap_or_else(|| { bug!("item_name: no name for {:?}", self.def_path(id)); }) @@ -1559,8 +1561,8 @@ impl<'tcx> TyCtxt<'tcx> { /// Look up the name and span of a definition. /// /// See [`item_name`][Self::item_name] for more information. - pub fn opt_item_ident(self, def_id: impl IntoQueryParam) -> Option { - let def_id = def_id.into_query_param(); + pub fn opt_item_ident(self, def_id: impl IntoQueryKey) -> Option { + let def_id = def_id.into_query_key(); let def = self.opt_item_name(def_id)?; let span = self .def_ident_span(def_id) @@ -1571,8 +1573,8 @@ impl<'tcx> TyCtxt<'tcx> { /// Look up the name and span of a definition. /// /// See [`item_name`][Self::item_name] for more information. - pub fn item_ident(self, def_id: impl IntoQueryParam) -> Ident { - let def_id = def_id.into_query_param(); + pub fn item_ident(self, def_id: impl IntoQueryKey) -> Ident { + let def_id = def_id.into_query_key(); self.opt_item_ident(def_id).unwrap_or_else(|| { bug!("item_ident: no name for {:?}", self.def_path(def_id)); }) @@ -1902,8 +1904,9 @@ impl<'tcx> TyCtxt<'tcx> { } /// Returns the trait item that is implemented by the given item `DefId`. - pub fn trait_item_of(self, def_id: impl IntoQueryParam) -> Option { - self.opt_associated_item(def_id.into_query_param())?.trait_item_def_id() + pub fn trait_item_of(self, def_id: impl IntoQueryKey) -> Option { + let def_id = def_id.into_query_key(); + self.opt_associated_item(def_id)?.trait_item_def_id() } /// If the given `DefId` is an associated item of a trait, @@ -1915,8 +1918,8 @@ impl<'tcx> TyCtxt<'tcx> { } } - pub fn impl_is_of_trait(self, def_id: impl IntoQueryParam) -> bool { - let def_id = def_id.into_query_param(); + pub fn impl_is_of_trait(self, def_id: impl IntoQueryKey) -> bool { + let def_id = def_id.into_query_key(); let DefKind::Impl { of_trait } = self.def_kind(def_id) else { panic!("expected Impl for {def_id:?}"); }; @@ -1950,15 +1953,17 @@ impl<'tcx> TyCtxt<'tcx> { } } - pub fn impl_polarity(self, def_id: impl IntoQueryParam) -> ty::ImplPolarity { + pub fn impl_polarity(self, def_id: impl IntoQueryKey) -> ty::ImplPolarity { + let def_id = def_id.into_query_key(); self.impl_trait_header(def_id).polarity } /// Given an `impl_id`, return the trait it implements. pub fn impl_trait_ref( self, - def_id: impl IntoQueryParam, + def_id: impl IntoQueryKey, ) -> ty::EarlyBinder<'tcx, ty::TraitRef<'tcx>> { + let def_id = def_id.into_query_key(); self.impl_trait_header(def_id).trait_ref } @@ -1966,21 +1971,22 @@ impl<'tcx> TyCtxt<'tcx> { /// Returns `None` if it is an inherent impl. pub fn impl_opt_trait_ref( self, - def_id: impl IntoQueryParam, + def_id: impl IntoQueryKey, ) -> Option>> { - let def_id = def_id.into_query_param(); + let def_id = def_id.into_query_key(); self.impl_is_of_trait(def_id).then(|| self.impl_trait_ref(def_id)) } /// Given the `DefId` of an impl, returns the `DefId` of the trait it implements. - pub fn impl_trait_id(self, def_id: impl IntoQueryParam) -> DefId { + pub fn impl_trait_id(self, def_id: impl IntoQueryKey) -> DefId { + let def_id = def_id.into_query_key(); self.impl_trait_ref(def_id).skip_binder().def_id } /// Given the `DefId` of an impl, returns the `DefId` of the trait it implements. /// Returns `None` if it is an inherent impl. - pub fn impl_opt_trait_id(self, def_id: impl IntoQueryParam) -> Option { - let def_id = def_id.into_query_param(); + pub fn impl_opt_trait_id(self, def_id: impl IntoQueryKey) -> Option { + let def_id = def_id.into_query_key(); self.impl_is_of_trait(def_id).then(|| self.impl_trait_id(def_id)) } diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index 8b8fe522842a4..2c14c37609d41 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -23,7 +23,7 @@ use smallvec::SmallVec; // `pretty` is a separate module only for organization. use super::*; use crate::mir::interpret::{AllocRange, GlobalAlloc, Pointer, Provenance, Scalar}; -use crate::query::{IntoQueryParam, Providers}; +use crate::query::{IntoQueryKey, Providers}; use crate::ty::{ ConstInt, Expr, GenericArgKind, ParamConst, ScalarInt, Term, TermKind, TraitPredicate, TypeFoldable, TypeSuperFoldable, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, @@ -2180,17 +2180,18 @@ fn guess_def_namespace(tcx: TyCtxt<'_>, def_id: DefId) -> Namespace { impl<'t> TyCtxt<'t> { /// Returns a string identifying this `DefId`. This string is /// suitable for user output. - pub fn def_path_str(self, def_id: impl IntoQueryParam) -> String { + pub fn def_path_str(self, def_id: impl IntoQueryKey) -> String { + let def_id = def_id.into_query_key(); self.def_path_str_with_args(def_id, &[]) } /// For this one we determine the appropriate namespace for the `def_id`. pub fn def_path_str_with_args( self, - def_id: impl IntoQueryParam, + def_id: impl IntoQueryKey, args: &'t [GenericArg<'t>], ) -> String { - let def_id = def_id.into_query_param(); + let def_id = def_id.into_query_key(); let ns = guess_def_namespace(self, def_id); debug!("def_path_str: def_id={:?}, ns={:?}", def_id, ns); @@ -2200,10 +2201,10 @@ impl<'t> TyCtxt<'t> { /// For this one we always use value namespace. pub fn value_path_str_with_args( self, - def_id: impl IntoQueryParam, + def_id: impl IntoQueryKey, args: &'t [GenericArg<'t>], ) -> String { - let def_id = def_id.into_query_param(); + let def_id = def_id.into_query_key(); let ns = Namespace::ValueNS; debug!("value_path_str: def_id={:?}, ns={:?}", def_id, ns);