Skip to content

Commit d2f5ccd

Browse files
yaahcVoultapher
authored andcommitted
Trigger lint more narrowly and tiebreak to prelude
1 parent 448e516 commit d2f5ccd

10 files changed

+86
-62
lines changed

compiler/rustc_resolve/src/ident.rs

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -645,11 +645,20 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
645645
return None;
646646
}
647647

648+
// preemptively look for ambiguities for panic macros to ensure we don't
649+
// speculatively resolve to a non-prelude item when an ambiguity that we'd
650+
// downgrade is present
651+
let is_issue_147319_hack = || ctxt.edition()
652+
<= Edition::Edition2024
653+
&& matches!(orig_ident.name, sym::panic)
654+
&& (this.is_specific_builtin_macro(binding.res(), sym::std_panic) || this.is_specific_builtin_macro(binding.res(), sym::core_panic));
655+
let finalizing = !matches!(finalize, None | Some(Finalize { stage: Stage::Late, .. }));
656+
648657
// Below we report various ambiguity errors.
649658
// We do not need to report them if we are either in speculative resolution,
650659
// or in late resolution when everything is already imported and expanded
651660
// and no ambiguities exist.
652-
if matches!(finalize, None | Some(Finalize { stage: Stage::Late, .. })) {
661+
if !finalizing && !is_issue_147319_hack() {
653662
return Some(Ok(binding));
654663
}
655664

@@ -720,8 +729,20 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
720729
let is_issue_147319_hack = ctxt.edition()
721730
<= Edition::Edition2024
722731
&& matches!(orig_ident.name, sym::panic)
723-
&& this.is_builtin_macro(binding.res())
724-
&& this.is_builtin_macro(innermost_binding.res());
732+
&& matches!(scope, Scope::StdLibPrelude)
733+
&& ((this.is_specific_builtin_macro(
734+
binding.res(),
735+
sym::std_panic,
736+
) && this.is_specific_builtin_macro(
737+
innermost_binding.res(),
738+
sym::core_panic,
739+
)) || (this.is_specific_builtin_macro(
740+
binding.res(),
741+
sym::core_panic,
742+
) && this.is_specific_builtin_macro(
743+
innermost_binding.res(),
744+
sym::std_panic,
745+
)));
725746

726747
let warning = if is_issue_147319_hack {
727748
Some(AmbiguityWarning::PanicImport)
@@ -740,16 +761,22 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
740761
AmbiguityErrorMisc::None
741762
}
742763
};
743-
this.get_mut().ambiguity_errors.push(AmbiguityError {
744-
kind,
745-
ident: orig_ident,
746-
b1: innermost_binding,
747-
b2: binding,
748-
warning,
749-
misc1: misc(innermost_flags),
750-
misc2: misc(flags),
751-
});
752-
return Some(Ok(innermost_binding));
764+
if finalizing {
765+
this.get_mut().ambiguity_errors.push(AmbiguityError {
766+
kind,
767+
ident: orig_ident,
768+
b1: innermost_binding,
769+
b2: binding,
770+
warning,
771+
misc1: misc(innermost_flags),
772+
misc2: misc(flags),
773+
});
774+
}
775+
if is_issue_147319_hack {
776+
return Some(Ok(binding));
777+
} else {
778+
return Some(Ok(innermost_binding));
779+
}
753780
}
754781
}
755782
} else {

compiler/rustc_resolve/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,6 +1857,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18571857
self.get_macro(res).is_some_and(|macro_data| macro_data.ext.builtin_name.is_some())
18581858
}
18591859

1860+
fn is_specific_builtin_macro(&self, res: Res, symbol: Symbol) -> bool {
1861+
self.get_macro(res).is_some_and(|macro_data| macro_data.ext.builtin_name == Some(symbol))
1862+
}
1863+
18601864
fn macro_def(&self, mut ctxt: SyntaxContext) -> DefId {
18611865
loop {
18621866
match ctxt.outer_expn_data().macro_def_id {

tests/ui/imports/ambiguous-panic-glob-vs-multiouter.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//@ edition: 2024
2-
//@ check-pass
32
#![crate_type = "lib"]
43
mod m1 {
54
pub use core::prelude::v1::*;
@@ -14,7 +13,5 @@ use m2::*;
1413
fn foo() {
1514
use m1::*;
1615

17-
panic!();
18-
//~^ WARN: `panic` is ambiguous [ambiguous_panic_imports]
19-
//~| WARN: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
16+
panic!(); //~ ERROR: `panic` is ambiguous [E0659]
2017
}
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
1-
warning: `panic` is ambiguous
2-
--> $DIR/ambiguous-panic-glob-vs-multiouter.rs:20:5
1+
error[E0659]: `panic` is ambiguous
2+
--> $DIR/ambiguous-panic-glob-vs-multiouter.rs:16:5
33
|
44
LL | panic!();
55
| ^^^^^ ambiguous name
66
|
7-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
8-
= note: for more information, see issue #147319 <https://github.com/rust-lang/rust/issues/147319>
97
= note: ambiguous because of a conflict between a name from a glob import and an outer scope during import or macro resolution
108
note: `panic` could refer to the macro imported here
11-
--> $DIR/ambiguous-panic-glob-vs-multiouter.rs:15:9
9+
--> $DIR/ambiguous-panic-glob-vs-multiouter.rs:14:9
1210
|
1311
LL | use m1::*;
1412
| ^^^^^
1513
= help: consider adding an explicit import of `panic` to disambiguate
1614
note: `panic` could also refer to the macro imported here
17-
--> $DIR/ambiguous-panic-glob-vs-multiouter.rs:13:5
15+
--> $DIR/ambiguous-panic-glob-vs-multiouter.rs:12:5
1816
|
1917
LL | use m2::*;
2018
| ^^^^^
2119
= help: use `crate::panic` to refer to this macro unambiguously
22-
= note: `#[warn(ambiguous_panic_imports)]` (part of `#[warn(future_incompatible)]`) on by default
2320

24-
warning: 1 warning emitted
21+
error: aborting due to 1 previous error
2522

23+
For more information about this error, try `rustc --explain E0659`.

tests/ui/imports/ambiguous-panic-pick-core.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//@ edition: 2018
2-
//@ check-pass
32
#![crate_type = "lib"]
43
#![no_std]
54

@@ -10,5 +9,6 @@ fn f() {
109
panic!(std::string::String::new());
1110
//~^ WARN: `panic` is ambiguous [ambiguous_panic_imports]
1211
//~| WARN: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
13-
//~| WARN: panic message is not a string literal [non_fmt_panics]
12+
//~| ERROR: mismatched types [E0308]
13+
1414
}
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
warning: `panic` is ambiguous
2-
--> $DIR/ambiguous-panic-pick-core.rs:10:5
2+
--> $DIR/ambiguous-panic-pick-core.rs:9:5
33
|
44
LL | panic!(std::string::String::new());
55
| ^^^^^ ambiguous name
@@ -8,7 +8,7 @@ LL | panic!(std::string::String::new());
88
= note: for more information, see issue #147319 <https://github.com/rust-lang/rust/issues/147319>
99
= note: ambiguous because of a conflict between a name from a glob import and an outer scope during import or macro resolution
1010
note: `panic` could refer to the macro imported here
11-
--> $DIR/ambiguous-panic-pick-core.rs:7:5
11+
--> $DIR/ambiguous-panic-pick-core.rs:6:5
1212
|
1313
LL | use ::std::prelude::v1::*;
1414
| ^^^^^^^^^^^^^^^^^^^^^
@@ -18,19 +18,22 @@ note: `panic` could also refer to a macro from prelude
1818
--> $SRC_DIR/core/src/prelude/mod.rs:LL:COL
1919
= note: `#[warn(ambiguous_panic_imports)]` (part of `#[warn(future_incompatible)]`) on by default
2020

21-
warning: panic message is not a string literal
22-
--> $DIR/ambiguous-panic-pick-core.rs:10:12
21+
error[E0308]: mismatched types
22+
--> $DIR/ambiguous-panic-pick-core.rs:9:12
2323
|
2424
LL | panic!(std::string::String::new());
25-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
25+
| -------^^^^^^^^^^^^^^^^^^^^^^^^^^-
26+
| | |
27+
| | expected `&str`, found `String`
28+
| arguments to this function are incorrect
2629
|
27-
= note: this usage of `panic!()` is deprecated; it will be a hard error in Rust 2021
28-
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2021/panic-macro-consistency.html>
29-
= note: `#[warn(non_fmt_panics)]` (part of `#[warn(rust_2021_compatibility)]`) on by default
30-
help: add a "{}" format string to `Display` the message
30+
note: function defined here
31+
--> $SRC_DIR/core/src/panicking.rs:LL:COL
32+
help: consider borrowing here
3133
|
32-
LL | panic!("{}", std::string::String::new());
33-
| +++++
34+
LL | panic!(&std::string::String::new());
35+
| +
3436

35-
warning: 2 warnings emitted
37+
error: aborting due to 1 previous error; 1 warning emitted
3638

39+
For more information about this error, try `rustc --explain E0308`.
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//@ edition: 2018
2+
//@ check-pass
23
#![crate_type = "lib"]
34
use ::core::prelude::v1::*;
45

@@ -7,5 +8,5 @@ fn f() {
78
panic!(std::string::String::new());
89
//~^ WARN: `panic` is ambiguous [ambiguous_panic_imports]
910
//~| WARN: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
10-
//~| ERROR: mismatched types [E0308]
11+
//~| WARN: panic message is not a string literal [non_fmt_panics]
1112
}
Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
warning: `panic` is ambiguous
2-
--> $DIR/ambiguous-panic-pick-std.rs:7:5
2+
--> $DIR/ambiguous-panic-pick-std.rs:8:5
33
|
44
LL | panic!(std::string::String::new());
55
| ^^^^^ ambiguous name
@@ -8,7 +8,7 @@ LL | panic!(std::string::String::new());
88
= note: for more information, see issue #147319 <https://github.com/rust-lang/rust/issues/147319>
99
= note: ambiguous because of a conflict between a name from a glob import and an outer scope during import or macro resolution
1010
note: `panic` could refer to the macro imported here
11-
--> $DIR/ambiguous-panic-pick-std.rs:3:5
11+
--> $DIR/ambiguous-panic-pick-std.rs:4:5
1212
|
1313
LL | use ::core::prelude::v1::*;
1414
| ^^^^^^^^^^^^^^^^^^^^^^
@@ -18,22 +18,19 @@ note: `panic` could also refer to a macro from prelude
1818
--> $SRC_DIR/std/src/prelude/mod.rs:LL:COL
1919
= note: `#[warn(ambiguous_panic_imports)]` (part of `#[warn(future_incompatible)]`) on by default
2020

21-
error[E0308]: mismatched types
22-
--> $DIR/ambiguous-panic-pick-std.rs:7:12
21+
warning: panic message is not a string literal
22+
--> $DIR/ambiguous-panic-pick-std.rs:8:12
2323
|
2424
LL | panic!(std::string::String::new());
25-
| -------^^^^^^^^^^^^^^^^^^^^^^^^^^-
26-
| | |
27-
| | expected `&str`, found `String`
28-
| arguments to this function are incorrect
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
2926
|
30-
note: function defined here
31-
--> $SRC_DIR/core/src/panicking.rs:LL:COL
32-
help: consider borrowing here
27+
= note: this usage of `panic!()` is deprecated; it will be a hard error in Rust 2021
28+
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2021/panic-macro-consistency.html>
29+
= note: `#[warn(non_fmt_panics)]` (part of `#[warn(rust_2021_compatibility)]`) on by default
30+
help: add a "{}" format string to `Display` the message
3331
|
34-
LL | panic!(&std::string::String::new());
35-
| +
32+
LL | panic!("{}", std::string::String::new());
33+
| +++++
3634

37-
error: aborting due to 1 previous error; 1 warning emitted
35+
warning: 2 warnings emitted
3836

39-
For more information about this error, try `rustc --explain E0308`.

tests/ui/imports/ambiguous-panic-rename-builtin.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,5 @@ use m1::*;
1111
fn xx() {
1212
panic!();
1313
//~^ ERROR: `env!()` takes 1 or 2 arguments
14-
//~| WARN: `panic` is ambiguous [ambiguous_panic_imports]
15-
//~| WARN: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
14+
//~| ERROR: `panic` is ambiguous [E0659]
1615
}

tests/ui/imports/ambiguous-panic-rename-builtin.stderr

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@ error: `env!()` takes 1 or 2 arguments
44
LL | panic!();
55
| ^^^^^^^^
66

7-
warning: `panic` is ambiguous
7+
error[E0659]: `panic` is ambiguous
88
--> $DIR/ambiguous-panic-rename-builtin.rs:12:5
99
|
1010
LL | panic!();
1111
| ^^^^^ ambiguous name
1212
|
13-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
14-
= note: for more information, see issue #147319 <https://github.com/rust-lang/rust/issues/147319>
1513
= note: ambiguous because of a conflict between a name from a glob import and an outer scope during import or macro resolution
1614
note: `panic` could refer to the macro imported here
1715
--> $DIR/ambiguous-panic-rename-builtin.rs:9:5
@@ -22,7 +20,7 @@ LL | use m1::*;
2220
= help: or use `crate::panic` to refer to this macro unambiguously
2321
note: `panic` could also refer to a macro from prelude
2422
--> $SRC_DIR/core/src/prelude/mod.rs:LL:COL
25-
= note: `#[warn(ambiguous_panic_imports)]` (part of `#[warn(future_incompatible)]`) on by default
2623

27-
error: aborting due to 1 previous error; 1 warning emitted
24+
error: aborting due to 2 previous errors
2825

26+
For more information about this error, try `rustc --explain E0659`.

0 commit comments

Comments
 (0)