From dedb2cbcf1fb5f7ea28d2d796eb0afb4520efb33 Mon Sep 17 00:00:00 2001 From: Jane Losare-Lusby Date: Mon, 1 Dec 2025 14:39:28 -0800 Subject: [PATCH 1/2] add test cases --- .../ambiguous-panic-glob-vs-multiouter.rs | 20 +++++++++ .../ambiguous-panic-glob-vs-multiouter.stderr | 25 +++++++++++ .../ui/imports/ambiguous-panic-globvsglob.rs | 23 ++++++++++ .../imports/ambiguous-panic-globvsglob.stderr | 42 +++++++++++++++++++ .../ambiguous-panic-no-implicit-prelude.rs | 16 +++++++ ...ambiguous-panic-no-implicit-prelude.stderr | 19 +++++++++ .../ambiguous-panic-non-prelude-core-glob.rs | 11 +++++ ...biguous-panic-non-prelude-core-glob.stderr | 22 ++++++++++ .../ambiguous-panic-non-prelude-std-glob.rs | 12 ++++++ ...mbiguous-panic-non-prelude-std-glob.stderr | 22 ++++++++++ tests/ui/imports/ambiguous-panic-pick-core.rs | 14 +++++++ .../imports/ambiguous-panic-pick-core.stderr | 36 ++++++++++++++++ tests/ui/imports/ambiguous-panic-pick-std.rs | 11 +++++ .../imports/ambiguous-panic-pick-std.stderr | 39 +++++++++++++++++ .../imports/ambiguous-panic-rename-builtin.rs | 16 +++++++ .../ambiguous-panic-rename-builtin.stderr | 28 +++++++++++++ .../imports/ambiguous-panic-rename-panics.rs | 17 ++++++++ .../ambiguous-panic-rename-panics.stderr | 23 ++++++++++ 18 files changed, 396 insertions(+) create mode 100644 tests/ui/imports/ambiguous-panic-glob-vs-multiouter.rs create mode 100644 tests/ui/imports/ambiguous-panic-glob-vs-multiouter.stderr create mode 100644 tests/ui/imports/ambiguous-panic-globvsglob.rs create mode 100644 tests/ui/imports/ambiguous-panic-globvsglob.stderr create mode 100644 tests/ui/imports/ambiguous-panic-no-implicit-prelude.rs create mode 100644 tests/ui/imports/ambiguous-panic-no-implicit-prelude.stderr create mode 100644 tests/ui/imports/ambiguous-panic-non-prelude-core-glob.rs create mode 100644 tests/ui/imports/ambiguous-panic-non-prelude-core-glob.stderr create mode 100644 tests/ui/imports/ambiguous-panic-non-prelude-std-glob.rs create mode 100644 tests/ui/imports/ambiguous-panic-non-prelude-std-glob.stderr create mode 100644 tests/ui/imports/ambiguous-panic-pick-core.rs create mode 100644 tests/ui/imports/ambiguous-panic-pick-core.stderr create mode 100644 tests/ui/imports/ambiguous-panic-pick-std.rs create mode 100644 tests/ui/imports/ambiguous-panic-pick-std.stderr create mode 100644 tests/ui/imports/ambiguous-panic-rename-builtin.rs create mode 100644 tests/ui/imports/ambiguous-panic-rename-builtin.stderr create mode 100644 tests/ui/imports/ambiguous-panic-rename-panics.rs create mode 100644 tests/ui/imports/ambiguous-panic-rename-panics.stderr diff --git a/tests/ui/imports/ambiguous-panic-glob-vs-multiouter.rs b/tests/ui/imports/ambiguous-panic-glob-vs-multiouter.rs new file mode 100644 index 0000000000000..844676605afea --- /dev/null +++ b/tests/ui/imports/ambiguous-panic-glob-vs-multiouter.rs @@ -0,0 +1,20 @@ +//@ edition: 2024 +//@ check-pass +#![crate_type = "lib"] +mod m1 { + pub use core::prelude::v1::*; +} + +mod m2 { + pub use std::prelude::v1::*; +} + +#[allow(unused)] +use m2::*; +fn foo() { + use m1::*; + + panic!(); + //~^ WARN: `panic` is ambiguous [ambiguous_panic_imports] + //~| WARN: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! +} diff --git a/tests/ui/imports/ambiguous-panic-glob-vs-multiouter.stderr b/tests/ui/imports/ambiguous-panic-glob-vs-multiouter.stderr new file mode 100644 index 0000000000000..1fe99648f7aaf --- /dev/null +++ b/tests/ui/imports/ambiguous-panic-glob-vs-multiouter.stderr @@ -0,0 +1,25 @@ +warning: `panic` is ambiguous + --> $DIR/ambiguous-panic-glob-vs-multiouter.rs:20:5 + | +LL | panic!(); + | ^^^^^ ambiguous name + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #147319 + = note: ambiguous because of a conflict between a name from a glob import and an outer scope during import or macro resolution +note: `panic` could refer to the macro imported here + --> $DIR/ambiguous-panic-glob-vs-multiouter.rs:15:9 + | +LL | use m1::*; + | ^^^^^ + = help: consider adding an explicit import of `panic` to disambiguate +note: `panic` could also refer to the macro imported here + --> $DIR/ambiguous-panic-glob-vs-multiouter.rs:13:5 + | +LL | use m2::*; + | ^^^^^ + = help: use `crate::panic` to refer to this macro unambiguously + = note: `#[warn(ambiguous_panic_imports)]` (part of `#[warn(future_incompatible)]`) on by default + +warning: 1 warning emitted + diff --git a/tests/ui/imports/ambiguous-panic-globvsglob.rs b/tests/ui/imports/ambiguous-panic-globvsglob.rs new file mode 100644 index 0000000000000..735adeda741ec --- /dev/null +++ b/tests/ui/imports/ambiguous-panic-globvsglob.rs @@ -0,0 +1,23 @@ +//@ edition: 2024 +#![crate_type = "lib"] +mod m1 { + pub use core::prelude::v1::*; +} + +mod m2 { + pub use std::prelude::v1::*; +} + +#[allow(unused)] +fn foo() { + use m1::*; + use m2::*; + + // I had hoped that this would not produce the globvsglob error because it would never be + // resolving `panic` via one of the ambiguous glob imports above but it appears to do so, not + // sure why + panic!(); + //~^ ERROR: `panic` is ambiguous [E0659] + //~| WARN: `panic` is ambiguous [ambiguous_panic_imports] + //~| WARN: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! +} diff --git a/tests/ui/imports/ambiguous-panic-globvsglob.stderr b/tests/ui/imports/ambiguous-panic-globvsglob.stderr new file mode 100644 index 0000000000000..e802b49f83e28 --- /dev/null +++ b/tests/ui/imports/ambiguous-panic-globvsglob.stderr @@ -0,0 +1,42 @@ +error[E0659]: `panic` is ambiguous + --> $DIR/ambiguous-panic-globvsglob.rs:19:5 + | +LL | panic!(); + | ^^^^^ ambiguous name + | + = note: ambiguous because of multiple glob imports of a name in the same module +note: `panic` could refer to the macro imported here + --> $DIR/ambiguous-panic-globvsglob.rs:13:9 + | +LL | use m1::*; + | ^^^^^ + = help: consider adding an explicit import of `panic` to disambiguate +note: `panic` could also refer to the macro imported here + --> $DIR/ambiguous-panic-globvsglob.rs:14:9 + | +LL | use m2::*; + | ^^^^^ + = help: consider adding an explicit import of `panic` to disambiguate + +warning: `panic` is ambiguous + --> $DIR/ambiguous-panic-globvsglob.rs:19:5 + | +LL | panic!(); + | ^^^^^ ambiguous name + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #147319 + = note: ambiguous because of a conflict between a name from a glob import and an outer scope during import or macro resolution +note: `panic` could refer to the macro imported here + --> $DIR/ambiguous-panic-globvsglob.rs:13:9 + | +LL | use m1::*; + | ^^^^^ + = help: consider adding an explicit import of `panic` to disambiguate +note: `panic` could also refer to a macro from prelude + --> $SRC_DIR/std/src/prelude/mod.rs:LL:COL + = note: `#[warn(ambiguous_panic_imports)]` (part of `#[warn(future_incompatible)]`) on by default + +error: aborting due to 1 previous error; 1 warning emitted + +For more information about this error, try `rustc --explain E0659`. diff --git a/tests/ui/imports/ambiguous-panic-no-implicit-prelude.rs b/tests/ui/imports/ambiguous-panic-no-implicit-prelude.rs new file mode 100644 index 0000000000000..1239cc67cd397 --- /dev/null +++ b/tests/ui/imports/ambiguous-panic-no-implicit-prelude.rs @@ -0,0 +1,16 @@ +//@ edition: 2024 +#![crate_type = "lib"] +#![no_implicit_prelude] + +mod m1 { + macro_rules! panic { + () => {}; + } + + pub(crate) use panic; +} + +fn foo() { + use m1::*; + panic!(); //~ERROR: `panic` is ambiguous [E0659] +} diff --git a/tests/ui/imports/ambiguous-panic-no-implicit-prelude.stderr b/tests/ui/imports/ambiguous-panic-no-implicit-prelude.stderr new file mode 100644 index 0000000000000..fa41409acb2cf --- /dev/null +++ b/tests/ui/imports/ambiguous-panic-no-implicit-prelude.stderr @@ -0,0 +1,19 @@ +error[E0659]: `panic` is ambiguous + --> $DIR/ambiguous-panic-no-implicit-prelude.rs:15:5 + | +LL | panic!(); + | ^^^^^ ambiguous name + | + = note: ambiguous because of a conflict between a name from a glob import and an outer scope during import or macro resolution +note: `panic` could refer to the macro imported here + --> $DIR/ambiguous-panic-no-implicit-prelude.rs:14:9 + | +LL | use m1::*; + | ^^^^^ + = help: consider adding an explicit import of `panic` to disambiguate +note: `panic` could also refer to a macro from prelude + --> $SRC_DIR/std/src/prelude/mod.rs:LL:COL + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0659`. diff --git a/tests/ui/imports/ambiguous-panic-non-prelude-core-glob.rs b/tests/ui/imports/ambiguous-panic-non-prelude-core-glob.rs new file mode 100644 index 0000000000000..e8b6e208c08ba --- /dev/null +++ b/tests/ui/imports/ambiguous-panic-non-prelude-core-glob.rs @@ -0,0 +1,11 @@ +//@ edition: 2024 +//@ check-pass +#![crate_type = "lib"] + +use ::core::*; + +fn f() { + panic!(); + //~^ WARN: `panic` is ambiguous [ambiguous_panic_imports] + //~| WARN: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! +} diff --git a/tests/ui/imports/ambiguous-panic-non-prelude-core-glob.stderr b/tests/ui/imports/ambiguous-panic-non-prelude-core-glob.stderr new file mode 100644 index 0000000000000..5317d8d6d312f --- /dev/null +++ b/tests/ui/imports/ambiguous-panic-non-prelude-core-glob.stderr @@ -0,0 +1,22 @@ +warning: `panic` is ambiguous + --> $DIR/ambiguous-panic-non-prelude-core-glob.rs:8:5 + | +LL | panic!(); + | ^^^^^ ambiguous name + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #147319 + = note: ambiguous because of a conflict between a name from a glob import and an outer scope during import or macro resolution +note: `panic` could refer to the macro imported here + --> $DIR/ambiguous-panic-non-prelude-core-glob.rs:5:5 + | +LL | use ::core::*; + | ^^^^^^^^^ + = help: consider adding an explicit import of `panic` to disambiguate + = help: or use `crate::panic` to refer to this macro unambiguously +note: `panic` could also refer to a macro from prelude + --> $SRC_DIR/std/src/prelude/mod.rs:LL:COL + = note: `#[warn(ambiguous_panic_imports)]` (part of `#[warn(future_incompatible)]`) on by default + +warning: 1 warning emitted + diff --git a/tests/ui/imports/ambiguous-panic-non-prelude-std-glob.rs b/tests/ui/imports/ambiguous-panic-non-prelude-std-glob.rs new file mode 100644 index 0000000000000..0e63f97ec98af --- /dev/null +++ b/tests/ui/imports/ambiguous-panic-non-prelude-std-glob.rs @@ -0,0 +1,12 @@ +//@ check-pass +#![crate_type = "lib"] +#![no_std] + +extern crate std; +use ::std::*; + +fn f() { + panic!(); + //~^ WARN: `panic` is ambiguous [ambiguous_panic_imports] + //~| WARN: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! +} diff --git a/tests/ui/imports/ambiguous-panic-non-prelude-std-glob.stderr b/tests/ui/imports/ambiguous-panic-non-prelude-std-glob.stderr new file mode 100644 index 0000000000000..b7434e3737b8f --- /dev/null +++ b/tests/ui/imports/ambiguous-panic-non-prelude-std-glob.stderr @@ -0,0 +1,22 @@ +warning: `panic` is ambiguous + --> $DIR/ambiguous-panic-non-prelude-std-glob.rs:9:5 + | +LL | panic!(); + | ^^^^^ ambiguous name + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #147319 + = note: ambiguous because of a conflict between a name from a glob import and an outer scope during import or macro resolution +note: `panic` could refer to the macro imported here + --> $DIR/ambiguous-panic-non-prelude-std-glob.rs:6:5 + | +LL | use ::std::*; + | ^^^^^^^^ + = help: consider adding an explicit import of `panic` to disambiguate + = help: or use `crate::panic` to refer to this macro unambiguously +note: `panic` could also refer to a macro from prelude + --> $SRC_DIR/core/src/prelude/mod.rs:LL:COL + = note: `#[warn(ambiguous_panic_imports)]` (part of `#[warn(future_incompatible)]`) on by default + +warning: 1 warning emitted + diff --git a/tests/ui/imports/ambiguous-panic-pick-core.rs b/tests/ui/imports/ambiguous-panic-pick-core.rs new file mode 100644 index 0000000000000..67b285ee430ae --- /dev/null +++ b/tests/ui/imports/ambiguous-panic-pick-core.rs @@ -0,0 +1,14 @@ +//@ edition: 2018 +//@ check-pass +#![crate_type = "lib"] +#![no_std] + +extern crate std; +use ::std::prelude::v1::*; + +fn f() { + panic!(std::string::String::new()); + //~^ WARN: `panic` is ambiguous [ambiguous_panic_imports] + //~| WARN: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + //~| WARN: panic message is not a string literal [non_fmt_panics] +} diff --git a/tests/ui/imports/ambiguous-panic-pick-core.stderr b/tests/ui/imports/ambiguous-panic-pick-core.stderr new file mode 100644 index 0000000000000..55e62961e787d --- /dev/null +++ b/tests/ui/imports/ambiguous-panic-pick-core.stderr @@ -0,0 +1,36 @@ +warning: `panic` is ambiguous + --> $DIR/ambiguous-panic-pick-core.rs:10:5 + | +LL | panic!(std::string::String::new()); + | ^^^^^ ambiguous name + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #147319 + = note: ambiguous because of a conflict between a name from a glob import and an outer scope during import or macro resolution +note: `panic` could refer to the macro imported here + --> $DIR/ambiguous-panic-pick-core.rs:7:5 + | +LL | use ::std::prelude::v1::*; + | ^^^^^^^^^^^^^^^^^^^^^ + = help: consider adding an explicit import of `panic` to disambiguate + = help: or use `crate::panic` to refer to this macro unambiguously +note: `panic` could also refer to a macro from prelude + --> $SRC_DIR/core/src/prelude/mod.rs:LL:COL + = note: `#[warn(ambiguous_panic_imports)]` (part of `#[warn(future_incompatible)]`) on by default + +warning: panic message is not a string literal + --> $DIR/ambiguous-panic-pick-core.rs:10:12 + | +LL | panic!(std::string::String::new()); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: this usage of `panic!()` is deprecated; it will be a hard error in Rust 2021 + = note: for more information, see + = note: `#[warn(non_fmt_panics)]` (part of `#[warn(rust_2021_compatibility)]`) on by default +help: add a "{}" format string to `Display` the message + | +LL | panic!("{}", std::string::String::new()); + | +++++ + +warning: 2 warnings emitted + diff --git a/tests/ui/imports/ambiguous-panic-pick-std.rs b/tests/ui/imports/ambiguous-panic-pick-std.rs new file mode 100644 index 0000000000000..01607999b7168 --- /dev/null +++ b/tests/ui/imports/ambiguous-panic-pick-std.rs @@ -0,0 +1,11 @@ +//@ edition: 2018 +#![crate_type = "lib"] +use ::core::prelude::v1::*; + +#[allow(unused)] +fn f() { + panic!(std::string::String::new()); + //~^ WARN: `panic` is ambiguous [ambiguous_panic_imports] + //~| WARN: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + //~| ERROR: mismatched types [E0308] +} diff --git a/tests/ui/imports/ambiguous-panic-pick-std.stderr b/tests/ui/imports/ambiguous-panic-pick-std.stderr new file mode 100644 index 0000000000000..3944a7ed8bd02 --- /dev/null +++ b/tests/ui/imports/ambiguous-panic-pick-std.stderr @@ -0,0 +1,39 @@ +warning: `panic` is ambiguous + --> $DIR/ambiguous-panic-pick-std.rs:7:5 + | +LL | panic!(std::string::String::new()); + | ^^^^^ ambiguous name + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #147319 + = note: ambiguous because of a conflict between a name from a glob import and an outer scope during import or macro resolution +note: `panic` could refer to the macro imported here + --> $DIR/ambiguous-panic-pick-std.rs:3:5 + | +LL | use ::core::prelude::v1::*; + | ^^^^^^^^^^^^^^^^^^^^^^ + = help: consider adding an explicit import of `panic` to disambiguate + = help: or use `crate::panic` to refer to this macro unambiguously +note: `panic` could also refer to a macro from prelude + --> $SRC_DIR/std/src/prelude/mod.rs:LL:COL + = note: `#[warn(ambiguous_panic_imports)]` (part of `#[warn(future_incompatible)]`) on by default + +error[E0308]: mismatched types + --> $DIR/ambiguous-panic-pick-std.rs:7:12 + | +LL | panic!(std::string::String::new()); + | -------^^^^^^^^^^^^^^^^^^^^^^^^^^- + | | | + | | expected `&str`, found `String` + | arguments to this function are incorrect + | +note: function defined here + --> $SRC_DIR/core/src/panicking.rs:LL:COL +help: consider borrowing here + | +LL | panic!(&std::string::String::new()); + | + + +error: aborting due to 1 previous error; 1 warning emitted + +For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/imports/ambiguous-panic-rename-builtin.rs b/tests/ui/imports/ambiguous-panic-rename-builtin.rs new file mode 100644 index 0000000000000..a03f2d866367e --- /dev/null +++ b/tests/ui/imports/ambiguous-panic-rename-builtin.rs @@ -0,0 +1,16 @@ +//@ edition: 2024 +#![crate_type = "lib"] +#![no_std] + +extern crate std; +mod m1 { + pub use std::prelude::v1::env as panic; +} +use m1::*; + +fn xx() { + panic!(); + //~^ ERROR: `env!()` takes 1 or 2 arguments + //~| WARN: `panic` is ambiguous [ambiguous_panic_imports] + //~| WARN: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! +} diff --git a/tests/ui/imports/ambiguous-panic-rename-builtin.stderr b/tests/ui/imports/ambiguous-panic-rename-builtin.stderr new file mode 100644 index 0000000000000..8b3a66807cb31 --- /dev/null +++ b/tests/ui/imports/ambiguous-panic-rename-builtin.stderr @@ -0,0 +1,28 @@ +error: `env!()` takes 1 or 2 arguments + --> $DIR/ambiguous-panic-rename-builtin.rs:12:5 + | +LL | panic!(); + | ^^^^^^^^ + +warning: `panic` is ambiguous + --> $DIR/ambiguous-panic-rename-builtin.rs:12:5 + | +LL | panic!(); + | ^^^^^ ambiguous name + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #147319 + = note: ambiguous because of a conflict between a name from a glob import and an outer scope during import or macro resolution +note: `panic` could refer to the macro imported here + --> $DIR/ambiguous-panic-rename-builtin.rs:9:5 + | +LL | use m1::*; + | ^^^^^ + = help: consider adding an explicit import of `panic` to disambiguate + = help: or use `crate::panic` to refer to this macro unambiguously +note: `panic` could also refer to a macro from prelude + --> $SRC_DIR/core/src/prelude/mod.rs:LL:COL + = note: `#[warn(ambiguous_panic_imports)]` (part of `#[warn(future_incompatible)]`) on by default + +error: aborting due to 1 previous error; 1 warning emitted + diff --git a/tests/ui/imports/ambiguous-panic-rename-panics.rs b/tests/ui/imports/ambiguous-panic-rename-panics.rs new file mode 100644 index 0000000000000..fbe23a223c93f --- /dev/null +++ b/tests/ui/imports/ambiguous-panic-rename-panics.rs @@ -0,0 +1,17 @@ +//@ edition: 2024 +#![crate_type = "lib"] + +mod m1 { + pub use core::prelude::v1::panic as p; +} + +mod m2 { + pub use std::prelude::v1::panic as p; +} + +use m2::*; +fn xx() { + use m1::*; + + p!(); //~ ERROR: `p` is ambiguous [E0659] +} diff --git a/tests/ui/imports/ambiguous-panic-rename-panics.stderr b/tests/ui/imports/ambiguous-panic-rename-panics.stderr new file mode 100644 index 0000000000000..0838b6bdf2c03 --- /dev/null +++ b/tests/ui/imports/ambiguous-panic-rename-panics.stderr @@ -0,0 +1,23 @@ +error[E0659]: `p` is ambiguous + --> $DIR/ambiguous-panic-rename-panics.rs:16:5 + | +LL | p!(); + | ^ ambiguous name + | + = note: ambiguous because of a conflict between a name from a glob import and an outer scope during import or macro resolution +note: `p` could refer to the macro imported here + --> $DIR/ambiguous-panic-rename-panics.rs:14:9 + | +LL | use m1::*; + | ^^^^^ + = help: consider adding an explicit import of `p` to disambiguate +note: `p` could also refer to the macro imported here + --> $DIR/ambiguous-panic-rename-panics.rs:12:5 + | +LL | use m2::*; + | ^^^^^ + = help: use `crate::p` to refer to this macro unambiguously + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0659`. From 03ee6899718efbae6027ff6435716c85267c5722 Mon Sep 17 00:00:00 2001 From: Jane Losare-Lusby Date: Mon, 1 Dec 2025 14:39:28 -0800 Subject: [PATCH 2/2] trigger lint more narrowly and tiebreak to prelude --- compiler/rustc_resolve/src/ident.rs | 53 ++++++++++++++----- compiler/rustc_resolve/src/lib.rs | 4 ++ .../ambiguous-panic-glob-vs-multiouter.rs | 5 +- .../ambiguous-panic-glob-vs-multiouter.stderr | 14 +++-- tests/ui/imports/ambiguous-panic-pick-core.rs | 4 +- .../imports/ambiguous-panic-pick-core.stderr | 27 +++++----- tests/ui/imports/ambiguous-panic-pick-std.rs | 3 +- .../imports/ambiguous-panic-pick-std.stderr | 27 +++++----- .../imports/ambiguous-panic-rename-builtin.rs | 3 +- .../ambiguous-panic-rename-builtin.stderr | 8 ++- 10 files changed, 86 insertions(+), 62 deletions(-) diff --git a/compiler/rustc_resolve/src/ident.rs b/compiler/rustc_resolve/src/ident.rs index c02109608f65d..7870abd2ffe6a 100644 --- a/compiler/rustc_resolve/src/ident.rs +++ b/compiler/rustc_resolve/src/ident.rs @@ -645,11 +645,20 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { return None; } + // preemptively look for ambiguities for panic macros to ensure we don't + // speculatively resolve to a non-prelude item when an ambiguity that we'd + // downgrade is present + let is_issue_147319_hack = || ctxt.edition() + <= Edition::Edition2024 + && matches!(orig_ident.name, sym::panic) + && (this.is_specific_builtin_macro(binding.res(), sym::std_panic) || this.is_specific_builtin_macro(binding.res(), sym::core_panic)); + let finalizing = !matches!(finalize, None | Some(Finalize { stage: Stage::Late, .. })); + // Below we report various ambiguity errors. // We do not need to report them if we are either in speculative resolution, // or in late resolution when everything is already imported and expanded // and no ambiguities exist. - if matches!(finalize, None | Some(Finalize { stage: Stage::Late, .. })) { + if !finalizing && !is_issue_147319_hack() { return Some(Ok(binding)); } @@ -720,8 +729,20 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { let is_issue_147319_hack = ctxt.edition() <= Edition::Edition2024 && matches!(orig_ident.name, sym::panic) - && this.is_builtin_macro(binding.res()) - && this.is_builtin_macro(innermost_binding.res()); + && matches!(scope, Scope::StdLibPrelude) + && ((this.is_specific_builtin_macro( + binding.res(), + sym::std_panic, + ) && this.is_specific_builtin_macro( + innermost_binding.res(), + sym::core_panic, + )) || (this.is_specific_builtin_macro( + binding.res(), + sym::core_panic, + ) && this.is_specific_builtin_macro( + innermost_binding.res(), + sym::std_panic, + ))); let warning = if is_issue_147319_hack { Some(AmbiguityWarning::PanicImport) @@ -740,16 +761,22 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { AmbiguityErrorMisc::None } }; - this.get_mut().ambiguity_errors.push(AmbiguityError { - kind, - ident: orig_ident, - b1: innermost_binding, - b2: binding, - warning, - misc1: misc(innermost_flags), - misc2: misc(flags), - }); - return Some(Ok(innermost_binding)); + if finalizing { + this.get_mut().ambiguity_errors.push(AmbiguityError { + kind, + ident: orig_ident, + b1: innermost_binding, + b2: binding, + warning, + misc1: misc(innermost_flags), + misc2: misc(flags), + }); + } + if is_issue_147319_hack { + return Some(Ok(binding)); + } else { + return Some(Ok(innermost_binding)); + } } } } else { diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 6ca6840c3788e..e83db37a0b2af 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -1857,6 +1857,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { self.get_macro(res).is_some_and(|macro_data| macro_data.ext.builtin_name.is_some()) } + fn is_specific_builtin_macro(&self, res: Res, symbol: Symbol) -> bool { + self.get_macro(res).is_some_and(|macro_data| macro_data.ext.builtin_name == Some(symbol)) + } + fn macro_def(&self, mut ctxt: SyntaxContext) -> DefId { loop { match ctxt.outer_expn_data().macro_def_id { diff --git a/tests/ui/imports/ambiguous-panic-glob-vs-multiouter.rs b/tests/ui/imports/ambiguous-panic-glob-vs-multiouter.rs index 844676605afea..3a94e19428cd6 100644 --- a/tests/ui/imports/ambiguous-panic-glob-vs-multiouter.rs +++ b/tests/ui/imports/ambiguous-panic-glob-vs-multiouter.rs @@ -1,5 +1,4 @@ //@ edition: 2024 -//@ check-pass #![crate_type = "lib"] mod m1 { pub use core::prelude::v1::*; @@ -14,7 +13,5 @@ use m2::*; fn foo() { use m1::*; - panic!(); - //~^ WARN: `panic` is ambiguous [ambiguous_panic_imports] - //~| WARN: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + panic!(); //~ ERROR: `panic` is ambiguous [E0659] } diff --git a/tests/ui/imports/ambiguous-panic-glob-vs-multiouter.stderr b/tests/ui/imports/ambiguous-panic-glob-vs-multiouter.stderr index 1fe99648f7aaf..aa15901b6619d 100644 --- a/tests/ui/imports/ambiguous-panic-glob-vs-multiouter.stderr +++ b/tests/ui/imports/ambiguous-panic-glob-vs-multiouter.stderr @@ -1,25 +1,23 @@ -warning: `panic` is ambiguous - --> $DIR/ambiguous-panic-glob-vs-multiouter.rs:20:5 +error[E0659]: `panic` is ambiguous + --> $DIR/ambiguous-panic-glob-vs-multiouter.rs:19:5 | LL | panic!(); | ^^^^^ ambiguous name | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #147319 = note: ambiguous because of a conflict between a name from a glob import and an outer scope during import or macro resolution note: `panic` could refer to the macro imported here - --> $DIR/ambiguous-panic-glob-vs-multiouter.rs:15:9 + --> $DIR/ambiguous-panic-glob-vs-multiouter.rs:14:9 | LL | use m1::*; | ^^^^^ = help: consider adding an explicit import of `panic` to disambiguate note: `panic` could also refer to the macro imported here - --> $DIR/ambiguous-panic-glob-vs-multiouter.rs:13:5 + --> $DIR/ambiguous-panic-glob-vs-multiouter.rs:12:5 | LL | use m2::*; | ^^^^^ = help: use `crate::panic` to refer to this macro unambiguously - = note: `#[warn(ambiguous_panic_imports)]` (part of `#[warn(future_incompatible)]`) on by default -warning: 1 warning emitted +error: aborting due to 1 previous error +For more information about this error, try `rustc --explain E0659`. diff --git a/tests/ui/imports/ambiguous-panic-pick-core.rs b/tests/ui/imports/ambiguous-panic-pick-core.rs index 67b285ee430ae..d543309d43efd 100644 --- a/tests/ui/imports/ambiguous-panic-pick-core.rs +++ b/tests/ui/imports/ambiguous-panic-pick-core.rs @@ -1,5 +1,4 @@ //@ edition: 2018 -//@ check-pass #![crate_type = "lib"] #![no_std] @@ -10,5 +9,6 @@ fn f() { panic!(std::string::String::new()); //~^ WARN: `panic` is ambiguous [ambiguous_panic_imports] //~| WARN: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - //~| WARN: panic message is not a string literal [non_fmt_panics] + //~| ERROR: mismatched types [E0308] + } diff --git a/tests/ui/imports/ambiguous-panic-pick-core.stderr b/tests/ui/imports/ambiguous-panic-pick-core.stderr index 55e62961e787d..350be2a58c436 100644 --- a/tests/ui/imports/ambiguous-panic-pick-core.stderr +++ b/tests/ui/imports/ambiguous-panic-pick-core.stderr @@ -1,5 +1,5 @@ warning: `panic` is ambiguous - --> $DIR/ambiguous-panic-pick-core.rs:10:5 + --> $DIR/ambiguous-panic-pick-core.rs:9:5 | LL | panic!(std::string::String::new()); | ^^^^^ ambiguous name @@ -8,7 +8,7 @@ LL | panic!(std::string::String::new()); = note: for more information, see issue #147319 = note: ambiguous because of a conflict between a name from a glob import and an outer scope during import or macro resolution note: `panic` could refer to the macro imported here - --> $DIR/ambiguous-panic-pick-core.rs:7:5 + --> $DIR/ambiguous-panic-pick-core.rs:6:5 | LL | use ::std::prelude::v1::*; | ^^^^^^^^^^^^^^^^^^^^^ @@ -18,19 +18,22 @@ note: `panic` could also refer to a macro from prelude --> $SRC_DIR/core/src/prelude/mod.rs:LL:COL = note: `#[warn(ambiguous_panic_imports)]` (part of `#[warn(future_incompatible)]`) on by default -warning: panic message is not a string literal - --> $DIR/ambiguous-panic-pick-core.rs:10:12 +error[E0308]: mismatched types + --> $DIR/ambiguous-panic-pick-core.rs:9:12 | LL | panic!(std::string::String::new()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | -------^^^^^^^^^^^^^^^^^^^^^^^^^^- + | | | + | | expected `&str`, found `String` + | arguments to this function are incorrect | - = note: this usage of `panic!()` is deprecated; it will be a hard error in Rust 2021 - = note: for more information, see - = note: `#[warn(non_fmt_panics)]` (part of `#[warn(rust_2021_compatibility)]`) on by default -help: add a "{}" format string to `Display` the message +note: function defined here + --> $SRC_DIR/core/src/panicking.rs:LL:COL +help: consider borrowing here | -LL | panic!("{}", std::string::String::new()); - | +++++ +LL | panic!(&std::string::String::new()); + | + -warning: 2 warnings emitted +error: aborting due to 1 previous error; 1 warning emitted +For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/imports/ambiguous-panic-pick-std.rs b/tests/ui/imports/ambiguous-panic-pick-std.rs index 01607999b7168..e61097d3bb2f1 100644 --- a/tests/ui/imports/ambiguous-panic-pick-std.rs +++ b/tests/ui/imports/ambiguous-panic-pick-std.rs @@ -1,4 +1,5 @@ //@ edition: 2018 +//@ check-pass #![crate_type = "lib"] use ::core::prelude::v1::*; @@ -7,5 +8,5 @@ fn f() { panic!(std::string::String::new()); //~^ WARN: `panic` is ambiguous [ambiguous_panic_imports] //~| WARN: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - //~| ERROR: mismatched types [E0308] + //~| WARN: panic message is not a string literal [non_fmt_panics] } diff --git a/tests/ui/imports/ambiguous-panic-pick-std.stderr b/tests/ui/imports/ambiguous-panic-pick-std.stderr index 3944a7ed8bd02..984e6489942cb 100644 --- a/tests/ui/imports/ambiguous-panic-pick-std.stderr +++ b/tests/ui/imports/ambiguous-panic-pick-std.stderr @@ -1,5 +1,5 @@ warning: `panic` is ambiguous - --> $DIR/ambiguous-panic-pick-std.rs:7:5 + --> $DIR/ambiguous-panic-pick-std.rs:8:5 | LL | panic!(std::string::String::new()); | ^^^^^ ambiguous name @@ -8,7 +8,7 @@ LL | panic!(std::string::String::new()); = note: for more information, see issue #147319 = note: ambiguous because of a conflict between a name from a glob import and an outer scope during import or macro resolution note: `panic` could refer to the macro imported here - --> $DIR/ambiguous-panic-pick-std.rs:3:5 + --> $DIR/ambiguous-panic-pick-std.rs:4:5 | LL | use ::core::prelude::v1::*; | ^^^^^^^^^^^^^^^^^^^^^^ @@ -18,22 +18,19 @@ note: `panic` could also refer to a macro from prelude --> $SRC_DIR/std/src/prelude/mod.rs:LL:COL = note: `#[warn(ambiguous_panic_imports)]` (part of `#[warn(future_incompatible)]`) on by default -error[E0308]: mismatched types - --> $DIR/ambiguous-panic-pick-std.rs:7:12 +warning: panic message is not a string literal + --> $DIR/ambiguous-panic-pick-std.rs:8:12 | LL | panic!(std::string::String::new()); - | -------^^^^^^^^^^^^^^^^^^^^^^^^^^- - | | | - | | expected `&str`, found `String` - | arguments to this function are incorrect + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | -note: function defined here - --> $SRC_DIR/core/src/panicking.rs:LL:COL -help: consider borrowing here + = note: this usage of `panic!()` is deprecated; it will be a hard error in Rust 2021 + = note: for more information, see + = note: `#[warn(non_fmt_panics)]` (part of `#[warn(rust_2021_compatibility)]`) on by default +help: add a "{}" format string to `Display` the message | -LL | panic!(&std::string::String::new()); - | + +LL | panic!("{}", std::string::String::new()); + | +++++ -error: aborting due to 1 previous error; 1 warning emitted +warning: 2 warnings emitted -For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/imports/ambiguous-panic-rename-builtin.rs b/tests/ui/imports/ambiguous-panic-rename-builtin.rs index a03f2d866367e..63dfa540c2a28 100644 --- a/tests/ui/imports/ambiguous-panic-rename-builtin.rs +++ b/tests/ui/imports/ambiguous-panic-rename-builtin.rs @@ -11,6 +11,5 @@ use m1::*; fn xx() { panic!(); //~^ ERROR: `env!()` takes 1 or 2 arguments - //~| WARN: `panic` is ambiguous [ambiguous_panic_imports] - //~| WARN: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + //~| ERROR: `panic` is ambiguous [E0659] } diff --git a/tests/ui/imports/ambiguous-panic-rename-builtin.stderr b/tests/ui/imports/ambiguous-panic-rename-builtin.stderr index 8b3a66807cb31..146863762eaf7 100644 --- a/tests/ui/imports/ambiguous-panic-rename-builtin.stderr +++ b/tests/ui/imports/ambiguous-panic-rename-builtin.stderr @@ -4,14 +4,12 @@ error: `env!()` takes 1 or 2 arguments LL | panic!(); | ^^^^^^^^ -warning: `panic` is ambiguous +error[E0659]: `panic` is ambiguous --> $DIR/ambiguous-panic-rename-builtin.rs:12:5 | LL | panic!(); | ^^^^^ ambiguous name | - = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! - = note: for more information, see issue #147319 = note: ambiguous because of a conflict between a name from a glob import and an outer scope during import or macro resolution note: `panic` could refer to the macro imported here --> $DIR/ambiguous-panic-rename-builtin.rs:9:5 @@ -22,7 +20,7 @@ LL | use m1::*; = help: or use `crate::panic` to refer to this macro unambiguously note: `panic` could also refer to a macro from prelude --> $SRC_DIR/core/src/prelude/mod.rs:LL:COL - = note: `#[warn(ambiguous_panic_imports)]` (part of `#[warn(future_incompatible)]`) on by default -error: aborting due to 1 previous error; 1 warning emitted +error: aborting due to 2 previous errors +For more information about this error, try `rustc --explain E0659`.