From 6318db1c5a06a08a6b4063b25a63df3c328a5bd7 Mon Sep 17 00:00:00 2001 From: jumbatm <30644300+jumbatm@users.noreply.github.com> Date: Sat, 12 Dec 2020 13:26:43 +1000 Subject: [PATCH 1/4] Add test case for wasm non-clash. --- src/test/ui/lint/clashing-extern-fn-wasm.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/test/ui/lint/clashing-extern-fn-wasm.rs diff --git a/src/test/ui/lint/clashing-extern-fn-wasm.rs b/src/test/ui/lint/clashing-extern-fn-wasm.rs new file mode 100644 index 0000000000000..eeb2b8eae2566 --- /dev/null +++ b/src/test/ui/lint/clashing-extern-fn-wasm.rs @@ -0,0 +1,21 @@ +// check-pass +#![crate_type = "lib"] + +#[cfg(target_arch = "wasm32")] +mod wasm_non_clash { + mod a { + #[link(wasm_import_module = "a")] + extern "C" { + pub fn foo(); + } + } + + mod b { + #[link(wasm_import_module = "b")] + extern "C" { + pub fn foo() -> usize; + // #79581: These declarations shouldn't clash because foreign fn names are mangled + // on wasm32. + } + } +} From 15c64a181b93584572f568a4c135a0fdb2870eb6 Mon Sep 17 00:00:00 2001 From: jumbatm <30644300+jumbatm@users.noreply.github.com> Date: Sun, 13 Dec 2020 23:02:32 +1000 Subject: [PATCH 2/4] Use tcx.symbol_name to check for clashes. --- compiler/rustc_lint/src/builtin.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index 374bd6d0d791f..0ef67a1355605 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -43,6 +43,7 @@ use rustc_index::vec::Idx; use rustc_middle::lint::LintDiagnosticBuilder; use rustc_middle::ty::print::with_no_trimmed_paths; use rustc_middle::ty::subst::{GenericArgKind, Subst}; +use rustc_middle::ty::Instance; use rustc_middle::ty::{self, layout::LayoutError, Ty, TyCtxt}; use rustc_session::Session; use rustc_span::edition::Edition; @@ -2595,7 +2596,7 @@ declare_lint! { } pub struct ClashingExternDeclarations { - seen_decls: FxHashMap, + seen_decls: FxHashMap, } /// Differentiate between whether the name for an extern decl came from the link_name attribute or @@ -2626,16 +2627,17 @@ impl ClashingExternDeclarations { fn insert(&mut self, tcx: TyCtxt<'_>, fi: &hir::ForeignItem<'_>) -> Option { let hid = fi.hir_id; - let name = - &tcx.codegen_fn_attrs(tcx.hir().local_def_id(hid)).link_name.unwrap_or(fi.ident.name); - - if self.seen_decls.contains_key(name) { + let local_did = tcx.hir().local_def_id(fi.hir_id); + let did = local_did.to_def_id(); + let instance = Instance::new(did, ty::List::identity_for_item(tcx, did)); + let name = tcx.symbol_name(instance).name.to_string(); + if self.seen_decls.contains_key(&name) { // Avoid updating the map with the new entry when we do find a collision. We want to // make sure we're always pointing to the first definition as the previous declaration. // This lets us avoid emitting "knock-on" diagnostics. - Some(*self.seen_decls.get(name).unwrap()) + Some(*self.seen_decls.get(&name).unwrap()) } else { - self.seen_decls.insert(*name, hid) + self.seen_decls.insert(name, hid) } } From 7a46a4f2197c6ff1026d2bd81480165ad0b43bbc Mon Sep 17 00:00:00 2001 From: jumbatm <30644300+jumbatm@users.noreply.github.com> Date: Wed, 23 Dec 2020 00:26:04 +1000 Subject: [PATCH 3/4] Remove unnecessary allocation. --- compiler/rustc_lint/src/builtin.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index 0ef67a1355605..131d783ce4e2c 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -2630,14 +2630,14 @@ impl ClashingExternDeclarations { let local_did = tcx.hir().local_def_id(fi.hir_id); let did = local_did.to_def_id(); let instance = Instance::new(did, ty::List::identity_for_item(tcx, did)); - let name = tcx.symbol_name(instance).name.to_string(); - if self.seen_decls.contains_key(&name) { + let name = tcx.symbol_name(instance).name; + if let Some(&hir_id) = self.seen_decls.get(name) { // Avoid updating the map with the new entry when we do find a collision. We want to // make sure we're always pointing to the first definition as the previous declaration. // This lets us avoid emitting "knock-on" diagnostics. - Some(*self.seen_decls.get(&name).unwrap()) + Some(hir_id) } else { - self.seen_decls.insert(name, hid) + self.seen_decls.insert(name.to_owned(), hid) } } From 4b740acea270464758a3c81ea4341d858064ee28 Mon Sep 17 00:00:00 2001 From: jumbatm <30644300+jumbatm@users.noreply.github.com> Date: Wed, 23 Dec 2020 00:34:26 +1000 Subject: [PATCH 4/4] Add FIXME note about storing &'tcx str --- compiler/rustc_lint/src/builtin.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index 131d783ce4e2c..20e476b38ccac 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -2596,6 +2596,11 @@ declare_lint! { } pub struct ClashingExternDeclarations { + /// Map of function symbol name to the first-seen hir id for that symbol name.. If seen_decls + /// contains an entry for key K, it means a symbol with name K has been seen by this lint and + /// the symbol should be reported as a clashing declaration. + // FIXME: Technically, we could just store a &'tcx str here without issue; however, the + // `impl_lint_pass` macro doesn't currently support lints parametric over a lifetime. seen_decls: FxHashMap, }