-
-
Notifications
You must be signed in to change notification settings - Fork 14.1k
Fix: Prevent macro-expanded extern crates from shadowing extern arguments #149860
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
base: main
Are you sure you want to change the base?
Conversation
|
rustbot has assigned @WaffleLapkin. Use |
|
r? @Kivooeo |
|
Have you ran |
This comment has been minimized.
This comment has been minimized.
|
Wait checking 😔 |
|
This needs a check from @petrochenkov, who is author of this code, I don't know if Vadim have time to take a look or not, but I assign this to him, so this won't be lost The current problem of this approach from what I see is that after emitting https://github.com/rust-lang/rust/blob/main/tests/ui/imports/issue-109148.rs It will not emit this two " r? petrochenkov |
|
Drafting to avoid stale review |
|
I think #149855 was actually closer to the right solution than this PR, this PR is just hiding the root issue. The root issue is that I guess the most surgical solution would looks something like diff --git i/compiler/rustc_resolve/src/ident.rs w/compiler/rustc_resolve/src/ident.rs
index e38d4370d5d..342c1192090 100644
--- i/compiler/rustc_resolve/src/ident.rs
+++ w/compiler/rustc_resolve/src/ident.rs
@@ -771,36 +771,38 @@ fn maybe_push_ambiguity(
} else {
None
};
- // Skip ambiguity errors for extern flag bindings "overridden"
- // by extern item bindings.
- // FIXME: Remove with lang team approval.
- let issue_145575_hack = Some(binding) == extern_prelude_flag_binding
- && extern_prelude_item_binding.is_some()
- && extern_prelude_item_binding != Some(innermost_binding);
- if let Some(kind) = ambiguity_error_kind
- && !issue_145575_hack
- {
- let misc = |f: Flags| {
- if f.contains(Flags::MISC_SUGGEST_CRATE) {
- AmbiguityErrorMisc::SuggestCrate
- } else if f.contains(Flags::MISC_SUGGEST_SELF) {
- AmbiguityErrorMisc::SuggestSelf
- } else if f.contains(Flags::MISC_FROM_PRELUDE) {
- AmbiguityErrorMisc::FromPrelude
- } else {
- AmbiguityErrorMisc::None
- }
- };
- self.ambiguity_errors.push(AmbiguityError {
- kind,
- ident: orig_ident,
- b1: innermost_binding,
- b2: binding,
- warning: false,
- misc1: misc(innermost_flags),
- misc2: misc(flags),
- });
- return true;
+ if let Some(kind) = ambiguity_error_kind {
+ // Skip ambiguity errors for extern flag bindings "overridden"
+ // by extern item bindings.
+ // FIXME: Remove with lang team approval.
+ let issue_145575_hack = Some(binding) == extern_prelude_flag_binding
+ && extern_prelude_item_binding.is_some()
+ && extern_prelude_item_binding != Some(innermost_binding);
+ if issue_145575_hack {
+ self.issue_145575_hack_applied = true;
+ } else {
+ let misc = |f: Flags| {
+ if f.contains(Flags::MISC_SUGGEST_CRATE) {
+ AmbiguityErrorMisc::SuggestCrate
+ } else if f.contains(Flags::MISC_SUGGEST_SELF) {
+ AmbiguityErrorMisc::SuggestSelf
+ } else if f.contains(Flags::MISC_FROM_PRELUDE) {
+ AmbiguityErrorMisc::FromPrelude
+ } else {
+ AmbiguityErrorMisc::None
+ }
+ };
+ self.ambiguity_errors.push(AmbiguityError {
+ kind,
+ ident: orig_ident,
+ b1: innermost_binding,
+ b2: binding,
+ warning: false,
+ misc1: misc(innermost_flags),
+ misc2: misc(flags),
+ });
+ return true;
+ }
}
false
diff --git i/compiler/rustc_resolve/src/imports.rs w/compiler/rustc_resolve/src/imports.rs
index 4ef87af5605..4e0f3db5982 100644
--- i/compiler/rustc_resolve/src/imports.rs
+++ w/compiler/rustc_resolve/src/imports.rs
@@ -1170,7 +1170,7 @@ fn finalize_import(&mut self, import: Import<'ra>) -> Option<UnresolvedImportErr
return;
}
if let Some(initial_res) = initial_res {
- if res != initial_res {
+ if res != initial_res && !this.issue_145575_hack_applied {
span_bug!(import.span, "inconsistent resolution for an import");
}
} else if this.privacy_errors.is_empty() {
diff --git i/compiler/rustc_resolve/src/lib.rs w/compiler/rustc_resolve/src/lib.rs
index 8132bf577d8..7e06b8511e3 100644
--- i/compiler/rustc_resolve/src/lib.rs
+++ w/compiler/rustc_resolve/src/lib.rs
@@ -1186,6 +1186,7 @@ pub struct Resolver<'ra, 'tcx> {
privacy_errors: Vec<PrivacyError<'ra>> = Vec::new(),
/// Ambiguity errors are delayed for deduplication.
ambiguity_errors: Vec<AmbiguityError<'ra>> = Vec::new(),
+ issue_145575_hack_applied: bool = false,
/// `use` injections are delayed for better placement and deduplication.
use_injections: Vec<UseError<'tcx>> = Vec::new(),
/// Crate-local macro expanded `macro_export` referred to by a module-relative path. |
41775d4 to
04aec44
Compare
|
@petrochenkov I have applied the surgical patch you suggested. Previously, I was attempting a fix using a dummy_binding, which similarly resolved the crash but had slightly different side effects on diagnostics. Switching to your flag-based approach, I verified that it explicitly suppresses ambiguity errors when the hack is active. Consequently, I have updated (blessed) tests/ui/imports/issue-109148.rs and tests/ui/macros/issue-78325-inconsistent-resolution.rs to match this intended behavior, as they were failing because they expected the ambiguity errors that are now suppressed. CI should be green now. Ready for review! |
This comment has been minimized.
This comment has been minimized.
My bad here then, it's been a lot of effortless PRs lately from people who trying to get a contribute just for resume line or something, it's hard to differ them from ones actual trying to understand, learn code and help to the project, especially when PR description is five times bigger than actual fix, and when you try to ask them about why they think this is correct approach of why they used this instead of something different here, it's likely will lead to dialogue with AI I also have taken a look more closely to all of this There is comment saying rust/compiler/rustc_resolve/src/imports.rs Line 1147 in f520900
rust/compiler/rustc_resolve/src/imports.rs Lines 1172 to 1175 in f520900
I took a look at rust/compiler/rustc_resolve/src/macros.rs Lines 840 to 846 in f520900
@Delta17920, Sorry for previous closing, seeing you not trying to elaborate on your solution is made me feel so |
|
@Kivooeo Thanks for the follow-up! No worries at all about the earlier confusion I totally understand and really appreciate you taking a second look. 🤝 Regarding the fix, I went with @petrochenkov's "surgical" suggestion (adding a specific flag) to keep the suppression tightly scoped to this hack. |
|
@Delta17920: 🔑 Insufficient privileges: not in try users |
|
reopen PR to restart CI :D this sounds fun but real, just close and open back this RP |
|
@Kivooeo thnx man |
|
sorry, mb |
This comment has been minimized.
This comment has been minimized.
|
@petrochenkov It looks like the error suppression also affects some Clippy tests (e.g., tests/ui/crashes/ice-6255.rs) which were expecting the ambiguity error. |
04aec44 to
aad4fc9
Compare
|
Some changes occurred in src/tools/clippy cc @rust-lang/clippy |
prevents an ICE by fixing a logic bug in
build_reduced_graph.rs.the bug caused the compiler to correctly detect and report a shadowing error for a macro-expanded
extern cratebut then continue processing the invalid item, corrupting the resolver's internal state (extern_prelude) and leading to a crash in later resolution passes the fix adds an early return after the shadowing error is reported to ensure the invalid item is not added to the resolution graph.Fixes #149821