Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use tcx.symbol_name when determining clashing extern declarations. #80009

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -2595,7 +2596,12 @@ declare_lint! {
}

pub struct ClashingExternDeclarations {
seen_decls: FxHashMap<Symbol, HirId>,
/// 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<String, HirId>,
Copy link
Member

@nagisa nagisa Dec 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory this could just store &'tcx str without an issue, but the impl_lint_pass! macro does not support lints parametric over a lifetime T_T.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could just store &'tcx str without an issue

Yeah, you're right -- that would let me avoid the allocation completely.

the impl_lint_pass! macro does not support lints parametric over a lifetime T_T

I don't think I'll address this in this PR -- I've just added a FIXME note for now, until impl_lint_pass! can take lifetimes.

}

/// Differentiate between whether the name for an extern decl came from the link_name attribute or
Expand Down Expand Up @@ -2626,16 +2632,17 @@ impl ClashingExternDeclarations {
fn insert(&mut self, tcx: TyCtxt<'_>, fi: &hir::ForeignItem<'_>) -> Option<HirId> {
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;
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)
}
}

Expand Down
21 changes: 21 additions & 0 deletions src/test/ui/lint/clashing-extern-fn-wasm.rs
Original file line number Diff line number Diff line change
@@ -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.
}
}
}