Skip to content

Commit 37f3610

Browse files
authored
Unrolled build for #141934
Rollup merge of #141934 - petrochenkov:privmacuse, r=compiler-errors resolve: Tweak `private_macro_use` lint to be compatible with upcoming macro prelude changes Unblocks #139493. Zulip thread requesting help - [#t-compiler/help > Help requested for effects of #139493](https://rust-lang.zulipchat.com/#narrow/channel/182449-t-compiler.2Fhelp/topic/Help.20requested.20for.20effects.20of.20.23139493/with/514653911). This PR by itself shouldn't cause any observable changes, its only observable effect is that the prelude changes from #139493 will no longer cause regressions in tests like `tests/ui/imports/issue-119369.rs` or `tests/ui/extern/issue-80074.rs`. This is achieved by moving the "is this thing in stdlib prelude" check from an early point (`fn process_macro_use_imports`) to a later point (`fn record_use_inner`), at which the stdlib prelude is already populated and can be inspected. (The `is_builtin_macro` check is subsumed by the stdlib prelude check, all built-in macros go through the stdlib prelude anyway.)
2 parents 573a015 + 6a5bad3 commit 37f3610

File tree

3 files changed

+36
-22
lines changed

3 files changed

+36
-22
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,22 +1098,20 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
10981098
self.r.potentially_unused_imports.push(import);
10991099
module.for_each_child(self, |this, ident, ns, binding| {
11001100
if ns == MacroNS {
1101-
let imported_binding =
1102-
if this.r.is_accessible_from(binding.vis, this.parent_scope.module) {
1103-
this.r.import(binding, import)
1104-
} else if !this.r.is_builtin_macro(binding.res())
1105-
&& !this.r.macro_use_prelude.contains_key(&ident.name)
1106-
{
1107-
// - `!r.is_builtin_macro(res)` excluding the built-in macros such as `Debug` or `Hash`.
1108-
// - `!r.macro_use_prelude.contains_key(name)` excluding macros defined in other extern
1109-
// crates such as `std`.
1110-
// FIXME: This branch should eventually be removed.
1111-
let import = macro_use_import(this, span, true);
1112-
this.r.import(binding, import)
1113-
} else {
1101+
let import = if this.r.is_accessible_from(binding.vis, this.parent_scope.module)
1102+
{
1103+
import
1104+
} else {
1105+
// FIXME: This branch is used for reporting the `private_macro_use` lint
1106+
// and should eventually be removed.
1107+
if this.r.macro_use_prelude.contains_key(&ident.name) {
1108+
// Do not override already existing entries with compatibility entries.
11141109
return;
1115-
};
1116-
this.add_macro_use_binding(ident.name, imported_binding, span, allow_shadowing);
1110+
}
1111+
macro_use_import(this, span, true)
1112+
};
1113+
let import_binding = this.r.import(binding, import);
1114+
this.add_macro_use_binding(ident.name, import_binding, span, allow_shadowing);
11171115
}
11181116
});
11191117
} else {

compiler/rustc_resolve/src/imports.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ impl<'ra> std::fmt::Debug for ImportKind<'ra> {
133133
.field("target", target)
134134
.field("id", id)
135135
.finish(),
136-
MacroUse { .. } => f.debug_struct("MacroUse").finish(),
136+
MacroUse { warn_private } => {
137+
f.debug_struct("MacroUse").field("warn_private", warn_private).finish()
138+
}
137139
MacroExport => f.debug_struct("MacroExport").finish(),
138140
}
139141
}

compiler/rustc_resolve/src/lib.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1934,12 +1934,26 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
19341934
}
19351935
if let NameBindingKind::Import { import, binding } = used_binding.kind {
19361936
if let ImportKind::MacroUse { warn_private: true } = import.kind {
1937-
self.lint_buffer().buffer_lint(
1938-
PRIVATE_MACRO_USE,
1939-
import.root_id,
1940-
ident.span,
1941-
BuiltinLintDiag::MacroIsPrivate(ident),
1942-
);
1937+
// Do not report the lint if the macro name resolves in stdlib prelude
1938+
// even without the problematic `macro_use` import.
1939+
let found_in_stdlib_prelude = self.prelude.is_some_and(|prelude| {
1940+
self.maybe_resolve_ident_in_module(
1941+
ModuleOrUniformRoot::Module(prelude),
1942+
ident,
1943+
MacroNS,
1944+
&ParentScope::module(self.empty_module, self),
1945+
None,
1946+
)
1947+
.is_ok()
1948+
});
1949+
if !found_in_stdlib_prelude {
1950+
self.lint_buffer().buffer_lint(
1951+
PRIVATE_MACRO_USE,
1952+
import.root_id,
1953+
ident.span,
1954+
BuiltinLintDiag::MacroIsPrivate(ident),
1955+
);
1956+
}
19431957
}
19441958
// Avoid marking `extern crate` items that refer to a name from extern prelude,
19451959
// but not introduce it, as used if they are accessed from lexical scope.

0 commit comments

Comments
 (0)