authorbors <bors@rust-lang.org> 2026-03-18 11:10:07 UTC
committerbors <bors@rust-lang.org> 2026-03-18 11:10:07 UTC
log53d60bb1c5d325b43419c7618287a1405dec36d0
tree52fe19ff9017c893ab5a083f9d0a94e9f9747ff4
parent70dd5b8d0e00ce34ecb7612c3b76a0b2f6601ab6
parent205d100ef54cbe148c2da2efa2b555740808c907

Auto merge of #154039 - Zalathar:rollup-Emyd5AO, r=Zalathar

Rollup of 3 pull requests Successful merges: - rust-lang/rust#153580 (Handle statics and TLS in raw-dylib for ELF) - rust-lang/rust#154020 (Moved tests/ui/issues/issue-23891.rs to tests/ui/macros/) - rust-lang/rust#154028 (Rename trait `IntoQueryParam` to `IntoQueryKey`)

17 files changed, 271 insertions(+), 170 deletions(-)

compiler/rustc_codegen_ssa/src/back/link/raw_dylib.rs+39-13
......@@ -9,7 +9,7 @@ use rustc_data_structures::stable_hasher::StableHasher;
99use rustc_hashes::Hash128;
1010use rustc_hir::attrs::NativeLibKind;
1111use rustc_session::Session;
12use rustc_session::cstore::DllImport;
12use rustc_session::cstore::{DllImport, DllImportSymbolType};
1313use rustc_span::Symbol;
1414use rustc_target::spec::Arch;
1515
......@@ -95,14 +95,14 @@ pub(super) fn create_raw_dylib_dll_import_libs<'a>(
9595 true,
9696 )
9797 }),
98 is_data: !import.is_fn,
98 is_data: import.symbol_type != DllImportSymbolType::Function,
9999 }
100100 } else {
101101 ImportLibraryItem {
102102 name: import.name.to_string(),
103103 ordinal: import.ordinal(),
104104 symbol_name: None,
105 is_data: !import.is_fn,
105 is_data: import.symbol_type != DllImportSymbolType::Function,
106106 }
107107 }
108108 })
......@@ -271,10 +271,10 @@ fn create_elf_raw_dylib_stub(sess: &Session, soname: &str, symbols: &[DllImport]
271271 vers.push((version_name, dynstr));
272272 id
273273 };
274 syms.push((name, dynstr, Some(ver), symbol.is_fn));
274 syms.push((name, dynstr, Some(ver), symbol.symbol_type, symbol.size));
275275 } else {
276276 let dynstr = stub.add_dynamic_string(symbol_name.as_bytes());
277 syms.push((symbol_name, dynstr, None, symbol.is_fn));
277 syms.push((symbol_name, dynstr, None, symbol.symbol_type, symbol.size));
278278 }
279279 }
280280
......@@ -296,6 +296,8 @@ fn create_elf_raw_dylib_stub(sess: &Session, soname: &str, symbols: &[DllImport]
296296 stub.reserve_shstrtab_section_index();
297297 let text_section_name = stub.add_section_name(".text".as_bytes());
298298 let text_section = stub.reserve_section_index();
299 let data_section_name = stub.add_section_name(".data".as_bytes());
300 let data_section = stub.reserve_section_index();
299301 stub.reserve_dynsym_section_index();
300302 stub.reserve_dynstr_section_index();
301303 if !vers.is_empty() {
......@@ -375,7 +377,7 @@ fn create_elf_raw_dylib_stub(sess: &Session, soname: &str, symbols: &[DllImport]
375377 // Section headers
376378 stub.write_null_section_header();
377379 stub.write_shstrtab_section_header();
378 // Create a dummy .text section for our dummy symbols.
380 // Create a dummy .text section for our dummy non-data symbols.
379381 stub.write_section_header(&write::SectionHeader {
380382 name: Some(text_section_name),
381383 sh_type: elf::SHT_PROGBITS,
......@@ -385,7 +387,20 @@ fn create_elf_raw_dylib_stub(sess: &Session, soname: &str, symbols: &[DllImport]
385387 sh_size: 0,
386388 sh_link: 0,
387389 sh_info: 0,
388 sh_addralign: 1,
390 sh_addralign: 16,
391 sh_entsize: 0,
392 });
393 // And also a dummy .data section for our dummy data symbols.
394 stub.write_section_header(&write::SectionHeader {
395 name: Some(data_section_name),
396 sh_type: elf::SHT_PROGBITS,
397 sh_flags: (elf::SHF_WRITE | elf::SHF_ALLOC) as u64,
398 sh_addr: 0,
399 sh_offset: 0,
400 sh_size: 0,
401 sh_link: 0,
402 sh_info: 0,
403 sh_addralign: 16,
389404 sh_entsize: 0,
390405 });
391406 stub.write_dynsym_section_header(0, 1);
......@@ -398,17 +413,28 @@ fn create_elf_raw_dylib_stub(sess: &Session, soname: &str, symbols: &[DllImport]
398413
399414 // .dynsym
400415 stub.write_null_dynamic_symbol();
401 for (_name, dynstr, _ver, is_fn) in syms.iter().copied() {
402 let sym_type = if is_fn { elf::STT_FUNC } else { elf::STT_NOTYPE };
416 // Linkers like LLD require at least somewhat reasonable symbol values rather than zero,
417 // otherwise all the symbols might get put at the same address. Thus we increment the value
418 // every time we write a symbol.
419 let mut st_value = 0;
420 for (_name, dynstr, _ver, symbol_type, size) in syms.iter().copied() {
421 let sym_type = match symbol_type {
422 DllImportSymbolType::Function => elf::STT_FUNC,
423 DllImportSymbolType::Static => elf::STT_OBJECT,
424 DllImportSymbolType::ThreadLocal => elf::STT_TLS,
425 };
426 let section =
427 if symbol_type == DllImportSymbolType::Static { data_section } else { text_section };
403428 stub.write_dynamic_symbol(&write::Sym {
404429 name: Some(dynstr),
405430 st_info: (elf::STB_GLOBAL << 4) | sym_type,
406431 st_other: elf::STV_DEFAULT,
407 section: Some(text_section),
432 section: Some(section),
408433 st_shndx: 0, // ignored by object in favor of the `section` field
409 st_value: 0,
410 st_size: 0,
434 st_value,
435 st_size: size.bytes(),
411436 });
437 st_value += 8;
412438 }
413439
414440 // .dynstr
......@@ -418,7 +444,7 @@ fn create_elf_raw_dylib_stub(sess: &Session, soname: &str, symbols: &[DllImport]
418444 if !vers.is_empty() {
419445 // .gnu_version
420446 stub.write_null_gnu_versym();
421 for (_name, _dynstr, ver, _is_fn) in syms.iter().copied() {
447 for (_name, _dynstr, ver, _symbol_type, _size) in syms.iter().copied() {
422448 stub.write_gnu_versym(if let Some(ver) = ver {
423449 assert!((2 + ver as u16) < elf::VERSYM_HIDDEN);
424450 elf::VERSYM_HIDDEN | (2 + ver as u16)
compiler/rustc_codegen_ssa/src/common.rs+4-4
......@@ -5,7 +5,7 @@ use rustc_hir::attrs::PeImportNameType;
55use rustc_middle::ty::layout::TyAndLayout;
66use rustc_middle::ty::{self, Instance, TyCtxt};
77use rustc_middle::{bug, mir, span_bug};
8use rustc_session::cstore::{DllCallingConvention, DllImport};
8use rustc_session::cstore::{DllCallingConvention, DllImport, DllImportSymbolType};
99use rustc_span::Span;
1010use rustc_target::spec::{Abi, Env, Os, Target};
1111
......@@ -199,7 +199,7 @@ pub fn i686_decorated_name(
199199 decorated_name.push('\x01');
200200 }
201201
202 let prefix = if add_prefix && dll_import.is_fn {
202 let prefix = if add_prefix && dll_import.symbol_type == DllImportSymbolType::Function {
203203 match dll_import.calling_convention {
204204 DllCallingConvention::C | DllCallingConvention::Vectorcall(_) => None,
205205 DllCallingConvention::Stdcall(_) => (!mingw
......@@ -207,7 +207,7 @@ pub fn i686_decorated_name(
207207 .then_some('_'),
208208 DllCallingConvention::Fastcall(_) => Some('@'),
209209 }
210 } else if !dll_import.is_fn && !mingw {
210 } else if dll_import.symbol_type != DllImportSymbolType::Function && !mingw {
211211 // For static variables, prefix with '_' on MSVC.
212212 Some('_')
213213 } else {
......@@ -219,7 +219,7 @@ pub fn i686_decorated_name(
219219
220220 decorated_name.push_str(name);
221221
222 if add_suffix && dll_import.is_fn {
222 if add_suffix && dll_import.symbol_type == DllImportSymbolType::Function {
223223 use std::fmt::Write;
224224
225225 match dll_import.calling_convention {
compiler/rustc_metadata/src/native_libs.rs+33-8
......@@ -5,12 +5,17 @@ use rustc_abi::ExternAbi;
55use rustc_attr_parsing::eval_config_entry;
66use rustc_data_structures::fx::FxHashSet;
77use rustc_hir::attrs::{NativeLibKind, PeImportNameType};
8use rustc_hir::def::DefKind;
89use rustc_hir::find_attr;
10use rustc_middle::bug;
11use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
912use rustc_middle::query::LocalCrate;
1013use rustc_middle::ty::{self, List, Ty, TyCtxt};
1114use rustc_session::Session;
1215use rustc_session::config::CrateType;
13use rustc_session::cstore::{DllCallingConvention, DllImport, ForeignModule, NativeLib};
16use rustc_session::cstore::{
17 DllCallingConvention, DllImport, DllImportSymbolType, ForeignModule, NativeLib,
18};
1419use rustc_session::search_paths::PathKind;
1520use rustc_span::Symbol;
1621use rustc_span::def_id::{DefId, LOCAL_CRATE};
......@@ -451,12 +456,32 @@ impl<'tcx> Collector<'tcx> {
451456 }
452457 }
453458
454 DllImport {
455 name,
456 import_name_type,
457 calling_convention,
458 span,
459 is_fn: self.tcx.def_kind(item).is_fn_like(),
460 }
459 let def_kind = self.tcx.def_kind(item);
460 let symbol_type = if def_kind.is_fn_like() {
461 DllImportSymbolType::Function
462 } else if matches!(def_kind, DefKind::Static { .. }) {
463 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL) {
464 DllImportSymbolType::ThreadLocal
465 } else {
466 DllImportSymbolType::Static
467 }
468 } else {
469 bug!("Unexpected type for raw-dylib: {}", def_kind.descr(item));
470 };
471
472 let size = match symbol_type {
473 // We cannot determine the size of a function at compile time, but it shouldn't matter anyway.
474 DllImportSymbolType::Function => rustc_abi::Size::ZERO,
475 DllImportSymbolType::Static | DllImportSymbolType::ThreadLocal => {
476 let ty = self.tcx.type_of(item).instantiate_identity();
477 self.tcx
478 .layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(ty))
479 .ok()
480 .map(|layout| layout.size)
481 .unwrap_or_else(|| bug!("Non-function symbols must have a size"))
482 }
483 };
484
485 DllImport { name, import_name_type, calling_convention, span, symbol_type, size }
461486 }
462487}
compiler/rustc_middle/src/query/into_query_key.rs created+70
......@@ -0,0 +1,70 @@
1use rustc_hir::OwnerId;
2use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId, ModDefId};
3
4/// Argument-conversion trait used by some queries and other `TyCtxt` methods.
5///
6/// A function that accepts an `impl IntoQueryKey<DefId>` argument can be thought
7/// of as taking a [`DefId`], except that callers can also pass a [`LocalDefId`]
8/// or values of other narrower ID types, as long as they have a trivial conversion
9/// to `DefId`.
10///
11/// Using a dedicated trait instead of [`Into`] makes the purpose of the conversion
12/// more explicit, and makes occurrences easier to search for.
13pub trait IntoQueryKey<K> {
14 /// Argument conversion from `Self` to `K`.
15 /// This should always be a very cheap conversion, e.g. [`LocalDefId::to_def_id`].
16 fn into_query_key(self) -> K;
17}
18
19/// Any type can be converted to itself.
20///
21/// This is useful in generic or macro-generated code where we don't know whether
22/// conversion is actually needed, so that we can do a conversion unconditionally.
23impl<K> IntoQueryKey<K> for K {
24 #[inline(always)]
25 fn into_query_key(self) -> K {
26 self
27 }
28}
29
30impl IntoQueryKey<LocalDefId> for OwnerId {
31 #[inline(always)]
32 fn into_query_key(self) -> LocalDefId {
33 self.def_id
34 }
35}
36
37impl IntoQueryKey<DefId> for LocalDefId {
38 #[inline(always)]
39 fn into_query_key(self) -> DefId {
40 self.to_def_id()
41 }
42}
43
44impl IntoQueryKey<DefId> for OwnerId {
45 #[inline(always)]
46 fn into_query_key(self) -> DefId {
47 self.to_def_id()
48 }
49}
50
51impl IntoQueryKey<DefId> for ModDefId {
52 #[inline(always)]
53 fn into_query_key(self) -> DefId {
54 self.to_def_id()
55 }
56}
57
58impl IntoQueryKey<DefId> for LocalModDefId {
59 #[inline(always)]
60 fn into_query_key(self) -> DefId {
61 self.to_def_id()
62 }
63}
64
65impl IntoQueryKey<LocalDefId> for LocalModDefId {
66 #[inline(always)]
67 fn into_query_key(self) -> LocalDefId {
68 self.into()
69 }
70}
compiler/rustc_middle/src/query/mod.rs+4-2
......@@ -1,11 +1,12 @@
11use rustc_hir::def_id::LocalDefId;
22
33pub use self::caches::{DefIdCache, DefaultCache, QueryCache, SingleCache, VecCache};
4pub use self::into_query_key::IntoQueryKey;
45pub use self::job::{QueryJob, QueryJobId, QueryLatch, QueryWaiter};
56pub use self::keys::{AsLocalQueryKey, LocalCrate, QueryKey};
67pub use self::plumbing::{
7 ActiveKeyStatus, CycleError, EnsureMode, IntoQueryParam, QueryMode, QueryState, QuerySystem,
8 QueryVTable, TyCtxtAt, TyCtxtEnsureDone, TyCtxtEnsureOk, TyCtxtEnsureResult,
8 ActiveKeyStatus, CycleError, EnsureMode, QueryMode, QueryState, QuerySystem, QueryVTable,
9 TyCtxtAt, TyCtxtEnsureDone, TyCtxtEnsureOk, TyCtxtEnsureResult,
910};
1011pub use self::stack::QueryStackFrame;
1112pub use crate::queries::Providers;
......@@ -15,6 +16,7 @@ pub(crate) mod arena_cached;
1516mod caches;
1617pub mod erase;
1718pub(crate) mod inner;
19mod into_query_key;
1820mod job;
1921mod keys;
2022pub(crate) mod modifiers;
compiler/rustc_middle/src/query/plumbing.rs+8-74
......@@ -6,10 +6,7 @@ use rustc_data_structures::hash_table::HashTable;
66use rustc_data_structures::sharded::Sharded;
77use rustc_data_structures::sync::{AtomicU64, WorkerLocal};
88use rustc_errors::Diag;
9use rustc_hir::def_id::{DefId, LocalDefId};
10use rustc_hir::hir_id::OwnerId;
119use rustc_span::Span;
12pub use sealed::IntoQueryParam;
1310
1411use crate::dep_graph::{DepKind, DepNodeIndex, SerializedDepNodeIndex};
1512use crate::ich::StableHashingContext;
......@@ -271,8 +268,8 @@ impl<'tcx> TyCtxt<'tcx> {
271268}
272269
273270macro_rules! query_helper_param_ty {
274 (DefId) => { impl $crate::query::IntoQueryParam<DefId> };
275 (LocalDefId) => { impl $crate::query::IntoQueryParam<LocalDefId> };
271 (DefId) => { impl $crate::query::IntoQueryKey<DefId> };
272 (LocalDefId) => { impl $crate::query::IntoQueryKey<LocalDefId> };
276273 ($K:ty) => { $K };
277274}
278275
......@@ -413,7 +410,7 @@ macro_rules! define_callbacks {
413410 crate::query::inner::query_ensure_ok_or_done(
414411 self.tcx,
415412 &self.tcx.query_system.query_vtables.$name,
416 $crate::query::IntoQueryParam::into_query_param(key),
413 $crate::query::IntoQueryKey::into_query_key(key),
417414 $crate::query::EnsureMode::Ok,
418415 )
419416 }
......@@ -433,7 +430,7 @@ macro_rules! define_callbacks {
433430 crate::query::inner::query_ensure_result(
434431 self.tcx,
435432 &self.tcx.query_system.query_vtables.$name,
436 $crate::query::IntoQueryParam::into_query_param(key),
433 $crate::query::IntoQueryKey::into_query_key(key),
437434 )
438435 }
439436 )*
......@@ -447,7 +444,7 @@ macro_rules! define_callbacks {
447444 crate::query::inner::query_ensure_ok_or_done(
448445 self.tcx,
449446 &self.tcx.query_system.query_vtables.$name,
450 $crate::query::IntoQueryParam::into_query_param(key),
447 $crate::query::IntoQueryKey::into_query_key(key),
451448 $crate::query::EnsureMode::Done,
452449 );
453450 }
......@@ -476,7 +473,7 @@ macro_rules! define_callbacks {
476473 self.tcx,
477474 self.span,
478475 &self.tcx.query_system.query_vtables.$name,
479 $crate::query::IntoQueryParam::into_query_param(key),
476 $crate::query::IntoQueryKey::into_query_key(key),
480477 ))
481478 }
482479 )*
......@@ -484,13 +481,13 @@ macro_rules! define_callbacks {
484481
485482 $(
486483 #[cfg($feedable)]
487 impl<'tcx, K: $crate::query::IntoQueryParam<$name::Key<'tcx>> + Copy>
484 impl<'tcx, K: $crate::query::IntoQueryKey<$name::Key<'tcx>> + Copy>
488485 TyCtxtFeed<'tcx, K>
489486 {
490487 $(#[$attr])*
491488 #[inline(always)]
492489 pub fn $name(self, value: $name::ProvidedValue<'tcx>) {
493 let key = self.key().into_query_param();
490 let key = self.key().into_query_key();
494491 let erased_value = $name::provided_to_erased(self.tcx, value);
495492 $crate::query::inner::query_feed(
496493 self.tcx,
......@@ -647,69 +644,6 @@ macro_rules! define_callbacks {
647644pub(crate) use define_callbacks;
648645pub(crate) use query_helper_param_ty;
649646
650mod sealed {
651 use rustc_hir::def_id::{LocalModDefId, ModDefId};
652
653 use super::{DefId, LocalDefId, OwnerId};
654
655 /// An analogue of the `Into` trait that's intended only for query parameters.
656 ///
657 /// This exists to allow queries to accept either `DefId` or `LocalDefId` while requiring that the
658 /// user call `to_def_id` to convert between them everywhere else.
659 pub trait IntoQueryParam<P> {
660 fn into_query_param(self) -> P;
661 }
662
663 impl<P> IntoQueryParam<P> for P {
664 #[inline(always)]
665 fn into_query_param(self) -> P {
666 self
667 }
668 }
669
670 impl IntoQueryParam<LocalDefId> for OwnerId {
671 #[inline(always)]
672 fn into_query_param(self) -> LocalDefId {
673 self.def_id
674 }
675 }
676
677 impl IntoQueryParam<DefId> for LocalDefId {
678 #[inline(always)]
679 fn into_query_param(self) -> DefId {
680 self.to_def_id()
681 }
682 }
683
684 impl IntoQueryParam<DefId> for OwnerId {
685 #[inline(always)]
686 fn into_query_param(self) -> DefId {
687 self.to_def_id()
688 }
689 }
690
691 impl IntoQueryParam<DefId> for ModDefId {
692 #[inline(always)]
693 fn into_query_param(self) -> DefId {
694 self.to_def_id()
695 }
696 }
697
698 impl IntoQueryParam<DefId> for LocalModDefId {
699 #[inline(always)]
700 fn into_query_param(self) -> DefId {
701 self.to_def_id()
702 }
703 }
704
705 impl IntoQueryParam<LocalDefId> for LocalModDefId {
706 #[inline(always)]
707 fn into_query_param(self) -> LocalDefId {
708 self.into()
709 }
710 }
711}
712
713647#[cold]
714648pub(crate) fn default_query(name: &str, key: &dyn std::fmt::Debug) -> ! {
715649 bug!(
compiler/rustc_middle/src/ty/context.rs+10-12
......@@ -61,7 +61,7 @@ use crate::middle::codegen_fn_attrs::{CodegenFnAttrs, TargetFeature};
6161use crate::middle::resolve_bound_vars;
6262use crate::mir::interpret::{self, Allocation, ConstAllocation};
6363use crate::mir::{Body, Local, Place, PlaceElem, ProjectionKind, Promoted};
64use crate::query::{IntoQueryParam, LocalCrate, Providers, QuerySystem, TyCtxtAt};
64use crate::query::{IntoQueryKey, LocalCrate, Providers, QuerySystem, TyCtxtAt};
6565use crate::thir::Thir;
6666use crate::traits;
6767use crate::traits::solve::{ExternalConstraints, ExternalConstraintsData, PredefinedOpaques};
......@@ -1111,10 +1111,9 @@ impl<'tcx> TyCtxt<'tcx> {
11111111 }
11121112
11131113 /// Check if the given `def_id` is a `type const` (mgca)
1114 pub fn is_type_const<I: Copy + IntoQueryParam<DefId>>(self, def_id: I) -> bool {
1115 // No need to call the query directly in this case always false.
1116 let def_kind = self.def_kind(def_id.into_query_param());
1117 match def_kind {
1114 pub fn is_type_const(self, def_id: impl IntoQueryKey<DefId>) -> bool {
1115 let def_id = def_id.into_query_key();
1116 match self.def_kind(def_id) {
11181117 DefKind::Const { is_type_const } | DefKind::AssocConst { is_type_const } => {
11191118 is_type_const
11201119 }
......@@ -1168,8 +1167,8 @@ impl<'tcx> TyCtxt<'tcx> {
11681167 self.features_query(())
11691168 }
11701169
1171 pub fn def_key(self, id: impl IntoQueryParam<DefId>) -> rustc_hir::definitions::DefKey {
1172 let id = id.into_query_param();
1170 pub fn def_key(self, id: impl IntoQueryKey<DefId>) -> rustc_hir::definitions::DefKey {
1171 let id = id.into_query_key();
11731172 // Accessing the DefKey is ok, since it is part of DefPathHash.
11741173 if let Some(id) = id.as_local() {
11751174 self.definitions_untracked().def_key(id)
......@@ -2705,7 +2704,8 @@ impl<'tcx> TyCtxt<'tcx> {
27052704 self.sess.opts.unstable_opts.build_sdylib_interface
27062705 }
27072706
2708 pub fn intrinsic(self, def_id: impl IntoQueryParam<DefId> + Copy) -> Option<ty::IntrinsicDef> {
2707 pub fn intrinsic(self, def_id: impl IntoQueryKey<DefId>) -> Option<ty::IntrinsicDef> {
2708 let def_id = def_id.into_query_key();
27092709 match self.def_kind(def_id) {
27102710 DefKind::Fn | DefKind::AssocFn => self.intrinsic_raw(def_id),
27112711 _ => None,
......@@ -2774,10 +2774,8 @@ impl<'tcx> TyCtxt<'tcx> {
27742774 find_attr!(self, def_id, DoNotRecommend { .. })
27752775 }
27762776
2777 pub fn is_trivial_const<P>(self, def_id: P) -> bool
2778 where
2779 P: IntoQueryParam<DefId>,
2780 {
2777 pub fn is_trivial_const(self, def_id: impl IntoQueryKey<DefId>) -> bool {
2778 let def_id = def_id.into_query_key();
27812779 self.trivial_const(def_id).is_some()
27822780 }
27832781
compiler/rustc_middle/src/ty/context/impl_interner.rs-2
......@@ -14,7 +14,6 @@ use rustc_type_ir::{CollectAndApply, Interner, TypeFoldable, search_graph};
1414
1515use crate::dep_graph::{DepKind, DepNodeIndex};
1616use crate::infer::canonical::CanonicalVarKinds;
17use crate::query::IntoQueryParam;
1817use crate::traits::cache::WithDepNode;
1918use crate::traits::solve::{
2019 self, CanonicalInput, ExternalConstraints, ExternalConstraintsData, QueryResult, inspect,
......@@ -720,7 +719,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
720719 }
721720
722721 fn item_name(self, id: DefId) -> Symbol {
723 let id = id.into_query_param();
724722 self.opt_item_name(id).unwrap_or_else(|| {
725723 bug!("item_name: no name for {:?}", self.def_path(id));
726724 })
compiler/rustc_middle/src/ty/mod.rs+28-22
......@@ -117,7 +117,7 @@ use crate::ich::StableHashingContext;
117117use crate::metadata::{AmbigModChild, ModChild};
118118use crate::middle::privacy::EffectiveVisibilities;
119119use crate::mir::{Body, CoroutineLayout, CoroutineSavedLocal, SourceInfo};
120use crate::query::{IntoQueryParam, Providers};
120use crate::query::{IntoQueryKey, Providers};
121121use crate::ty;
122122use crate::ty::codec::{TyDecoder, TyEncoder};
123123pub use crate::ty::diagnostics::*;
......@@ -1031,12 +1031,14 @@ impl<'tcx> TypingEnv<'tcx> {
10311031 /// converted to use proper canonical inputs instead.
10321032 pub fn non_body_analysis(
10331033 tcx: TyCtxt<'tcx>,
1034 def_id: impl IntoQueryParam<DefId>,
1034 def_id: impl IntoQueryKey<DefId>,
10351035 ) -> TypingEnv<'tcx> {
1036 let def_id = def_id.into_query_key();
10361037 TypingEnv { typing_mode: TypingMode::non_body_analysis(), param_env: tcx.param_env(def_id) }
10371038 }
10381039
1039 pub fn post_analysis(tcx: TyCtxt<'tcx>, def_id: impl IntoQueryParam<DefId>) -> TypingEnv<'tcx> {
1040 pub fn post_analysis(tcx: TyCtxt<'tcx>, def_id: impl IntoQueryKey<DefId>) -> TypingEnv<'tcx> {
1041 let def_id = def_id.into_query_key();
10401042 tcx.typing_env_normalized_for_post_analysis(def_id)
10411043 }
10421044
......@@ -1528,8 +1530,8 @@ impl<'tcx> TyCtxt<'tcx> {
15281530 }
15291531
15301532 /// Look up the name of a definition across crates. This does not look at HIR.
1531 pub fn opt_item_name(self, def_id: impl IntoQueryParam<DefId>) -> Option<Symbol> {
1532 let def_id = def_id.into_query_param();
1533 pub fn opt_item_name(self, def_id: impl IntoQueryKey<DefId>) -> Option<Symbol> {
1534 let def_id = def_id.into_query_key();
15331535 if let Some(cnum) = def_id.as_crate_root() {
15341536 Some(self.crate_name(cnum))
15351537 } else {
......@@ -1549,8 +1551,8 @@ impl<'tcx> TyCtxt<'tcx> {
15491551 /// [`opt_item_name`] instead.
15501552 ///
15511553 /// [`opt_item_name`]: Self::opt_item_name
1552 pub fn item_name(self, id: impl IntoQueryParam<DefId>) -> Symbol {
1553 let id = id.into_query_param();
1554 pub fn item_name(self, id: impl IntoQueryKey<DefId>) -> Symbol {
1555 let id = id.into_query_key();
15541556 self.opt_item_name(id).unwrap_or_else(|| {
15551557 bug!("item_name: no name for {:?}", self.def_path(id));
15561558 })
......@@ -1559,8 +1561,8 @@ impl<'tcx> TyCtxt<'tcx> {
15591561 /// Look up the name and span of a definition.
15601562 ///
15611563 /// See [`item_name`][Self::item_name] for more information.
1562 pub fn opt_item_ident(self, def_id: impl IntoQueryParam<DefId>) -> Option<Ident> {
1563 let def_id = def_id.into_query_param();
1564 pub fn opt_item_ident(self, def_id: impl IntoQueryKey<DefId>) -> Option<Ident> {
1565 let def_id = def_id.into_query_key();
15641566 let def = self.opt_item_name(def_id)?;
15651567 let span = self
15661568 .def_ident_span(def_id)
......@@ -1571,8 +1573,8 @@ impl<'tcx> TyCtxt<'tcx> {
15711573 /// Look up the name and span of a definition.
15721574 ///
15731575 /// See [`item_name`][Self::item_name] for more information.
1574 pub fn item_ident(self, def_id: impl IntoQueryParam<DefId>) -> Ident {
1575 let def_id = def_id.into_query_param();
1576 pub fn item_ident(self, def_id: impl IntoQueryKey<DefId>) -> Ident {
1577 let def_id = def_id.into_query_key();
15761578 self.opt_item_ident(def_id).unwrap_or_else(|| {
15771579 bug!("item_ident: no name for {:?}", self.def_path(def_id));
15781580 })
......@@ -1902,8 +1904,9 @@ impl<'tcx> TyCtxt<'tcx> {
19021904 }
19031905
19041906 /// Returns the trait item that is implemented by the given item `DefId`.
1905 pub fn trait_item_of(self, def_id: impl IntoQueryParam<DefId>) -> Option<DefId> {
1906 self.opt_associated_item(def_id.into_query_param())?.trait_item_def_id()
1907 pub fn trait_item_of(self, def_id: impl IntoQueryKey<DefId>) -> Option<DefId> {
1908 let def_id = def_id.into_query_key();
1909 self.opt_associated_item(def_id)?.trait_item_def_id()
19071910 }
19081911
19091912 /// If the given `DefId` is an associated item of a trait,
......@@ -1915,8 +1918,8 @@ impl<'tcx> TyCtxt<'tcx> {
19151918 }
19161919 }
19171920
1918 pub fn impl_is_of_trait(self, def_id: impl IntoQueryParam<DefId>) -> bool {
1919 let def_id = def_id.into_query_param();
1921 pub fn impl_is_of_trait(self, def_id: impl IntoQueryKey<DefId>) -> bool {
1922 let def_id = def_id.into_query_key();
19201923 let DefKind::Impl { of_trait } = self.def_kind(def_id) else {
19211924 panic!("expected Impl for {def_id:?}");
19221925 };
......@@ -1950,15 +1953,17 @@ impl<'tcx> TyCtxt<'tcx> {
19501953 }
19511954 }
19521955
1953 pub fn impl_polarity(self, def_id: impl IntoQueryParam<DefId>) -> ty::ImplPolarity {
1956 pub fn impl_polarity(self, def_id: impl IntoQueryKey<DefId>) -> ty::ImplPolarity {
1957 let def_id = def_id.into_query_key();
19541958 self.impl_trait_header(def_id).polarity
19551959 }
19561960
19571961 /// Given an `impl_id`, return the trait it implements.
19581962 pub fn impl_trait_ref(
19591963 self,
1960 def_id: impl IntoQueryParam<DefId>,
1964 def_id: impl IntoQueryKey<DefId>,
19611965 ) -> ty::EarlyBinder<'tcx, ty::TraitRef<'tcx>> {
1966 let def_id = def_id.into_query_key();
19621967 self.impl_trait_header(def_id).trait_ref
19631968 }
19641969
......@@ -1966,21 +1971,22 @@ impl<'tcx> TyCtxt<'tcx> {
19661971 /// Returns `None` if it is an inherent impl.
19671972 pub fn impl_opt_trait_ref(
19681973 self,
1969 def_id: impl IntoQueryParam<DefId>,
1974 def_id: impl IntoQueryKey<DefId>,
19701975 ) -> Option<ty::EarlyBinder<'tcx, ty::TraitRef<'tcx>>> {
1971 let def_id = def_id.into_query_param();
1976 let def_id = def_id.into_query_key();
19721977 self.impl_is_of_trait(def_id).then(|| self.impl_trait_ref(def_id))
19731978 }
19741979
19751980 /// Given the `DefId` of an impl, returns the `DefId` of the trait it implements.
1976 pub fn impl_trait_id(self, def_id: impl IntoQueryParam<DefId>) -> DefId {
1981 pub fn impl_trait_id(self, def_id: impl IntoQueryKey<DefId>) -> DefId {
1982 let def_id = def_id.into_query_key();
19771983 self.impl_trait_ref(def_id).skip_binder().def_id
19781984 }
19791985
19801986 /// Given the `DefId` of an impl, returns the `DefId` of the trait it implements.
19811987 /// Returns `None` if it is an inherent impl.
1982 pub fn impl_opt_trait_id(self, def_id: impl IntoQueryParam<DefId>) -> Option<DefId> {
1983 let def_id = def_id.into_query_param();
1988 pub fn impl_opt_trait_id(self, def_id: impl IntoQueryKey<DefId>) -> Option<DefId> {
1989 let def_id = def_id.into_query_key();
19841990 self.impl_is_of_trait(def_id).then(|| self.impl_trait_id(def_id))
19851991 }
19861992
compiler/rustc_middle/src/ty/print/pretty.rs+7-6
......@@ -23,7 +23,7 @@ use smallvec::SmallVec;
2323// `pretty` is a separate module only for organization.
2424use super::*;
2525use crate::mir::interpret::{AllocRange, GlobalAlloc, Pointer, Provenance, Scalar};
26use crate::query::{IntoQueryParam, Providers};
26use crate::query::{IntoQueryKey, Providers};
2727use crate::ty::{
2828 ConstInt, Expr, GenericArgKind, ParamConst, ScalarInt, Term, TermKind, TraitPredicate,
2929 TypeFoldable, TypeSuperFoldable, TypeSuperVisitable, TypeVisitable, TypeVisitableExt,
......@@ -2180,17 +2180,18 @@ fn guess_def_namespace(tcx: TyCtxt<'_>, def_id: DefId) -> Namespace {
21802180impl<'t> TyCtxt<'t> {
21812181 /// Returns a string identifying this `DefId`. This string is
21822182 /// suitable for user output.
2183 pub fn def_path_str(self, def_id: impl IntoQueryParam<DefId>) -> String {
2183 pub fn def_path_str(self, def_id: impl IntoQueryKey<DefId>) -> String {
2184 let def_id = def_id.into_query_key();
21842185 self.def_path_str_with_args(def_id, &[])
21852186 }
21862187
21872188 /// For this one we determine the appropriate namespace for the `def_id`.
21882189 pub fn def_path_str_with_args(
21892190 self,
2190 def_id: impl IntoQueryParam<DefId>,
2191 def_id: impl IntoQueryKey<DefId>,
21912192 args: &'t [GenericArg<'t>],
21922193 ) -> String {
2193 let def_id = def_id.into_query_param();
2194 let def_id = def_id.into_query_key();
21942195 let ns = guess_def_namespace(self, def_id);
21952196 debug!("def_path_str: def_id={:?}, ns={:?}", def_id, ns);
21962197
......@@ -2200,10 +2201,10 @@ impl<'t> TyCtxt<'t> {
22002201 /// For this one we always use value namespace.
22012202 pub fn value_path_str_with_args(
22022203 self,
2203 def_id: impl IntoQueryParam<DefId>,
2204 def_id: impl IntoQueryKey<DefId>,
22042205 args: &'t [GenericArg<'t>],
22052206 ) -> String {
2206 let def_id = def_id.into_query_param();
2207 let def_id = def_id.into_query_key();
22072208 let ns = Namespace::ValueNS;
22082209 debug!("value_path_str: def_id={:?}, ns={:?}", def_id, ns);
22092210
compiler/rustc_session/src/cstore.rs+9-2
......@@ -97,8 +97,15 @@ pub struct DllImport {
9797 pub calling_convention: DllCallingConvention,
9898 /// Span of import's "extern" declaration; used for diagnostics.
9999 pub span: Span,
100 /// Is this for a function (rather than a static variable).
101 pub is_fn: bool,
100 pub symbol_type: DllImportSymbolType,
101 pub size: rustc_abi::Size,
102}
103
104#[derive(Copy, Clone, Debug, Encodable, Decodable, HashStable_Generic, PartialEq)]
105pub enum DllImportSymbolType {
106 Function,
107 Static,
108 ThreadLocal,
102109}
103110
104111impl DllImport {
tests/run-make/raw-dylib-elf/library.c+5
......@@ -1,3 +1,8 @@
1int global_variable = 3;
2__thread int tls_variable = 33;
3const long const_array[] = {795, 906};
4const char *const_string = "sums of three cubes";
5
16int this_is_a_library_function() {
27 return 42;
38}
tests/run-make/raw-dylib-elf/main.rs+20-2
......@@ -1,11 +1,29 @@
11#![feature(raw_dylib_elf)]
2#![feature(thread_local)]
23#![allow(incomplete_features)]
34
5use std::ffi::{CStr, c_char, c_int, c_long};
6
47#[link(name = "library", kind = "raw-dylib")]
58unsafe extern "C" {
6 safe fn this_is_a_library_function() -> core::ffi::c_int;
9 safe fn this_is_a_library_function() -> c_int;
10 static mut global_variable: c_int;
11 #[thread_local]
12 static mut tls_variable: c_int;
13 safe static const_array: [c_long; 2];
14 safe static const_string: *const c_char;
715}
816
917fn main() {
10 println!("{}", this_is_a_library_function())
18 unsafe {
19 println!(
20 "{} {} {} {} {} {}",
21 this_is_a_library_function(),
22 global_variable,
23 tls_variable,
24 const_array[0],
25 const_array[1],
26 CStr::from_ptr(const_string).to_str().unwrap(),
27 );
28 }
1129}
tests/run-make/raw-dylib-elf/output.txt+1-1
......@@ -1 +1 @@
142
142 3 33 795 906 sums of three cubes
tests/run-make/raw-dylib-elf/rmake.rs+19-11
......@@ -11,19 +11,27 @@
1111use run_make_support::{build_native_dynamic_lib, cwd, diff, run, rustc};
1212
1313fn main() {
14 // We compile the binary without having the library present.
14 // We compile the binaries without having the library present with different relocation
15 // models.
1516 // We also set the rpath to the current directory so we can pick up the library at runtime.
16 rustc()
17 .crate_type("bin")
18 .input("main.rs")
19 .arg(&format!("-Wl,-rpath={}", cwd().display()))
20 .run();
17 let relocation_models = ["static", "pic", "pie"];
18 for relocation_model in relocation_models {
19 rustc()
20 .crate_type("bin")
21 .input("main.rs")
22 .arg(&format!("-Wl,-rpath={}", cwd().display()))
23 .arg(&format!("-Crelocation-model={}", relocation_model))
24 .output(&format!("main-{}", relocation_model))
25 .run();
26 }
2127
22 // Now, *after* building the binary, we build the library...
28 // Now, *after* building the binaries, we build the library...
2329 build_native_dynamic_lib("library");
2430
25 // ... and run with this library, ensuring it was linked correctly at runtime.
26 let output = run("main").stdout_utf8();
27
28 diff().expected_file("output.txt").actual_text("actual", output).run();
31 for relocation_model in relocation_models {
32 // ... and run with this library, ensuring it was linked correctly at runtime.
33 // The output here should be the same regardless of the relocation model.
34 let output = run(&format!("main-{}", relocation_model)).stdout_utf8();
35 diff().expected_file("output.txt").actual_text("actual", output).run();
36 }
2937}
tests/ui/issues/issue-23891.rs deleted-11
......@@ -1,11 +0,0 @@
1//@ run-pass
2macro_rules! id {
3 ($s: pat) => ($s);
4}
5
6fn main() {
7 match (Some(123), Some(456)) {
8 (id!(Some(a)), _) | (_, id!(Some(a))) => println!("{}", a),
9 _ => (),
10 }
11}
tests/ui/macros/macro-in-or-pattern.rs created+14
......@@ -0,0 +1,14 @@
1// Regression test for https://github.com/rust-lang/rust/issues/23891
2// Tests that a macro expanding to a pattern works correctly inside of or patterns
3
4//@ run-pass
5macro_rules! id {
6 ($s: pat) => ($s);
7}
8
9fn main() {
10 match (Some(123), Some(456)) {
11 (id!(Some(a)), _) | (_, id!(Some(a))) => println!("{}", a),
12 _ => (),
13 }
14}