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::def::DefKind;
6use rustc_hir::find_attr;
7use rustc_middle::query::Providers;
8use rustc_middle::ty::{self, AdtDef, Instance, Ty, TyCtxt};
9use rustc_session::declare_lint;
10use rustc_span::{Span, Symbol};
11use tracing::{debug, instrument};
1213use crate::lints::{BuiltinClashingExtern, BuiltinClashingExternSub};
14use crate::{LintVec, types};
1516pub(crate) fn provide(providers: &mut Providers) {
17*providers = Providers { clashing_extern_declarations, ..*providers };
18}
1920pub(crate) fn get_lints() -> LintVec {
21::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[CLASHING_EXTERN_DECLARATIONS]))vec![CLASHING_EXTERN_DECLARATIONS]22}
2324fn clashing_extern_declarations(tcx: TyCtxt<'_>, (): ()) {
25let mut lint = ClashingExternDeclarations::new();
26for id in tcx.hir_crate_items(()).foreign_items() {
27 lint.check_foreign_item(tcx, id);
28 }
29}
3031#[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! {
32/// The `clashing_extern_declarations` lint detects when an `extern fn`
33 /// has been declared with the same name but different types.
34 ///
35 /// ### Example
36 ///
37 /// ```rust
38 /// mod m {
39 /// unsafe extern "C" {
40 /// fn foo();
41 /// }
42 /// }
43 ///
44 /// unsafe extern "C" {
45 /// fn foo(_: u32);
46 /// }
47 /// ```
48 ///
49 /// {{produces}}
50 ///
51 /// ### Explanation
52 ///
53 /// Because two symbols of the same name cannot be resolved to two
54 /// different functions at link time, and one function cannot possibly
55 /// have two types, a clashing extern declaration is almost certainly a
56 /// mistake. Check to make sure that the `extern` definitions are correct
57 /// and equivalent, and possibly consider unifying them in one location.
58 ///
59 /// This lint does not run between crates because a project may have
60 /// dependencies which both rely on the same extern function, but declare
61 /// it in a different (but valid) way. For example, they may both declare
62 /// an opaque type for one or more of the arguments (which would end up
63 /// distinct types), or use types that are valid conversions in the
64 /// language the `extern fn` is defined in. In these cases, the compiler
65 /// can't say that the clashing declaration is incorrect.
66pub CLASHING_EXTERN_DECLARATIONS,
67 Warn,
68"detects when an extern fn has been declared with the same name but different types"
69}7071struct ClashingExternDeclarations {
72/// Map of function symbol name to the first-seen hir id for that symbol name.. If seen_decls
73 /// contains an entry for key K, it means a symbol with name K has been seen by this lint and
74 /// the symbol should be reported as a clashing declaration.
75// FIXME: Technically, we could just store a &'tcx str here without issue; however, the
76 // `impl_lint_pass` macro doesn't currently support lints parametric over a lifetime.
77seen_decls: UnordMap<Symbol, hir::OwnerId>,
78}
7980/// Differentiate between whether the name for an extern decl came from the link_name attribute or
81/// just from declaration itself. This is important because we don't want to report clashes on
82/// symbol name if they don't actually clash because one or the other links against a symbol with a
83/// different name.
84enum SymbolName {
85/// The name of the symbol + the span of the annotation which introduced the link name.
86Link(Symbol, Span),
87/// No link name, so just the name of the symbol.
88Normal(Symbol),
89}
9091impl SymbolName {
92fn get_name(&self) -> Symbol {
93match self {
94 SymbolName::Link(s, _) | SymbolName::Normal(s) => *s,
95 }
96 }
97}
9899impl ClashingExternDeclarations {
100pub(crate) fn new() -> Self {
101ClashingExternDeclarations { seen_decls: Default::default() }
102 }
103104/// Insert a new foreign item into the seen set. If a symbol with the same name already exists
105 /// for the item, return its HirId without updating the set.
106fn insert(&mut self, tcx: TyCtxt<'_>, fi: hir::ForeignItemId) -> Option<hir::OwnerId> {
107let did = fi.owner_id.to_def_id();
108let instance = Instance::new_raw(did, ty::List::identity_for_item(tcx, did));
109let name = Symbol::intern(tcx.symbol_name(instance).name);
110if let Some(&existing_id) = self.seen_decls.get(&name) {
111// Avoid updating the map with the new entry when we do find a collision. We want to
112 // make sure we're always pointing to the first definition as the previous declaration.
113 // This lets us avoid emitting "knock-on" diagnostics.
114Some(existing_id)
115 } else {
116self.seen_decls.insert(name, fi.owner_id)
117 }
118 }
119120#[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(120u32),
::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:127",
"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(127u32),
::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))]121fn check_foreign_item<'tcx>(&mut self, tcx: TyCtxt<'tcx>, this_fi: hir::ForeignItemId) {
122let DefKind::Fn = tcx.def_kind(this_fi.owner_id) else { return };
123let Some(existing_did) = self.insert(tcx, this_fi) else { return };
124125let existing_decl_ty = tcx.type_of(existing_did).skip_binder();
126let this_decl_ty = tcx.type_of(this_fi.owner_id).instantiate_identity();
127debug!(
128"ClashingExternDeclarations: Comparing existing {:?}: {:?} to this {:?}: {:?}",
129 existing_did, existing_decl_ty, this_fi.owner_id, this_decl_ty
130 );
131132// Check that the declarations match.
133if !structurally_same_type(
134 tcx,
135 ty::TypingEnv::non_body_analysis(tcx, this_fi.owner_id),
136 existing_decl_ty,
137 this_decl_ty,
138 ) {
139let orig = name_of_extern_decl(tcx, existing_did);
140141// Finally, emit the diagnostic.
142let this = tcx.item_name(this_fi.owner_id.to_def_id());
143let orig = orig.get_name();
144let previous_decl_label = get_relevant_span(tcx, existing_did);
145let mismatch_label = get_relevant_span(tcx, this_fi.owner_id);
146let sub =
147 BuiltinClashingExternSub { tcx, expected: existing_decl_ty, found: this_decl_ty };
148let decorator = if orig == this {
149 BuiltinClashingExtern::SameName {
150 this,
151 orig,
152 previous_decl_label,
153 mismatch_label,
154 sub,
155 }
156 } else {
157 BuiltinClashingExtern::DiffName {
158 this,
159 orig,
160 previous_decl_label,
161 mismatch_label,
162 sub,
163 }
164 };
165 tcx.emit_node_span_lint(
166 CLASHING_EXTERN_DECLARATIONS,
167 this_fi.hir_id(),
168 mismatch_label,
169 decorator,
170 );
171 }
172 }
173}
174175/// Get the name of the symbol that's linked against for a given extern declaration. That is,
176/// the name specified in a #[link_name = ...] attribute if one was specified, else, just the
177/// symbol's name.
178fn name_of_extern_decl(tcx: TyCtxt<'_>, fi: hir::OwnerId) -> SymbolName {
179if let Some((overridden_link_name, overridden_link_name_span)) =
180tcx.codegen_fn_attrs(fi).symbol_name.map(|overridden_link_name| {
181// FIXME: Instead of searching through the attributes again to get span
182 // information, we could have codegen_fn_attrs also give span information back for
183 // where the attribute was defined. However, until this is found to be a
184 // bottleneck, this does just fine.
185 (overridden_link_name, {
#[allow(deprecated)]
{
{
'done:
{
for i in tcx.get_all_attrs(fi) {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(LinkName { span, .. }) => {
break 'done Some(*span);
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}
}find_attr!(tcx, fi, LinkName {span, ..} => *span).unwrap())
186 })
187 {
188 SymbolName::Link(overridden_link_name, overridden_link_name_span)
189 } else {
190 SymbolName::Normal(tcx.item_name(fi.to_def_id()))
191 }
192}
193194/// We want to ensure that we use spans for both decls that include where the
195/// name was defined, whether that was from the link_name attribute or not.
196fn get_relevant_span(tcx: TyCtxt<'_>, fi: hir::OwnerId) -> Span {
197match name_of_extern_decl(tcx, fi) {
198 SymbolName::Normal(_) => tcx.def_span(fi),
199 SymbolName::Link(_, annot_span) => annot_span,
200 }
201}
202203/// Checks whether two types are structurally the same enough that the declarations shouldn't
204/// clash. We need this so we don't emit a lint when two modules both declare an extern struct,
205/// with the same members (as the declarations shouldn't clash).
206fn structurally_same_type<'tcx>(
207 tcx: TyCtxt<'tcx>,
208 typing_env: ty::TypingEnv<'tcx>,
209 a: Ty<'tcx>,
210 b: Ty<'tcx>,
211) -> bool {
212let mut seen_types = UnordSet::default();
213let result = structurally_same_type_impl(&mut seen_types, tcx, typing_env, a, b);
214if truecfg!(debug_assertions) && result {
215// Sanity-check: must have same ABI, size and alignment.
216 // `extern` blocks cannot be generic, so we'll always get a layout here.
217let a_layout = tcx.layout_of(typing_env.as_query_input(a)).unwrap();
218let b_layout = tcx.layout_of(typing_env.as_query_input(b)).unwrap();
219match (&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);
220match (&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);
221match (&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);
222 }
223result224}
225226fn structurally_same_type_impl<'tcx>(
227 seen_types: &mut UnordSet<(Ty<'tcx>, Ty<'tcx>)>,
228 tcx: TyCtxt<'tcx>,
229 typing_env: ty::TypingEnv<'tcx>,
230 a: Ty<'tcx>,
231 b: Ty<'tcx>,
232) -> bool {
233{
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:233",
"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(233u32),
::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);
234235// Given a transparent newtype, reach through and grab the inner
236 // type unless the newtype makes the type non-null.
237let non_transparent_ty = |mut ty: Ty<'tcx>| -> Ty<'tcx> {
238loop {
239if let ty::Adt(def, args) = *ty.kind() {
240let is_transparent = def.repr().transparent();
241let is_non_null = types::nonnull_optimization_guaranteed(tcx, def);
242{
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:242",
"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(242u32),
::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);
243if is_transparent && !is_non_null {
244if 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);
245let v = &def.variant(FIRST_VARIANT);
246// continue with `ty`'s non-ZST field,
247 // otherwise `ty` is a ZST and we can return
248if let Some(field) = types::transparent_newtype_field(tcx, v) {
249ty = field.ty(tcx, args);
250continue;
251 }
252 }
253 }
254{
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:254",
"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(254u32),
::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);
255return ty;
256 }
257 };
258259let a = non_transparent_ty(a);
260let b = non_transparent_ty(b);
261262if !seen_types.insert((a, b)) {
263// We've encountered a cycle. There's no point going any further -- the types are
264 // structurally the same.
265true
266} else if a == b {
267// All nominally-same types are structurally same, too.
268true
269} else {
270// Do a full, depth-first comparison between the two.
271let is_primitive_or_pointer =
272 |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(..));
273274ensure_sufficient_stack(|| {
275match (a.kind(), b.kind()) {
276 (&ty::Adt(a_def, a_gen_args), &ty::Adt(b_def, b_gen_args)) => {
277// Only `repr(C)` types can be compared structurally.
278if !(a_def.repr().c() && b_def.repr().c()) {
279return false;
280 }
281// If the types differ in their packed-ness, align, or simd-ness they conflict.
282let repr_characteristica =
283 |def: AdtDef<'tcx>| (def.repr().pack, def.repr().align, def.repr().simd());
284if repr_characteristica(a_def) != repr_characteristica(b_def) {
285return false;
286 }
287288// Grab a flattened representation of all fields.
289let a_fields = a_def.variants().iter().flat_map(|v| v.fields.iter());
290let b_fields = b_def.variants().iter().flat_map(|v| v.fields.iter());
291292// Perform a structural comparison for each field.
293a_fields.eq_by(
294b_fields,
295 |&ty::FieldDef { did: a_did, .. }, &ty::FieldDef { did: b_did, .. }| {
296structurally_same_type_impl(
297seen_types,
298tcx,
299typing_env,
300tcx.type_of(a_did).instantiate(tcx, a_gen_args),
301tcx.type_of(b_did).instantiate(tcx, b_gen_args),
302 )
303 },
304 )
305 }
306 (ty::Array(a_ty, a_len), ty::Array(b_ty, b_len)) => {
307// For arrays, we also check the length.
308a_len == b_len309 && structurally_same_type_impl(seen_types, tcx, typing_env, *a_ty, *b_ty)
310 }
311 (ty::Slice(a_ty), ty::Slice(b_ty)) => {
312structurally_same_type_impl(seen_types, tcx, typing_env, *a_ty, *b_ty)
313 }
314 (ty::RawPtr(a_ty, a_mutbl), ty::RawPtr(b_ty, b_mutbl)) => {
315a_mutbl == b_mutbl316 && structurally_same_type_impl(seen_types, tcx, typing_env, *a_ty, *b_ty)
317 }
318 (ty::Ref(_a_region, a_ty, a_mut), ty::Ref(_b_region, b_ty, b_mut)) => {
319// For structural sameness, we don't need the region to be same.
320a_mut == b_mut321 && structurally_same_type_impl(seen_types, tcx, typing_env, *a_ty, *b_ty)
322 }
323 (ty::FnDef(..), ty::FnDef(..)) => {
324let a_poly_sig = a.fn_sig(tcx);
325let b_poly_sig = b.fn_sig(tcx);
326327// We don't compare regions, but leaving bound regions around ICEs, so
328 // we erase them.
329let a_sig = tcx.instantiate_bound_regions_with_erased(a_poly_sig);
330let b_sig = tcx.instantiate_bound_regions_with_erased(b_poly_sig);
331332 (a_sig.abi, a_sig.safety, a_sig.c_variadic)
333 == (b_sig.abi, b_sig.safety, b_sig.c_variadic)
334 && a_sig.inputs().iter().eq_by(b_sig.inputs().iter(), |a, b| {
335structurally_same_type_impl(seen_types, tcx, typing_env, *a, *b)
336 })
337 && structurally_same_type_impl(
338seen_types,
339tcx,
340typing_env,
341a_sig.output(),
342b_sig.output(),
343 )
344 }
345 (ty::Tuple(..), ty::Tuple(..)) => {
346// Tuples are not `repr(C)` so these cannot be compared structurally.
347false
348}
349// For these, it's not quite as easy to define structural-sameness quite so easily.
350 // For the purposes of this lint, take the conservative approach and mark them as
351 // not structurally same.
352(ty::Dynamic(..), ty::Dynamic(..))
353 | (ty::Error(..), ty::Error(..))
354 | (ty::Closure(..), ty::Closure(..))
355 | (ty::Coroutine(..), ty::Coroutine(..))
356 | (ty::CoroutineWitness(..), ty::CoroutineWitness(..))
357 | (ty::Alias(ty::Projection, ..), ty::Alias(ty::Projection, ..))
358 | (ty::Alias(ty::Inherent, ..), ty::Alias(ty::Inherent, ..))
359 | (ty::Alias(ty::Opaque, ..), ty::Alias(ty::Opaque, ..)) => false,
360361// These definitely should have been caught above.
362(ty::Bool, ty::Bool)
363 | (ty::Char, ty::Char)
364 | (ty::Never, ty::Never)
365 | (ty::Str, ty::Str) => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
366367// An Adt and a primitive or pointer type. This can be FFI-safe if non-null
368 // enum layout optimisation is being applied.
369(ty::Adt(..) | ty::Pat(..), _) if is_primitive_or_pointer(b) => {
370if let Some(a_inner) = types::repr_nullable_ptr(tcx, typing_env, a) {
371a_inner == b372 } else {
373false
374}
375 }
376 (_, ty::Adt(..) | ty::Pat(..)) if is_primitive_or_pointer(a) => {
377if let Some(b_inner) = types::repr_nullable_ptr(tcx, typing_env, b) {
378b_inner == a379 } else {
380false
381}
382 }
383384_ => false,
385 }
386 })
387 }
388}