1use rustc_abi::FIRST_VARIANT;
2use rustc_data_structures::stack::ensure_sufficient_stack;
3use rustc_data_structures::unord::{UnordMap, UnordSet};
4use rustc_hiras hir;
5use rustc_hir::attrs::AttributeKind;
6use rustc_hir::def::DefKind;
7use rustc_hir::find_attr;
8use rustc_middle::query::Providers;
9use rustc_middle::ty::{self, AdtDef, Instance, Ty, TyCtxt};
10use rustc_session::declare_lint;
11use rustc_span::{Span, Symbol};
12use tracing::{debug, instrument};
1314use crate::lints::{BuiltinClashingExtern, BuiltinClashingExternSub};
15use crate::{LintVec, types};
1617pub(crate) fn provide(providers: &mut Providers) {
18*providers = Providers { clashing_extern_declarations, ..*providers };
19}
2021pub(crate) fn get_lints() -> LintVec {
22<[_]>::into_vec(::alloc::boxed::box_new([CLASHING_EXTERN_DECLARATIONS]))vec![CLASHING_EXTERN_DECLARATIONS]23}
2425fn clashing_extern_declarations(tcx: TyCtxt<'_>, (): ()) {
26let mut lint = ClashingExternDeclarations::new();
27for id in tcx.hir_crate_items(()).foreign_items() {
28 lint.check_foreign_item(tcx, id);
29 }
30}
3132#[doc =
r" The `clashing_extern_declarations` lint detects when an `extern fn`"]
#[doc = r" has been declared with the same name but different types."]
#[doc = r""]
#[doc = r" ### Example"]
#[doc = r""]
#[doc = r" ```rust"]
#[doc = r" mod m {"]
#[doc = r#" unsafe extern "C" {"#]
#[doc = r" fn foo();"]
#[doc = r" }"]
#[doc = r" }"]
#[doc = r""]
#[doc = r#" unsafe extern "C" {"#]
#[doc = r" fn foo(_: u32);"]
#[doc = r" }"]
#[doc = r" ```"]
#[doc = r""]
#[doc = r" {{produces}}"]
#[doc = r""]
#[doc = r" ### Explanation"]
#[doc = r""]
#[doc = r" Because two symbols of the same name cannot be resolved to two"]
#[doc =
r" different functions at link time, and one function cannot possibly"]
#[doc =
r" have two types, a clashing extern declaration is almost certainly a"]
#[doc =
r" mistake. Check to make sure that the `extern` definitions are correct"]
#[doc =
r" and equivalent, and possibly consider unifying them in one location."]
#[doc = r""]
#[doc = r" This lint does not run between crates because a project may have"]
#[doc =
r" dependencies which both rely on the same extern function, but declare"]
#[doc =
r" it in a different (but valid) way. For example, they may both declare"]
#[doc =
r" an opaque type for one or more of the arguments (which would end up"]
#[doc = r" distinct types), or use types that are valid conversions in the"]
#[doc =
r" language the `extern fn` is defined in. In these cases, the compiler"]
#[doc = r" can't say that the clashing declaration is incorrect."]
pub static CLASHING_EXTERN_DECLARATIONS: &::rustc_lint_defs::Lint =
&::rustc_lint_defs::Lint {
name: "CLASHING_EXTERN_DECLARATIONS",
default_level: ::rustc_lint_defs::Warn,
desc: "detects when an extern fn has been declared with the same name but different types",
is_externally_loaded: false,
..::rustc_lint_defs::Lint::default_fields_for_macro()
};declare_lint! {
33/// The `clashing_extern_declarations` lint detects when an `extern fn`
34 /// has been declared with the same name but different types.
35 ///
36 /// ### Example
37 ///
38 /// ```rust
39 /// mod m {
40 /// unsafe extern "C" {
41 /// fn foo();
42 /// }
43 /// }
44 ///
45 /// unsafe extern "C" {
46 /// fn foo(_: u32);
47 /// }
48 /// ```
49 ///
50 /// {{produces}}
51 ///
52 /// ### Explanation
53 ///
54 /// Because two symbols of the same name cannot be resolved to two
55 /// different functions at link time, and one function cannot possibly
56 /// have two types, a clashing extern declaration is almost certainly a
57 /// mistake. Check to make sure that the `extern` definitions are correct
58 /// and equivalent, and possibly consider unifying them in one location.
59 ///
60 /// This lint does not run between crates because a project may have
61 /// dependencies which both rely on the same extern function, but declare
62 /// it in a different (but valid) way. For example, they may both declare
63 /// an opaque type for one or more of the arguments (which would end up
64 /// distinct types), or use types that are valid conversions in the
65 /// language the `extern fn` is defined in. In these cases, the compiler
66 /// can't say that the clashing declaration is incorrect.
67pub CLASHING_EXTERN_DECLARATIONS,
68 Warn,
69"detects when an extern fn has been declared with the same name but different types"
70}7172struct ClashingExternDeclarations {
73/// Map of function symbol name to the first-seen hir id for that symbol name.. If seen_decls
74 /// contains an entry for key K, it means a symbol with name K has been seen by this lint and
75 /// the symbol should be reported as a clashing declaration.
76// FIXME: Technically, we could just store a &'tcx str here without issue; however, the
77 // `impl_lint_pass` macro doesn't currently support lints parametric over a lifetime.
78seen_decls: UnordMap<Symbol, hir::OwnerId>,
79}
8081/// Differentiate between whether the name for an extern decl came from the link_name attribute or
82/// just from declaration itself. This is important because we don't want to report clashes on
83/// symbol name if they don't actually clash because one or the other links against a symbol with a
84/// different name.
85enum SymbolName {
86/// The name of the symbol + the span of the annotation which introduced the link name.
87Link(Symbol, Span),
88/// No link name, so just the name of the symbol.
89Normal(Symbol),
90}
9192impl SymbolName {
93fn get_name(&self) -> Symbol {
94match self {
95 SymbolName::Link(s, _) | SymbolName::Normal(s) => *s,
96 }
97 }
98}
99100impl ClashingExternDeclarations {
101pub(crate) fn new() -> Self {
102ClashingExternDeclarations { seen_decls: Default::default() }
103 }
104105/// Insert a new foreign item into the seen set. If a symbol with the same name already exists
106 /// for the item, return its HirId without updating the set.
107fn insert(&mut self, tcx: TyCtxt<'_>, fi: hir::ForeignItemId) -> Option<hir::OwnerId> {
108let did = fi.owner_id.to_def_id();
109let instance = Instance::new_raw(did, ty::List::identity_for_item(tcx, did));
110let name = Symbol::intern(tcx.symbol_name(instance).name);
111if let Some(&existing_id) = self.seen_decls.get(&name) {
112// Avoid updating the map with the new entry when we do find a collision. We want to
113 // make sure we're always pointing to the first definition as the previous declaration.
114 // This lets us avoid emitting "knock-on" diagnostics.
115Some(existing_id)
116 } else {
117self.seen_decls.insert(name, fi.owner_id)
118 }
119 }
120121#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("check_foreign_item",
"rustc_lint::foreign_modules", ::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("compiler/rustc_lint/src/foreign_modules.rs"),
::tracing_core::__macro_support::Option::Some(121u32),
::tracing_core::__macro_support::Option::Some("rustc_lint::foreign_modules"),
::tracing_core::field::FieldSet::new(&["this_fi"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::TRACE <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&this_fi)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: () = loop {};
return __tracing_attr_fake_return;
}
{
let DefKind::Fn = tcx.def_kind(this_fi.owner_id) else { return };
let Some(existing_did) =
self.insert(tcx, this_fi) else { return };
let existing_decl_ty = tcx.type_of(existing_did).skip_binder();
let this_decl_ty =
tcx.type_of(this_fi.owner_id).instantiate_identity();
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_lint/src/foreign_modules.rs:128",
"rustc_lint::foreign_modules", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_lint/src/foreign_modules.rs"),
::tracing_core::__macro_support::Option::Some(128u32),
::tracing_core::__macro_support::Option::Some("rustc_lint::foreign_modules"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("ClashingExternDeclarations: Comparing existing {0:?}: {1:?} to this {2:?}: {3:?}",
existing_did, existing_decl_ty, this_fi.owner_id,
this_decl_ty) as &dyn Value))])
});
} else { ; }
};
if !structurally_same_type(tcx,
ty::TypingEnv::non_body_analysis(tcx, this_fi.owner_id),
existing_decl_ty, this_decl_ty) {
let orig = name_of_extern_decl(tcx, existing_did);
let this = tcx.item_name(this_fi.owner_id.to_def_id());
let orig = orig.get_name();
let previous_decl_label =
get_relevant_span(tcx, existing_did);
let mismatch_label = get_relevant_span(tcx, this_fi.owner_id);
let sub =
BuiltinClashingExternSub {
tcx,
expected: existing_decl_ty,
found: this_decl_ty,
};
let decorator =
if orig == this {
BuiltinClashingExtern::SameName {
this,
orig,
previous_decl_label,
mismatch_label,
sub,
}
} else {
BuiltinClashingExtern::DiffName {
this,
orig,
previous_decl_label,
mismatch_label,
sub,
}
};
tcx.emit_node_span_lint(CLASHING_EXTERN_DECLARATIONS,
this_fi.hir_id(), mismatch_label, decorator);
}
}
}
}#[instrument(level = "trace", skip(self, tcx))]122fn check_foreign_item<'tcx>(&mut self, tcx: TyCtxt<'tcx>, this_fi: hir::ForeignItemId) {
123let DefKind::Fn = tcx.def_kind(this_fi.owner_id) else { return };
124let Some(existing_did) = self.insert(tcx, this_fi) else { return };
125126let existing_decl_ty = tcx.type_of(existing_did).skip_binder();
127let this_decl_ty = tcx.type_of(this_fi.owner_id).instantiate_identity();
128debug!(
129"ClashingExternDeclarations: Comparing existing {:?}: {:?} to this {:?}: {:?}",
130 existing_did, existing_decl_ty, this_fi.owner_id, this_decl_ty
131 );
132133// Check that the declarations match.
134if !structurally_same_type(
135 tcx,
136 ty::TypingEnv::non_body_analysis(tcx, this_fi.owner_id),
137 existing_decl_ty,
138 this_decl_ty,
139 ) {
140let orig = name_of_extern_decl(tcx, existing_did);
141142// Finally, emit the diagnostic.
143let this = tcx.item_name(this_fi.owner_id.to_def_id());
144let orig = orig.get_name();
145let previous_decl_label = get_relevant_span(tcx, existing_did);
146let mismatch_label = get_relevant_span(tcx, this_fi.owner_id);
147let sub =
148 BuiltinClashingExternSub { tcx, expected: existing_decl_ty, found: this_decl_ty };
149let decorator = if orig == this {
150 BuiltinClashingExtern::SameName {
151 this,
152 orig,
153 previous_decl_label,
154 mismatch_label,
155 sub,
156 }
157 } else {
158 BuiltinClashingExtern::DiffName {
159 this,
160 orig,
161 previous_decl_label,
162 mismatch_label,
163 sub,
164 }
165 };
166 tcx.emit_node_span_lint(
167 CLASHING_EXTERN_DECLARATIONS,
168 this_fi.hir_id(),
169 mismatch_label,
170 decorator,
171 );
172 }
173 }
174}
175176/// Get the name of the symbol that's linked against for a given extern declaration. That is,
177/// the name specified in a #[link_name = ...] attribute if one was specified, else, just the
178/// symbol's name.
179fn name_of_extern_decl(tcx: TyCtxt<'_>, fi: hir::OwnerId) -> SymbolName {
180if let Some((overridden_link_name, overridden_link_name_span)) =
181tcx.codegen_fn_attrs(fi).symbol_name.map(|overridden_link_name| {
182// FIXME: Instead of searching through the attributes again to get span
183 // information, we could have codegen_fn_attrs also give span information back for
184 // where the attribute was defined. However, until this is found to be a
185 // bottleneck, this does just fine.
186(
187overridden_link_name,
188{
'done:
{
for i in tcx.get_all_attrs(fi) {
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(AttributeKind::LinkName { span,
.. }) => {
break 'done Some(*span);
}
_ => {}
}
}
None
}
}find_attr!(tcx.get_all_attrs(fi), AttributeKind::LinkName {span, ..} => *span)189 .unwrap(),
190 )
191 })
192 {
193 SymbolName::Link(overridden_link_name, overridden_link_name_span)
194 } else {
195 SymbolName::Normal(tcx.item_name(fi.to_def_id()))
196 }
197}
198199/// We want to ensure that we use spans for both decls that include where the
200/// name was defined, whether that was from the link_name attribute or not.
201fn get_relevant_span(tcx: TyCtxt<'_>, fi: hir::OwnerId) -> Span {
202match name_of_extern_decl(tcx, fi) {
203 SymbolName::Normal(_) => tcx.def_span(fi),
204 SymbolName::Link(_, annot_span) => annot_span,
205 }
206}
207208/// Checks whether two types are structurally the same enough that the declarations shouldn't
209/// clash. We need this so we don't emit a lint when two modules both declare an extern struct,
210/// with the same members (as the declarations shouldn't clash).
211fn structurally_same_type<'tcx>(
212 tcx: TyCtxt<'tcx>,
213 typing_env: ty::TypingEnv<'tcx>,
214 a: Ty<'tcx>,
215 b: Ty<'tcx>,
216) -> bool {
217let mut seen_types = UnordSet::default();
218let result = structurally_same_type_impl(&mut seen_types, tcx, typing_env, a, b);
219if truecfg!(debug_assertions) && result {
220// Sanity-check: must have same ABI, size and alignment.
221 // `extern` blocks cannot be generic, so we'll always get a layout here.
222let a_layout = tcx.layout_of(typing_env.as_query_input(a)).unwrap();
223let b_layout = tcx.layout_of(typing_env.as_query_input(b)).unwrap();
224match (&a_layout.backend_repr, &b_layout.backend_repr) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::None);
}
}
};assert_eq!(a_layout.backend_repr, b_layout.backend_repr);
225match (&a_layout.size, &b_layout.size) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::None);
}
}
};assert_eq!(a_layout.size, b_layout.size);
226match (&a_layout.align, &b_layout.align) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::None);
}
}
};assert_eq!(a_layout.align, b_layout.align);
227 }
228result229}
230231fn structurally_same_type_impl<'tcx>(
232 seen_types: &mut UnordSet<(Ty<'tcx>, Ty<'tcx>)>,
233 tcx: TyCtxt<'tcx>,
234 typing_env: ty::TypingEnv<'tcx>,
235 a: Ty<'tcx>,
236 b: Ty<'tcx>,
237) -> bool {
238{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_lint/src/foreign_modules.rs:238",
"rustc_lint::foreign_modules", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_lint/src/foreign_modules.rs"),
::tracing_core::__macro_support::Option::Some(238u32),
::tracing_core::__macro_support::Option::Some("rustc_lint::foreign_modules"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("structurally_same_type_impl(tcx, a = {0:?}, b = {1:?})",
a, b) as &dyn Value))])
});
} else { ; }
};debug!("structurally_same_type_impl(tcx, a = {:?}, b = {:?})", a, b);
239240// Given a transparent newtype, reach through and grab the inner
241 // type unless the newtype makes the type non-null.
242let non_transparent_ty = |mut ty: Ty<'tcx>| -> Ty<'tcx> {
243loop {
244if let ty::Adt(def, args) = *ty.kind() {
245let is_transparent = def.repr().transparent();
246let is_non_null = types::nonnull_optimization_guaranteed(tcx, def);
247{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_lint/src/foreign_modules.rs:247",
"rustc_lint::foreign_modules", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_lint/src/foreign_modules.rs"),
::tracing_core::__macro_support::Option::Some(247u32),
::tracing_core::__macro_support::Option::Some("rustc_lint::foreign_modules"),
::tracing_core::field::FieldSet::new(&["ty",
"is_transparent", "is_non_null"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&ty) as
&dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&is_transparent as
&dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&is_non_null as
&dyn Value))])
});
} else { ; }
};debug!(?ty, is_transparent, is_non_null);
248if is_transparent && !is_non_null {
249if true {
match (&def.variants().len(), &1) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
};debug_assert_eq!(def.variants().len(), 1);
250let v = &def.variant(FIRST_VARIANT);
251// continue with `ty`'s non-ZST field,
252 // otherwise `ty` is a ZST and we can return
253if let Some(field) = types::transparent_newtype_field(tcx, v) {
254ty = field.ty(tcx, args);
255continue;
256 }
257 }
258 }
259{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_lint/src/foreign_modules.rs:259",
"rustc_lint::foreign_modules", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_lint/src/foreign_modules.rs"),
::tracing_core::__macro_support::Option::Some(259u32),
::tracing_core::__macro_support::Option::Some("rustc_lint::foreign_modules"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("non_transparent_ty -> {0:?}",
ty) as &dyn Value))])
});
} else { ; }
};debug!("non_transparent_ty -> {:?}", ty);
260return ty;
261 }
262 };
263264let a = non_transparent_ty(a);
265let b = non_transparent_ty(b);
266267if !seen_types.insert((a, b)) {
268// We've encountered a cycle. There's no point going any further -- the types are
269 // structurally the same.
270true
271} else if a == b {
272// All nominally-same types are structurally same, too.
273true
274} else {
275// Do a full, depth-first comparison between the two.
276let is_primitive_or_pointer =
277 |ty: Ty<'tcx>| ty.is_primitive() || #[allow(non_exhaustive_omitted_patterns)] match ty.kind() {
ty::RawPtr(..) | ty::Ref(..) => true,
_ => false,
}matches!(ty.kind(), ty::RawPtr(..) | ty::Ref(..));
278279ensure_sufficient_stack(|| {
280match (a.kind(), b.kind()) {
281 (&ty::Adt(a_def, a_gen_args), &ty::Adt(b_def, b_gen_args)) => {
282// Only `repr(C)` types can be compared structurally.
283if !(a_def.repr().c() && b_def.repr().c()) {
284return false;
285 }
286// If the types differ in their packed-ness, align, or simd-ness they conflict.
287let repr_characteristica =
288 |def: AdtDef<'tcx>| (def.repr().pack, def.repr().align, def.repr().simd());
289if repr_characteristica(a_def) != repr_characteristica(b_def) {
290return false;
291 }
292293// Grab a flattened representation of all fields.
294let a_fields = a_def.variants().iter().flat_map(|v| v.fields.iter());
295let b_fields = b_def.variants().iter().flat_map(|v| v.fields.iter());
296297// Perform a structural comparison for each field.
298a_fields.eq_by(
299b_fields,
300 |&ty::FieldDef { did: a_did, .. }, &ty::FieldDef { did: b_did, .. }| {
301structurally_same_type_impl(
302seen_types,
303tcx,
304typing_env,
305tcx.type_of(a_did).instantiate(tcx, a_gen_args),
306tcx.type_of(b_did).instantiate(tcx, b_gen_args),
307 )
308 },
309 )
310 }
311 (ty::Array(a_ty, a_len), ty::Array(b_ty, b_len)) => {
312// For arrays, we also check the length.
313a_len == b_len314 && structurally_same_type_impl(seen_types, tcx, typing_env, *a_ty, *b_ty)
315 }
316 (ty::Slice(a_ty), ty::Slice(b_ty)) => {
317structurally_same_type_impl(seen_types, tcx, typing_env, *a_ty, *b_ty)
318 }
319 (ty::RawPtr(a_ty, a_mutbl), ty::RawPtr(b_ty, b_mutbl)) => {
320a_mutbl == b_mutbl321 && structurally_same_type_impl(seen_types, tcx, typing_env, *a_ty, *b_ty)
322 }
323 (ty::Ref(_a_region, a_ty, a_mut), ty::Ref(_b_region, b_ty, b_mut)) => {
324// For structural sameness, we don't need the region to be same.
325a_mut == b_mut326 && structurally_same_type_impl(seen_types, tcx, typing_env, *a_ty, *b_ty)
327 }
328 (ty::FnDef(..), ty::FnDef(..)) => {
329let a_poly_sig = a.fn_sig(tcx);
330let b_poly_sig = b.fn_sig(tcx);
331332// We don't compare regions, but leaving bound regions around ICEs, so
333 // we erase them.
334let a_sig = tcx.instantiate_bound_regions_with_erased(a_poly_sig);
335let b_sig = tcx.instantiate_bound_regions_with_erased(b_poly_sig);
336337 (a_sig.abi, a_sig.safety, a_sig.c_variadic)
338 == (b_sig.abi, b_sig.safety, b_sig.c_variadic)
339 && a_sig.inputs().iter().eq_by(b_sig.inputs().iter(), |a, b| {
340structurally_same_type_impl(seen_types, tcx, typing_env, *a, *b)
341 })
342 && structurally_same_type_impl(
343seen_types,
344tcx,
345typing_env,
346a_sig.output(),
347b_sig.output(),
348 )
349 }
350 (ty::Tuple(..), ty::Tuple(..)) => {
351// Tuples are not `repr(C)` so these cannot be compared structurally.
352false
353}
354// For these, it's not quite as easy to define structural-sameness quite so easily.
355 // For the purposes of this lint, take the conservative approach and mark them as
356 // not structurally same.
357(ty::Dynamic(..), ty::Dynamic(..))
358 | (ty::Error(..), ty::Error(..))
359 | (ty::Closure(..), ty::Closure(..))
360 | (ty::Coroutine(..), ty::Coroutine(..))
361 | (ty::CoroutineWitness(..), ty::CoroutineWitness(..))
362 | (ty::Alias(ty::Projection, ..), ty::Alias(ty::Projection, ..))
363 | (ty::Alias(ty::Inherent, ..), ty::Alias(ty::Inherent, ..))
364 | (ty::Alias(ty::Opaque, ..), ty::Alias(ty::Opaque, ..)) => false,
365366// These definitely should have been caught above.
367(ty::Bool, ty::Bool)
368 | (ty::Char, ty::Char)
369 | (ty::Never, ty::Never)
370 | (ty::Str, ty::Str) => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
371372// An Adt and a primitive or pointer type. This can be FFI-safe if non-null
373 // enum layout optimisation is being applied.
374(ty::Adt(..) | ty::Pat(..), _) if is_primitive_or_pointer(b) => {
375if let Some(a_inner) = types::repr_nullable_ptr(tcx, typing_env, a) {
376a_inner == b377 } else {
378false
379}
380 }
381 (_, ty::Adt(..) | ty::Pat(..)) if is_primitive_or_pointer(a) => {
382if let Some(b_inner) = types::repr_nullable_ptr(tcx, typing_env, b) {
383b_inner == a384 } else {
385false
386}
387 }
388389_ => false,
390 }
391 })
392 }
393}