From 25ab022f2951318e8ffdec2d8566c3a7d164e00d Mon Sep 17 00:00:00 2001 From: xizheyin Date: Sun, 29 Jun 2025 15:37:28 +0800 Subject: move `tests/ui/resolve/suggest*` to `tests/ui/resolve/suggestions/` Signed-off-by: xizheyin --- .../auxiliary/suggest-constructor-cycle-error.rs | 12 -- tests/ui/resolve/suggest-builder-fn.rs | 64 -------- tests/ui/resolve/suggest-builder-fn.stderr | 51 ------ .../ui/resolve/suggest-constructor-cycle-error.rs | 10 -- .../resolve/suggest-constructor-cycle-error.stderr | 15 -- .../suggest-import-without-clobbering-attrs.fixed | 16 -- .../suggest-import-without-clobbering-attrs.rs | 15 -- .../suggest-import-without-clobbering-attrs.stderr | 14 -- tests/ui/resolve/suggest-path-for-tuple-struct.rs | 26 --- .../resolve/suggest-path-for-tuple-struct.stderr | 27 ---- .../suggest-path-instead-of-mod-dot-item.rs | 111 ------------- .../suggest-path-instead-of-mod-dot-item.stderr | 178 --------------------- .../auxiliary/suggest-constructor-cycle-error.rs | 12 ++ tests/ui/resolve/suggestions/suggest-builder-fn.rs | 64 ++++++++ .../resolve/suggestions/suggest-builder-fn.stderr | 51 ++++++ .../suggestions/suggest-constructor-cycle-error.rs | 10 ++ .../suggest-constructor-cycle-error.stderr | 15 ++ .../suggest-import-without-clobbering-attrs.fixed | 16 ++ .../suggest-import-without-clobbering-attrs.rs | 15 ++ .../suggest-import-without-clobbering-attrs.stderr | 14 ++ .../suggestions/suggest-path-for-tuple-struct.rs | 26 +++ .../suggest-path-for-tuple-struct.stderr | 27 ++++ .../suggest-path-instead-of-mod-dot-item.rs | 111 +++++++++++++ .../suggest-path-instead-of-mod-dot-item.stderr | 178 +++++++++++++++++++++ 24 files changed, 539 insertions(+), 539 deletions(-) delete mode 100644 tests/ui/resolve/auxiliary/suggest-constructor-cycle-error.rs delete mode 100644 tests/ui/resolve/suggest-builder-fn.rs delete mode 100644 tests/ui/resolve/suggest-builder-fn.stderr delete mode 100644 tests/ui/resolve/suggest-constructor-cycle-error.rs delete mode 100644 tests/ui/resolve/suggest-constructor-cycle-error.stderr delete mode 100644 tests/ui/resolve/suggest-import-without-clobbering-attrs.fixed delete mode 100644 tests/ui/resolve/suggest-import-without-clobbering-attrs.rs delete mode 100644 tests/ui/resolve/suggest-import-without-clobbering-attrs.stderr delete mode 100644 tests/ui/resolve/suggest-path-for-tuple-struct.rs delete mode 100644 tests/ui/resolve/suggest-path-for-tuple-struct.stderr delete mode 100644 tests/ui/resolve/suggest-path-instead-of-mod-dot-item.rs delete mode 100644 tests/ui/resolve/suggest-path-instead-of-mod-dot-item.stderr create mode 100644 tests/ui/resolve/suggestions/auxiliary/suggest-constructor-cycle-error.rs create mode 100644 tests/ui/resolve/suggestions/suggest-builder-fn.rs create mode 100644 tests/ui/resolve/suggestions/suggest-builder-fn.stderr create mode 100644 tests/ui/resolve/suggestions/suggest-constructor-cycle-error.rs create mode 100644 tests/ui/resolve/suggestions/suggest-constructor-cycle-error.stderr create mode 100644 tests/ui/resolve/suggestions/suggest-import-without-clobbering-attrs.fixed create mode 100644 tests/ui/resolve/suggestions/suggest-import-without-clobbering-attrs.rs create mode 100644 tests/ui/resolve/suggestions/suggest-import-without-clobbering-attrs.stderr create mode 100644 tests/ui/resolve/suggestions/suggest-path-for-tuple-struct.rs create mode 100644 tests/ui/resolve/suggestions/suggest-path-for-tuple-struct.stderr create mode 100644 tests/ui/resolve/suggestions/suggest-path-instead-of-mod-dot-item.rs create mode 100644 tests/ui/resolve/suggestions/suggest-path-instead-of-mod-dot-item.stderr (limited to 'tests') diff --git a/tests/ui/resolve/auxiliary/suggest-constructor-cycle-error.rs b/tests/ui/resolve/auxiliary/suggest-constructor-cycle-error.rs deleted file mode 100644 index 8de68c38bc3..00000000000 --- a/tests/ui/resolve/auxiliary/suggest-constructor-cycle-error.rs +++ /dev/null @@ -1,12 +0,0 @@ -mod m { - pub struct Uuid(()); - - impl Uuid { - pub fn encode_buffer() -> [u8; LENGTH] { - [] - } - } - const LENGTH: usize = 0; -} - -pub use m::Uuid; diff --git a/tests/ui/resolve/suggest-builder-fn.rs b/tests/ui/resolve/suggest-builder-fn.rs deleted file mode 100644 index 959675ef2c9..00000000000 --- a/tests/ui/resolve/suggest-builder-fn.rs +++ /dev/null @@ -1,64 +0,0 @@ -// Tests that we suggest the right alternatives when -// a builder method cannot be resolved - -use std::net::TcpStream; - -trait SomeTrait {} - -struct Foo { - v : T -} - -impl Foo { - // Should not be suggested if constraint on the impl not met - fn new() -> Self { - Self { v: T::default() } - } -} - -struct Bar; - -impl Bar { - // Should be suggested - fn build() -> Self { - Self {} - } - - // Method with self can't be a builder. - // Should not be suggested - fn build_with_self(&self) -> Self { - Self {} - } -} - -mod SomeMod { - use crate::Bar; - - impl Bar { - // Public method. Should be suggested - pub fn build_public() -> Self { - Self {} - } - - // Private method. Should not be suggested - fn build_private() -> Self { - Self {} - } - } -} - -fn main() { - // `new` not found on `TcpStream` and `connect` should be suggested - let _stream = TcpStream::new(); - //~^ ERROR no function or associated item named `new` found - - // Although `new` is found on `>` it should not be - // suggested because `u8` does not meet the `T: SomeTrait` constraint - let _foo = Foo::::new(); - //~^ ERROR the function or associated item `new` exists for struct `Foo`, but its trait bounds were not satisfied - - // Should suggest only `::build()` and `SomeMod::::build_public()`. - // Other methods should not suggested because they are private or are not a builder - let _bar = Bar::new(); - //~^ ERROR no function or associated item named `new` found -} diff --git a/tests/ui/resolve/suggest-builder-fn.stderr b/tests/ui/resolve/suggest-builder-fn.stderr deleted file mode 100644 index 9c5eed35ccf..00000000000 --- a/tests/ui/resolve/suggest-builder-fn.stderr +++ /dev/null @@ -1,51 +0,0 @@ -error[E0599]: no function or associated item named `new` found for struct `TcpStream` in the current scope - --> $DIR/suggest-builder-fn.rs:52:29 - | -LL | let _stream = TcpStream::new(); - | ^^^ function or associated item not found in `TcpStream` - | -note: if you're trying to build a new `TcpStream` consider using one of the following associated functions: - TcpStream::connect - TcpStream::connect_timeout - --> $SRC_DIR/std/src/net/tcp.rs:LL:COL - -error[E0599]: the function or associated item `new` exists for struct `Foo`, but its trait bounds were not satisfied - --> $DIR/suggest-builder-fn.rs:57:27 - | -LL | struct Foo { - | ------------- function or associated item `new` not found for this struct -... -LL | let _foo = Foo::::new(); - | ^^^ function or associated item cannot be called on `Foo` due to unsatisfied trait bounds - | -note: trait bound `u8: SomeTrait` was not satisfied - --> $DIR/suggest-builder-fn.rs:12:9 - | -LL | impl Foo { - | ^^^^^^^^^ ------ - | | - | unsatisfied trait bound introduced here - -error[E0599]: no function or associated item named `new` found for struct `Bar` in the current scope - --> $DIR/suggest-builder-fn.rs:62:21 - | -LL | struct Bar; - | ---------- function or associated item `new` not found for this struct -... -LL | let _bar = Bar::new(); - | ^^^ function or associated item not found in `Bar` - | -note: if you're trying to build a new `Bar` consider using one of the following associated functions: - Bar::build - SomeMod::::build_public - --> $DIR/suggest-builder-fn.rs:23:5 - | -LL | fn build() -> Self { - | ^^^^^^^^^^^^^^^^^^ -... -LL | pub fn build_public() -> Self { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -error: aborting due to 3 previous errors - -For more information about this error, try `rustc --explain E0599`. diff --git a/tests/ui/resolve/suggest-constructor-cycle-error.rs b/tests/ui/resolve/suggest-constructor-cycle-error.rs deleted file mode 100644 index c23d6788eef..00000000000 --- a/tests/ui/resolve/suggest-constructor-cycle-error.rs +++ /dev/null @@ -1,10 +0,0 @@ -//@ aux-build:suggest-constructor-cycle-error.rs - -// Regression test for https://github.com/rust-lang/rust/issues/119625 - -extern crate suggest_constructor_cycle_error as a; - -const CONST_NAME: a::Uuid = a::Uuid(()); -//~^ ERROR: cannot initialize a tuple struct which contains private fields [E0423] - -fn main() {} diff --git a/tests/ui/resolve/suggest-constructor-cycle-error.stderr b/tests/ui/resolve/suggest-constructor-cycle-error.stderr deleted file mode 100644 index c6ec2465a43..00000000000 --- a/tests/ui/resolve/suggest-constructor-cycle-error.stderr +++ /dev/null @@ -1,15 +0,0 @@ -error[E0423]: cannot initialize a tuple struct which contains private fields - --> $DIR/suggest-constructor-cycle-error.rs:7:29 - | -LL | const CONST_NAME: a::Uuid = a::Uuid(()); - | ^^^^^^^ - | -note: constructor is not visible here due to private fields - --> $DIR/auxiliary/suggest-constructor-cycle-error.rs:2:21 - | -LL | pub struct Uuid(()); - | ^^ private field - -error: aborting due to 1 previous error - -For more information about this error, try `rustc --explain E0423`. diff --git a/tests/ui/resolve/suggest-import-without-clobbering-attrs.fixed b/tests/ui/resolve/suggest-import-without-clobbering-attrs.fixed deleted file mode 100644 index 607c9af4927..00000000000 --- a/tests/ui/resolve/suggest-import-without-clobbering-attrs.fixed +++ /dev/null @@ -1,16 +0,0 @@ -//@ run-rustfix -//@ compile-flags: -Aunused - -use y::z; -#[cfg(all())] -use y::Whatever; - -mod y { - pub(crate) fn z() {} - pub(crate) struct Whatever; -} - -fn main() { - z(); - //~^ ERROR cannot find function `z` in this scope -} diff --git a/tests/ui/resolve/suggest-import-without-clobbering-attrs.rs b/tests/ui/resolve/suggest-import-without-clobbering-attrs.rs deleted file mode 100644 index 6cc53fb1086..00000000000 --- a/tests/ui/resolve/suggest-import-without-clobbering-attrs.rs +++ /dev/null @@ -1,15 +0,0 @@ -//@ run-rustfix -//@ compile-flags: -Aunused - -#[cfg(all())] -use y::Whatever; - -mod y { - pub(crate) fn z() {} - pub(crate) struct Whatever; -} - -fn main() { - z(); - //~^ ERROR cannot find function `z` in this scope -} diff --git a/tests/ui/resolve/suggest-import-without-clobbering-attrs.stderr b/tests/ui/resolve/suggest-import-without-clobbering-attrs.stderr deleted file mode 100644 index de65d695dd2..00000000000 --- a/tests/ui/resolve/suggest-import-without-clobbering-attrs.stderr +++ /dev/null @@ -1,14 +0,0 @@ -error[E0425]: cannot find function `z` in this scope - --> $DIR/suggest-import-without-clobbering-attrs.rs:13:5 - | -LL | z(); - | ^ not found in this scope - | -help: consider importing this function - | -LL + use y::z; - | - -error: aborting due to 1 previous error - -For more information about this error, try `rustc --explain E0425`. diff --git a/tests/ui/resolve/suggest-path-for-tuple-struct.rs b/tests/ui/resolve/suggest-path-for-tuple-struct.rs deleted file mode 100644 index c8bc3e79fe2..00000000000 --- a/tests/ui/resolve/suggest-path-for-tuple-struct.rs +++ /dev/null @@ -1,26 +0,0 @@ -mod module { - pub struct SomeTupleStruct(u8); - pub struct SomeRegularStruct { - foo: u8 - } - - impl SomeTupleStruct { - pub fn new() -> Self { - Self(0) - } - } - impl SomeRegularStruct { - pub fn new() -> Self { - Self { foo: 0 } - } - } -} - -use module::{SomeTupleStruct, SomeRegularStruct}; - -fn main() { - let _ = SomeTupleStruct.new(); - //~^ ERROR expected value, found struct `SomeTupleStruct` - let _ = SomeRegularStruct.new(); - //~^ ERROR expected value, found struct `SomeRegularStruct` -} diff --git a/tests/ui/resolve/suggest-path-for-tuple-struct.stderr b/tests/ui/resolve/suggest-path-for-tuple-struct.stderr deleted file mode 100644 index 68a5b550978..00000000000 --- a/tests/ui/resolve/suggest-path-for-tuple-struct.stderr +++ /dev/null @@ -1,27 +0,0 @@ -error[E0423]: expected value, found struct `SomeTupleStruct` - --> $DIR/suggest-path-for-tuple-struct.rs:22:13 - | -LL | let _ = SomeTupleStruct.new(); - | ^^^^^^^^^^^^^^^ - | -help: use the path separator to refer to an item - | -LL - let _ = SomeTupleStruct.new(); -LL + let _ = SomeTupleStruct::new(); - | - -error[E0423]: expected value, found struct `SomeRegularStruct` - --> $DIR/suggest-path-for-tuple-struct.rs:24:13 - | -LL | let _ = SomeRegularStruct.new(); - | ^^^^^^^^^^^^^^^^^ - | -help: use the path separator to refer to an item - | -LL - let _ = SomeRegularStruct.new(); -LL + let _ = SomeRegularStruct::new(); - | - -error: aborting due to 2 previous errors - -For more information about this error, try `rustc --explain E0423`. diff --git a/tests/ui/resolve/suggest-path-instead-of-mod-dot-item.rs b/tests/ui/resolve/suggest-path-instead-of-mod-dot-item.rs deleted file mode 100644 index d5d6b13d62c..00000000000 --- a/tests/ui/resolve/suggest-path-instead-of-mod-dot-item.rs +++ /dev/null @@ -1,111 +0,0 @@ -// Beginners write `mod.item` when they should write `mod::item`. -// This tests that we suggest the latter when we encounter the former. - -pub mod a { - pub const I: i32 = 1; - - pub fn f() -> i32 { 2 } - - pub mod b { - pub const J: i32 = 3; - - pub fn g() -> i32 { 4 } - } -} - -fn h1() -> i32 { - a.I - //~^ ERROR expected value, found module `a` - //~| HELP use the path separator -} - -fn h2() -> i32 { - a.g() - //~^ ERROR expected value, found module `a` - //~| HELP use the path separator -} - -fn h3() -> i32 { - a.b.J - //~^ ERROR expected value, found module `a` - //~| HELP use the path separator -} - -fn h4() -> i32 { - a::b.J - //~^ ERROR expected value, found module `a::b` - //~| HELP a constant with a similar name exists - //~| HELP use the path separator -} - -fn h5() { - a.b.f(); - //~^ ERROR expected value, found module `a` - //~| HELP use the path separator - let v = Vec::new(); - v.push(a::b); - //~^ ERROR expected value, found module `a::b` - //~| HELP a constant with a similar name exists -} - -fn h6() -> i32 { - a::b.f() - //~^ ERROR expected value, found module `a::b` - //~| HELP a constant with a similar name exists - //~| HELP use the path separator -} - -fn h7() { - a::b - //~^ ERROR expected value, found module `a::b` - //~| HELP a constant with a similar name exists -} - -fn h8() -> i32 { - a::b() - //~^ ERROR expected function, found module `a::b` - //~| HELP a constant with a similar name exists -} - -macro_rules! module { - () => { - a - //~^ ERROR expected value, found module `a` - //~| ERROR expected value, found module `a` - }; -} - -macro_rules! create { - (method) => { - a.f() - //~^ ERROR expected value, found module `a` - //~| HELP use the path separator - }; - (field) => { - a.f - //~^ ERROR expected value, found module `a` - //~| HELP use the path separator - }; -} - -fn h9() { - // - // Note that if the receiver is a macro call, we do not want to suggest to replace - // `.` with `::` as that would be a syntax error. - // Since the receiver is a module and not a type, we cannot suggest to surround - // it with angle brackets. - // - - module!().g::<()>(); // no `help` here! - - module!().g; // no `help` here! - - // - // Ensure that the suggestion is shown for expressions inside of macro definitions. - // - - let _ = create!(method); - let _ = create!(field); -} - -fn main() {} diff --git a/tests/ui/resolve/suggest-path-instead-of-mod-dot-item.stderr b/tests/ui/resolve/suggest-path-instead-of-mod-dot-item.stderr deleted file mode 100644 index 5db943cd10d..00000000000 --- a/tests/ui/resolve/suggest-path-instead-of-mod-dot-item.stderr +++ /dev/null @@ -1,178 +0,0 @@ -error[E0423]: expected value, found module `a` - --> $DIR/suggest-path-instead-of-mod-dot-item.rs:17:5 - | -LL | a.I - | ^ - | -help: use the path separator to refer to an item - | -LL - a.I -LL + a::I - | - -error[E0423]: expected value, found module `a` - --> $DIR/suggest-path-instead-of-mod-dot-item.rs:23:5 - | -LL | a.g() - | ^ - | -help: use the path separator to refer to an item - | -LL - a.g() -LL + a::g() - | - -error[E0423]: expected value, found module `a` - --> $DIR/suggest-path-instead-of-mod-dot-item.rs:29:5 - | -LL | a.b.J - | ^ - | -help: use the path separator to refer to an item - | -LL - a.b.J -LL + a::b.J - | - -error[E0423]: expected value, found module `a::b` - --> $DIR/suggest-path-instead-of-mod-dot-item.rs:35:5 - | -LL | pub const I: i32 = 1; - | --------------------- similarly named constant `I` defined here -... -LL | a::b.J - | ^^^^ - | -help: use the path separator to refer to an item - | -LL - a::b.J -LL + a::b::J - | -help: a constant with a similar name exists - | -LL - a::b.J -LL + a::I.J - | - -error[E0423]: expected value, found module `a` - --> $DIR/suggest-path-instead-of-mod-dot-item.rs:42:5 - | -LL | a.b.f(); - | ^ - | -help: use the path separator to refer to an item - | -LL - a.b.f(); -LL + a::b.f(); - | - -error[E0423]: expected value, found module `a::b` - --> $DIR/suggest-path-instead-of-mod-dot-item.rs:46:12 - | -LL | pub const I: i32 = 1; - | --------------------- similarly named constant `I` defined here -... -LL | v.push(a::b); - | ^^^- - | | - | help: a constant with a similar name exists: `I` - -error[E0423]: expected value, found module `a::b` - --> $DIR/suggest-path-instead-of-mod-dot-item.rs:52:5 - | -LL | pub const I: i32 = 1; - | --------------------- similarly named constant `I` defined here -... -LL | a::b.f() - | ^^^^ - | -help: use the path separator to refer to an item - | -LL - a::b.f() -LL + a::b::f() - | -help: a constant with a similar name exists - | -LL - a::b.f() -LL + a::I.f() - | - -error[E0423]: expected value, found module `a::b` - --> $DIR/suggest-path-instead-of-mod-dot-item.rs:59:5 - | -LL | pub const I: i32 = 1; - | --------------------- similarly named constant `I` defined here -... -LL | a::b - | ^^^- - | | - | help: a constant with a similar name exists: `I` - -error[E0423]: expected function, found module `a::b` - --> $DIR/suggest-path-instead-of-mod-dot-item.rs:65:5 - | -LL | pub const I: i32 = 1; - | --------------------- similarly named constant `I` defined here -... -LL | a::b() - | ^^^- - | | - | help: a constant with a similar name exists: `I` - -error[E0423]: expected value, found module `a` - --> $DIR/suggest-path-instead-of-mod-dot-item.rs:72:9 - | -LL | a - | ^ not a value -... -LL | module!().g::<()>(); // no `help` here! - | --------- in this macro invocation - | - = note: this error originates in the macro `module` (in Nightly builds, run with -Z macro-backtrace for more info) - -error[E0423]: expected value, found module `a` - --> $DIR/suggest-path-instead-of-mod-dot-item.rs:72:9 - | -LL | a - | ^ not a value -... -LL | module!().g; // no `help` here! - | --------- in this macro invocation - | - = note: this error originates in the macro `module` (in Nightly builds, run with -Z macro-backtrace for more info) - -error[E0423]: expected value, found module `a` - --> $DIR/suggest-path-instead-of-mod-dot-item.rs:80:9 - | -LL | a.f() - | ^ -... -LL | let _ = create!(method); - | --------------- in this macro invocation - | - = note: this error originates in the macro `create` (in Nightly builds, run with -Z macro-backtrace for more info) -help: use the path separator to refer to an item - | -LL - a.f() -LL + a::f() - | - -error[E0423]: expected value, found module `a` - --> $DIR/suggest-path-instead-of-mod-dot-item.rs:85:9 - | -LL | a.f - | ^ -... -LL | let _ = create!(field); - | -------------- in this macro invocation - | - = note: this error originates in the macro `create` (in Nightly builds, run with -Z macro-backtrace for more info) -help: use the path separator to refer to an item - | -LL - a.f -LL + a::f - | - -error: aborting due to 13 previous errors - -For more information about this error, try `rustc --explain E0423`. diff --git a/tests/ui/resolve/suggestions/auxiliary/suggest-constructor-cycle-error.rs b/tests/ui/resolve/suggestions/auxiliary/suggest-constructor-cycle-error.rs new file mode 100644 index 00000000000..8de68c38bc3 --- /dev/null +++ b/tests/ui/resolve/suggestions/auxiliary/suggest-constructor-cycle-error.rs @@ -0,0 +1,12 @@ +mod m { + pub struct Uuid(()); + + impl Uuid { + pub fn encode_buffer() -> [u8; LENGTH] { + [] + } + } + const LENGTH: usize = 0; +} + +pub use m::Uuid; diff --git a/tests/ui/resolve/suggestions/suggest-builder-fn.rs b/tests/ui/resolve/suggestions/suggest-builder-fn.rs new file mode 100644 index 00000000000..959675ef2c9 --- /dev/null +++ b/tests/ui/resolve/suggestions/suggest-builder-fn.rs @@ -0,0 +1,64 @@ +// Tests that we suggest the right alternatives when +// a builder method cannot be resolved + +use std::net::TcpStream; + +trait SomeTrait {} + +struct Foo { + v : T +} + +impl Foo { + // Should not be suggested if constraint on the impl not met + fn new() -> Self { + Self { v: T::default() } + } +} + +struct Bar; + +impl Bar { + // Should be suggested + fn build() -> Self { + Self {} + } + + // Method with self can't be a builder. + // Should not be suggested + fn build_with_self(&self) -> Self { + Self {} + } +} + +mod SomeMod { + use crate::Bar; + + impl Bar { + // Public method. Should be suggested + pub fn build_public() -> Self { + Self {} + } + + // Private method. Should not be suggested + fn build_private() -> Self { + Self {} + } + } +} + +fn main() { + // `new` not found on `TcpStream` and `connect` should be suggested + let _stream = TcpStream::new(); + //~^ ERROR no function or associated item named `new` found + + // Although `new` is found on `>` it should not be + // suggested because `u8` does not meet the `T: SomeTrait` constraint + let _foo = Foo::::new(); + //~^ ERROR the function or associated item `new` exists for struct `Foo`, but its trait bounds were not satisfied + + // Should suggest only `::build()` and `SomeMod::::build_public()`. + // Other methods should not suggested because they are private or are not a builder + let _bar = Bar::new(); + //~^ ERROR no function or associated item named `new` found +} diff --git a/tests/ui/resolve/suggestions/suggest-builder-fn.stderr b/tests/ui/resolve/suggestions/suggest-builder-fn.stderr new file mode 100644 index 00000000000..9c5eed35ccf --- /dev/null +++ b/tests/ui/resolve/suggestions/suggest-builder-fn.stderr @@ -0,0 +1,51 @@ +error[E0599]: no function or associated item named `new` found for struct `TcpStream` in the current scope + --> $DIR/suggest-builder-fn.rs:52:29 + | +LL | let _stream = TcpStream::new(); + | ^^^ function or associated item not found in `TcpStream` + | +note: if you're trying to build a new `TcpStream` consider using one of the following associated functions: + TcpStream::connect + TcpStream::connect_timeout + --> $SRC_DIR/std/src/net/tcp.rs:LL:COL + +error[E0599]: the function or associated item `new` exists for struct `Foo`, but its trait bounds were not satisfied + --> $DIR/suggest-builder-fn.rs:57:27 + | +LL | struct Foo { + | ------------- function or associated item `new` not found for this struct +... +LL | let _foo = Foo::::new(); + | ^^^ function or associated item cannot be called on `Foo` due to unsatisfied trait bounds + | +note: trait bound `u8: SomeTrait` was not satisfied + --> $DIR/suggest-builder-fn.rs:12:9 + | +LL | impl Foo { + | ^^^^^^^^^ ------ + | | + | unsatisfied trait bound introduced here + +error[E0599]: no function or associated item named `new` found for struct `Bar` in the current scope + --> $DIR/suggest-builder-fn.rs:62:21 + | +LL | struct Bar; + | ---------- function or associated item `new` not found for this struct +... +LL | let _bar = Bar::new(); + | ^^^ function or associated item not found in `Bar` + | +note: if you're trying to build a new `Bar` consider using one of the following associated functions: + Bar::build + SomeMod::::build_public + --> $DIR/suggest-builder-fn.rs:23:5 + | +LL | fn build() -> Self { + | ^^^^^^^^^^^^^^^^^^ +... +LL | pub fn build_public() -> Self { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0599`. diff --git a/tests/ui/resolve/suggestions/suggest-constructor-cycle-error.rs b/tests/ui/resolve/suggestions/suggest-constructor-cycle-error.rs new file mode 100644 index 00000000000..c23d6788eef --- /dev/null +++ b/tests/ui/resolve/suggestions/suggest-constructor-cycle-error.rs @@ -0,0 +1,10 @@ +//@ aux-build:suggest-constructor-cycle-error.rs + +// Regression test for https://github.com/rust-lang/rust/issues/119625 + +extern crate suggest_constructor_cycle_error as a; + +const CONST_NAME: a::Uuid = a::Uuid(()); +//~^ ERROR: cannot initialize a tuple struct which contains private fields [E0423] + +fn main() {} diff --git a/tests/ui/resolve/suggestions/suggest-constructor-cycle-error.stderr b/tests/ui/resolve/suggestions/suggest-constructor-cycle-error.stderr new file mode 100644 index 00000000000..c6ec2465a43 --- /dev/null +++ b/tests/ui/resolve/suggestions/suggest-constructor-cycle-error.stderr @@ -0,0 +1,15 @@ +error[E0423]: cannot initialize a tuple struct which contains private fields + --> $DIR/suggest-constructor-cycle-error.rs:7:29 + | +LL | const CONST_NAME: a::Uuid = a::Uuid(()); + | ^^^^^^^ + | +note: constructor is not visible here due to private fields + --> $DIR/auxiliary/suggest-constructor-cycle-error.rs:2:21 + | +LL | pub struct Uuid(()); + | ^^ private field + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0423`. diff --git a/tests/ui/resolve/suggestions/suggest-import-without-clobbering-attrs.fixed b/tests/ui/resolve/suggestions/suggest-import-without-clobbering-attrs.fixed new file mode 100644 index 00000000000..607c9af4927 --- /dev/null +++ b/tests/ui/resolve/suggestions/suggest-import-without-clobbering-attrs.fixed @@ -0,0 +1,16 @@ +//@ run-rustfix +//@ compile-flags: -Aunused + +use y::z; +#[cfg(all())] +use y::Whatever; + +mod y { + pub(crate) fn z() {} + pub(crate) struct Whatever; +} + +fn main() { + z(); + //~^ ERROR cannot find function `z` in this scope +} diff --git a/tests/ui/resolve/suggestions/suggest-import-without-clobbering-attrs.rs b/tests/ui/resolve/suggestions/suggest-import-without-clobbering-attrs.rs new file mode 100644 index 00000000000..6cc53fb1086 --- /dev/null +++ b/tests/ui/resolve/suggestions/suggest-import-without-clobbering-attrs.rs @@ -0,0 +1,15 @@ +//@ run-rustfix +//@ compile-flags: -Aunused + +#[cfg(all())] +use y::Whatever; + +mod y { + pub(crate) fn z() {} + pub(crate) struct Whatever; +} + +fn main() { + z(); + //~^ ERROR cannot find function `z` in this scope +} diff --git a/tests/ui/resolve/suggestions/suggest-import-without-clobbering-attrs.stderr b/tests/ui/resolve/suggestions/suggest-import-without-clobbering-attrs.stderr new file mode 100644 index 00000000000..de65d695dd2 --- /dev/null +++ b/tests/ui/resolve/suggestions/suggest-import-without-clobbering-attrs.stderr @@ -0,0 +1,14 @@ +error[E0425]: cannot find function `z` in this scope + --> $DIR/suggest-import-without-clobbering-attrs.rs:13:5 + | +LL | z(); + | ^ not found in this scope + | +help: consider importing this function + | +LL + use y::z; + | + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0425`. diff --git a/tests/ui/resolve/suggestions/suggest-path-for-tuple-struct.rs b/tests/ui/resolve/suggestions/suggest-path-for-tuple-struct.rs new file mode 100644 index 00000000000..c8bc3e79fe2 --- /dev/null +++ b/tests/ui/resolve/suggestions/suggest-path-for-tuple-struct.rs @@ -0,0 +1,26 @@ +mod module { + pub struct SomeTupleStruct(u8); + pub struct SomeRegularStruct { + foo: u8 + } + + impl SomeTupleStruct { + pub fn new() -> Self { + Self(0) + } + } + impl SomeRegularStruct { + pub fn new() -> Self { + Self { foo: 0 } + } + } +} + +use module::{SomeTupleStruct, SomeRegularStruct}; + +fn main() { + let _ = SomeTupleStruct.new(); + //~^ ERROR expected value, found struct `SomeTupleStruct` + let _ = SomeRegularStruct.new(); + //~^ ERROR expected value, found struct `SomeRegularStruct` +} diff --git a/tests/ui/resolve/suggestions/suggest-path-for-tuple-struct.stderr b/tests/ui/resolve/suggestions/suggest-path-for-tuple-struct.stderr new file mode 100644 index 00000000000..68a5b550978 --- /dev/null +++ b/tests/ui/resolve/suggestions/suggest-path-for-tuple-struct.stderr @@ -0,0 +1,27 @@ +error[E0423]: expected value, found struct `SomeTupleStruct` + --> $DIR/suggest-path-for-tuple-struct.rs:22:13 + | +LL | let _ = SomeTupleStruct.new(); + | ^^^^^^^^^^^^^^^ + | +help: use the path separator to refer to an item + | +LL - let _ = SomeTupleStruct.new(); +LL + let _ = SomeTupleStruct::new(); + | + +error[E0423]: expected value, found struct `SomeRegularStruct` + --> $DIR/suggest-path-for-tuple-struct.rs:24:13 + | +LL | let _ = SomeRegularStruct.new(); + | ^^^^^^^^^^^^^^^^^ + | +help: use the path separator to refer to an item + | +LL - let _ = SomeRegularStruct.new(); +LL + let _ = SomeRegularStruct::new(); + | + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0423`. diff --git a/tests/ui/resolve/suggestions/suggest-path-instead-of-mod-dot-item.rs b/tests/ui/resolve/suggestions/suggest-path-instead-of-mod-dot-item.rs new file mode 100644 index 00000000000..d5d6b13d62c --- /dev/null +++ b/tests/ui/resolve/suggestions/suggest-path-instead-of-mod-dot-item.rs @@ -0,0 +1,111 @@ +// Beginners write `mod.item` when they should write `mod::item`. +// This tests that we suggest the latter when we encounter the former. + +pub mod a { + pub const I: i32 = 1; + + pub fn f() -> i32 { 2 } + + pub mod b { + pub const J: i32 = 3; + + pub fn g() -> i32 { 4 } + } +} + +fn h1() -> i32 { + a.I + //~^ ERROR expected value, found module `a` + //~| HELP use the path separator +} + +fn h2() -> i32 { + a.g() + //~^ ERROR expected value, found module `a` + //~| HELP use the path separator +} + +fn h3() -> i32 { + a.b.J + //~^ ERROR expected value, found module `a` + //~| HELP use the path separator +} + +fn h4() -> i32 { + a::b.J + //~^ ERROR expected value, found module `a::b` + //~| HELP a constant with a similar name exists + //~| HELP use the path separator +} + +fn h5() { + a.b.f(); + //~^ ERROR expected value, found module `a` + //~| HELP use the path separator + let v = Vec::new(); + v.push(a::b); + //~^ ERROR expected value, found module `a::b` + //~| HELP a constant with a similar name exists +} + +fn h6() -> i32 { + a::b.f() + //~^ ERROR expected value, found module `a::b` + //~| HELP a constant with a similar name exists + //~| HELP use the path separator +} + +fn h7() { + a::b + //~^ ERROR expected value, found module `a::b` + //~| HELP a constant with a similar name exists +} + +fn h8() -> i32 { + a::b() + //~^ ERROR expected function, found module `a::b` + //~| HELP a constant with a similar name exists +} + +macro_rules! module { + () => { + a + //~^ ERROR expected value, found module `a` + //~| ERROR expected value, found module `a` + }; +} + +macro_rules! create { + (method) => { + a.f() + //~^ ERROR expected value, found module `a` + //~| HELP use the path separator + }; + (field) => { + a.f + //~^ ERROR expected value, found module `a` + //~| HELP use the path separator + }; +} + +fn h9() { + // + // Note that if the receiver is a macro call, we do not want to suggest to replace + // `.` with `::` as that would be a syntax error. + // Since the receiver is a module and not a type, we cannot suggest to surround + // it with angle brackets. + // + + module!().g::<()>(); // no `help` here! + + module!().g; // no `help` here! + + // + // Ensure that the suggestion is shown for expressions inside of macro definitions. + // + + let _ = create!(method); + let _ = create!(field); +} + +fn main() {} diff --git a/tests/ui/resolve/suggestions/suggest-path-instead-of-mod-dot-item.stderr b/tests/ui/resolve/suggestions/suggest-path-instead-of-mod-dot-item.stderr new file mode 100644 index 00000000000..5db943cd10d --- /dev/null +++ b/tests/ui/resolve/suggestions/suggest-path-instead-of-mod-dot-item.stderr @@ -0,0 +1,178 @@ +error[E0423]: expected value, found module `a` + --> $DIR/suggest-path-instead-of-mod-dot-item.rs:17:5 + | +LL | a.I + | ^ + | +help: use the path separator to refer to an item + | +LL - a.I +LL + a::I + | + +error[E0423]: expected value, found module `a` + --> $DIR/suggest-path-instead-of-mod-dot-item.rs:23:5 + | +LL | a.g() + | ^ + | +help: use the path separator to refer to an item + | +LL - a.g() +LL + a::g() + | + +error[E0423]: expected value, found module `a` + --> $DIR/suggest-path-instead-of-mod-dot-item.rs:29:5 + | +LL | a.b.J + | ^ + | +help: use the path separator to refer to an item + | +LL - a.b.J +LL + a::b.J + | + +error[E0423]: expected value, found module `a::b` + --> $DIR/suggest-path-instead-of-mod-dot-item.rs:35:5 + | +LL | pub const I: i32 = 1; + | --------------------- similarly named constant `I` defined here +... +LL | a::b.J + | ^^^^ + | +help: use the path separator to refer to an item + | +LL - a::b.J +LL + a::b::J + | +help: a constant with a similar name exists + | +LL - a::b.J +LL + a::I.J + | + +error[E0423]: expected value, found module `a` + --> $DIR/suggest-path-instead-of-mod-dot-item.rs:42:5 + | +LL | a.b.f(); + | ^ + | +help: use the path separator to refer to an item + | +LL - a.b.f(); +LL + a::b.f(); + | + +error[E0423]: expected value, found module `a::b` + --> $DIR/suggest-path-instead-of-mod-dot-item.rs:46:12 + | +LL | pub const I: i32 = 1; + | --------------------- similarly named constant `I` defined here +... +LL | v.push(a::b); + | ^^^- + | | + | help: a constant with a similar name exists: `I` + +error[E0423]: expected value, found module `a::b` + --> $DIR/suggest-path-instead-of-mod-dot-item.rs:52:5 + | +LL | pub const I: i32 = 1; + | --------------------- similarly named constant `I` defined here +... +LL | a::b.f() + | ^^^^ + | +help: use the path separator to refer to an item + | +LL - a::b.f() +LL + a::b::f() + | +help: a constant with a similar name exists + | +LL - a::b.f() +LL + a::I.f() + | + +error[E0423]: expected value, found module `a::b` + --> $DIR/suggest-path-instead-of-mod-dot-item.rs:59:5 + | +LL | pub const I: i32 = 1; + | --------------------- similarly named constant `I` defined here +... +LL | a::b + | ^^^- + | | + | help: a constant with a similar name exists: `I` + +error[E0423]: expected function, found module `a::b` + --> $DIR/suggest-path-instead-of-mod-dot-item.rs:65:5 + | +LL | pub const I: i32 = 1; + | --------------------- similarly named constant `I` defined here +... +LL | a::b() + | ^^^- + | | + | help: a constant with a similar name exists: `I` + +error[E0423]: expected value, found module `a` + --> $DIR/suggest-path-instead-of-mod-dot-item.rs:72:9 + | +LL | a + | ^ not a value +... +LL | module!().g::<()>(); // no `help` here! + | --------- in this macro invocation + | + = note: this error originates in the macro `module` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0423]: expected value, found module `a` + --> $DIR/suggest-path-instead-of-mod-dot-item.rs:72:9 + | +LL | a + | ^ not a value +... +LL | module!().g; // no `help` here! + | --------- in this macro invocation + | + = note: this error originates in the macro `module` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0423]: expected value, found module `a` + --> $DIR/suggest-path-instead-of-mod-dot-item.rs:80:9 + | +LL | a.f() + | ^ +... +LL | let _ = create!(method); + | --------------- in this macro invocation + | + = note: this error originates in the macro `create` (in Nightly builds, run with -Z macro-backtrace for more info) +help: use the path separator to refer to an item + | +LL - a.f() +LL + a::f() + | + +error[E0423]: expected value, found module `a` + --> $DIR/suggest-path-instead-of-mod-dot-item.rs:85:9 + | +LL | a.f + | ^ +... +LL | let _ = create!(field); + | -------------- in this macro invocation + | + = note: this error originates in the macro `create` (in Nightly builds, run with -Z macro-backtrace for more info) +help: use the path separator to refer to an item + | +LL - a.f +LL + a::f + | + +error: aborting due to 13 previous errors + +For more information about this error, try `rustc --explain E0423`. -- cgit 1.4.1-3-g733a5 From 000f038e9999ebdbaf3167c42dadc3db872a5d3f Mon Sep 17 00:00:00 2001 From: xizheyin Date: Sun, 29 Jun 2025 15:38:09 +0800 Subject: Add ui test resolve/false-self-in-macro-issue-143134.rs Signed-off-by: xizheyin --- tests/ui/resolve/false-self-in-macro-issue-143134.rs | 10 ++++++++++ tests/ui/resolve/false-self-in-macro-issue-143134.stderr | 12 ++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 tests/ui/resolve/false-self-in-macro-issue-143134.rs create mode 100644 tests/ui/resolve/false-self-in-macro-issue-143134.stderr (limited to 'tests') diff --git a/tests/ui/resolve/false-self-in-macro-issue-143134.rs b/tests/ui/resolve/false-self-in-macro-issue-143134.rs new file mode 100644 index 00000000000..0983b8b3dc3 --- /dev/null +++ b/tests/ui/resolve/false-self-in-macro-issue-143134.rs @@ -0,0 +1,10 @@ +trait T { + fn f(self); + } + impl T for () { + fn f(self) { + let self = (); //~ ERROR expected unit struct, unit variant or constant, found local variable `self` + } +} + +fn main() {} diff --git a/tests/ui/resolve/false-self-in-macro-issue-143134.stderr b/tests/ui/resolve/false-self-in-macro-issue-143134.stderr new file mode 100644 index 00000000000..43e77e183ae --- /dev/null +++ b/tests/ui/resolve/false-self-in-macro-issue-143134.stderr @@ -0,0 +1,12 @@ +error[E0424]: expected unit struct, unit variant or constant, found local variable `self` + --> $DIR/false-self-in-macro-issue-143134.rs:6:13 + | +LL | / fn f(self) { +LL | | let self = (); + | | ^^^^ `self` value is a keyword and may not be bound to variables or shadowed +LL | | } + | |_____- this function has a `self` parameter, but a macro invocation can only access identifiers it receives from parameters + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0424`. -- cgit 1.4.1-3-g733a5 From 236b392904d234d9ec2968c55ae20132405a555d Mon Sep 17 00:00:00 2001 From: xizheyin Date: Sun, 29 Jun 2025 16:10:52 +0800 Subject: Return early when `self` resolve failure because of `let self = ...` Signed-off-by: xizheyin --- compiler/rustc_resolve/src/late/diagnostics.rs | 16 ++++++++++++---- tests/ui/error-codes/E0424.stderr | 2 -- tests/ui/resolve/false-self-in-macro-issue-143134.stderr | 7 ++----- 3 files changed, 14 insertions(+), 11 deletions(-) (limited to 'tests') diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index e7b8c988cd4..0b78835326f 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -1183,15 +1183,23 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> { _ => "`self` value is a keyword only available in methods with a `self` parameter", }, ); + + // using `let self` is wrong even if we're not in an associated method or if we're in a macro expansion. + // So, we should return early if we're in a pattern, see issue #143134. + if matches!(source, PathSource::Pat) { + return true; + } + let is_assoc_fn = self.self_type_is_available(); let self_from_macro = "a `self` parameter, but a macro invocation can only \ access identifiers it receives from parameters"; - if let Some((fn_kind, span)) = &self.diag_metadata.current_function { + if let Some((fn_kind, fn_span)) = &self.diag_metadata.current_function { // The current function has a `self` parameter, but we were unable to resolve // a reference to `self`. This can only happen if the `self` identifier we - // are resolving came from a different hygiene context. + // are resolving came from a different hygiene context or a variable binding. + // But variable binding error is returned early above. if fn_kind.decl().inputs.get(0).is_some_and(|p| p.is_self()) { - err.span_label(*span, format!("this function has {self_from_macro}")); + err.span_label(*fn_span, format!("this function has {self_from_macro}")); } else { let doesnt = if is_assoc_fn { let (span, sugg) = fn_kind @@ -1204,7 +1212,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> { // This avoids placing the suggestion into the visibility specifier. let span = fn_kind .ident() - .map_or(*span, |ident| span.with_lo(ident.span.hi())); + .map_or(*fn_span, |ident| fn_span.with_lo(ident.span.hi())); ( self.r .tcx diff --git a/tests/ui/error-codes/E0424.stderr b/tests/ui/error-codes/E0424.stderr index d02da3e4ecb..831a070bf6c 100644 --- a/tests/ui/error-codes/E0424.stderr +++ b/tests/ui/error-codes/E0424.stderr @@ -40,8 +40,6 @@ LL | fn qux(&self) { error[E0424]: expected unit struct, unit variant or constant, found module `self` --> $DIR/E0424.rs:20:9 | -LL | fn main () { - | ---- this function can't have a `self` parameter LL | let self = "self"; | ^^^^ `self` value is a keyword and may not be bound to variables or shadowed diff --git a/tests/ui/resolve/false-self-in-macro-issue-143134.stderr b/tests/ui/resolve/false-self-in-macro-issue-143134.stderr index 43e77e183ae..48c979575ea 100644 --- a/tests/ui/resolve/false-self-in-macro-issue-143134.stderr +++ b/tests/ui/resolve/false-self-in-macro-issue-143134.stderr @@ -1,11 +1,8 @@ error[E0424]: expected unit struct, unit variant or constant, found local variable `self` --> $DIR/false-self-in-macro-issue-143134.rs:6:13 | -LL | / fn f(self) { -LL | | let self = (); - | | ^^^^ `self` value is a keyword and may not be bound to variables or shadowed -LL | | } - | |_____- this function has a `self` parameter, but a macro invocation can only access identifiers it receives from parameters +LL | let self = (); + | ^^^^ `self` value is a keyword and may not be bound to variables or shadowed error: aborting due to 1 previous error -- cgit 1.4.1-3-g733a5