Skip to content

resolve: Tweak private_macro_use lint to be compatible with upcoming macro prelude changes #141934

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
28 changes: 13 additions & 15 deletions compiler/rustc_resolve/src/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1098,22 +1098,20 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
self.r.potentially_unused_imports.push(import);
module.for_each_child(self, |this, ident, ns, binding| {
if ns == MacroNS {
let imported_binding =
if this.r.is_accessible_from(binding.vis, this.parent_scope.module) {
this.r.import(binding, import)
} else if !this.r.is_builtin_macro(binding.res())
&& !this.r.macro_use_prelude.contains_key(&ident.name)
{
// - `!r.is_builtin_macro(res)` excluding the built-in macros such as `Debug` or `Hash`.
// - `!r.macro_use_prelude.contains_key(name)` excluding macros defined in other extern
// crates such as `std`.
// FIXME: This branch should eventually be removed.
let import = macro_use_import(this, span, true);
this.r.import(binding, import)
} else {
let import = if this.r.is_accessible_from(binding.vis, this.parent_scope.module)
{
import
} else {
// FIXME: This branch is used for reporting the `private_macro_use` lint
// and should eventually be removed.
if this.r.macro_use_prelude.contains_key(&ident.name) {
// Do not override already existing entries with compatibility entries.
return;
};
this.add_macro_use_binding(ident.name, imported_binding, span, allow_shadowing);
}
macro_use_import(this, span, true)
};
let import_binding = this.r.import(binding, import);
this.add_macro_use_binding(ident.name, import_binding, span, allow_shadowing);
}
});
} else {
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_resolve/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ impl<'ra> std::fmt::Debug for ImportKind<'ra> {
.field("target", target)
.field("id", id)
.finish(),
MacroUse { .. } => f.debug_struct("MacroUse").finish(),
MacroUse { warn_private } => {
f.debug_struct("MacroUse").field("warn_private", warn_private).finish()
}
MacroExport => f.debug_struct("MacroExport").finish(),
}
}
Expand Down
26 changes: 20 additions & 6 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1934,12 +1934,26 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
}
if let NameBindingKind::Import { import, binding } = used_binding.kind {
if let ImportKind::MacroUse { warn_private: true } = import.kind {
self.lint_buffer().buffer_lint(
PRIVATE_MACRO_USE,
import.root_id,
ident.span,
BuiltinLintDiag::MacroIsPrivate(ident),
);
// Do not report the lint if the macro name resolves in stdlib prelude
// even without the problematic `macro_use` import.
let found_in_stdlib_prelude = self.prelude.is_some_and(|prelude| {
self.maybe_resolve_ident_in_module(
ModuleOrUniformRoot::Module(prelude),
ident,
MacroNS,
&ParentScope::module(self.empty_module, self),
None,
)
.is_ok()
});
if !found_in_stdlib_prelude {
self.lint_buffer().buffer_lint(
PRIVATE_MACRO_USE,
import.root_id,
ident.span,
BuiltinLintDiag::MacroIsPrivate(ident),
);
}
}
// Avoid marking `extern crate` items that refer to a name from extern prelude,
// but not introduce it, as used if they are accessed from lexical scope.
Expand Down
Loading