diff options
| author | Zachary S <zasample18+github@gmail.com> | 2025-01-30 16:28:00 -0600 |
|---|---|---|
| committer | Zachary S <zasample18+github@gmail.com> | 2025-02-18 13:11:37 -0600 |
| commit | fe37adab4b378d68eda8a8893339606d7f381465 (patch) | |
| tree | 9eb168ce9e4cb70f05d0978e96d5abb2c88b0ec9 | |
| parent | 3b022d8ceea570db9730be34d964f0cc663a567f (diff) | |
| download | rust-fe37adab4b378d68eda8a8893339606d7f381465.tar.gz rust-fe37adab4b378d68eda8a8893339606d7f381465.zip | |
Suggest using :: instead of . in more cases.
When `Foo.field` or `Foo.method()` exprs are encountered, suggest `Foo::field` or `Foo::method()` when Foo is a type alias, not just a struct, trait, or module. Also rename test for this suggestion from issue-22692.rs to something more meaningful.
| -rw-r--r-- | compiler/rustc_resolve/src/late/diagnostics.rs | 16 | ||||
| -rw-r--r-- | src/tools/tidy/src/issues.txt | 1 | ||||
| -rw-r--r-- | tests/ui/resolve/dot-notation-type-namespace-suggest-path-sep.rs (renamed from tests/ui/resolve/issue-22692.rs) | 20 | ||||
| -rw-r--r-- | tests/ui/resolve/dot-notation-type-namespace-suggest-path-sep.stderr (renamed from tests/ui/resolve/issue-22692.stderr) | 51 |
4 files changed, 69 insertions, 19 deletions
diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 0962865e7f1..524915b44e7 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -1566,7 +1566,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> { } }; - let mut bad_struct_syntax_suggestion = |this: &mut Self, def_id: DefId| { + let bad_struct_syntax_suggestion = |this: &mut Self, err: &mut Diag<'_>, def_id: DefId| { let (followed_by_brace, closing_brace) = this.followed_by_brace(span); match source { @@ -1740,12 +1740,10 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> { } } ( - Res::Def(kind @ (DefKind::Mod | DefKind::Trait), _), + Res::Def(kind @ (DefKind::Mod | DefKind::Trait | DefKind::TyAlias), _), PathSource::Expr(Some(parent)), - ) => { - if !path_sep(self, err, parent, kind) { - return false; - } + ) if path_sep(self, err, parent, kind) => { + return true; } ( Res::Def(DefKind::Enum, def_id), @@ -1777,13 +1775,13 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> { let (ctor_def, ctor_vis, fields) = if let Some(struct_ctor) = struct_ctor { if let PathSource::Expr(Some(parent)) = source { if let ExprKind::Field(..) | ExprKind::MethodCall(..) = parent.kind { - bad_struct_syntax_suggestion(self, def_id); + bad_struct_syntax_suggestion(self, err, def_id); return true; } } struct_ctor } else { - bad_struct_syntax_suggestion(self, def_id); + bad_struct_syntax_suggestion(self, err, def_id); return true; }; @@ -1861,7 +1859,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> { err.span_label(span, "constructor is not visible here due to private fields"); } (Res::Def(DefKind::Union | DefKind::Variant, def_id), _) if ns == ValueNS => { - bad_struct_syntax_suggestion(self, def_id); + bad_struct_syntax_suggestion(self, err, def_id); } (Res::Def(DefKind::Ctor(_, CtorKind::Const), def_id), _) if ns == ValueNS => { match source { diff --git a/src/tools/tidy/src/issues.txt b/src/tools/tidy/src/issues.txt index 39c9a148e9e..1cb47353469 100644 --- a/src/tools/tidy/src/issues.txt +++ b/src/tools/tidy/src/issues.txt @@ -3619,7 +3619,6 @@ ui/resolve/issue-21221-1.rs ui/resolve/issue-21221-2.rs ui/resolve/issue-21221-3.rs ui/resolve/issue-21221-4.rs -ui/resolve/issue-22692.rs ui/resolve/issue-2330.rs ui/resolve/issue-23305.rs ui/resolve/issue-2356.rs diff --git a/tests/ui/resolve/issue-22692.rs b/tests/ui/resolve/dot-notation-type-namespace-suggest-path-sep.rs index 31a76261408..104d11f685c 100644 --- a/tests/ui/resolve/issue-22692.rs +++ b/tests/ui/resolve/dot-notation-type-namespace-suggest-path-sep.rs @@ -1,3 +1,11 @@ +// see also https://github.com/rust-lang/rust/issues/22692 + +type Alias = Vec<u32>; + +mod foo { + fn bar() {} +} + fn main() { let _ = String.new(); //~^ ERROR expected value, found struct `String` @@ -10,6 +18,18 @@ fn main() { let _ = Vec::<()>.with_capacity(1); //~^ ERROR expected value, found struct `Vec` //~| HELP use the path separator + + let _ = Alias.new(); + //~^ ERROR expected value, found type alias `Alias` + //~| HELP use the path separator + + let _ = Alias.default; + //~^ ERROR expected value, found type alias `Alias` + //~| HELP use the path separator + + let _ = foo.bar; + //~^ ERROR expected value, found module `foo` + //~| HELP use the path separator } macro_rules! Type { diff --git a/tests/ui/resolve/issue-22692.stderr b/tests/ui/resolve/dot-notation-type-namespace-suggest-path-sep.stderr index 546f12b35c1..a5397306b01 100644 --- a/tests/ui/resolve/issue-22692.stderr +++ b/tests/ui/resolve/dot-notation-type-namespace-suggest-path-sep.stderr @@ -1,5 +1,5 @@ error[E0423]: expected value, found struct `String` - --> $DIR/issue-22692.rs:2:13 + --> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:10:13 | LL | let _ = String.new(); | ^^^^^^ @@ -11,7 +11,7 @@ LL + let _ = String::new(); | error[E0423]: expected value, found struct `String` - --> $DIR/issue-22692.rs:6:13 + --> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:14:13 | LL | let _ = String.default; | ^^^^^^ @@ -23,7 +23,7 @@ LL + let _ = String::default; | error[E0423]: expected value, found struct `Vec` - --> $DIR/issue-22692.rs:10:13 + --> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:18:13 | LL | let _ = Vec::<()>.with_capacity(1); | ^^^^^^^^^ @@ -34,8 +34,41 @@ LL - let _ = Vec::<()>.with_capacity(1); LL + let _ = Vec::<()>::with_capacity(1); | +error[E0423]: expected value, found type alias `Alias` + --> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:22:13 + | +LL | let _ = Alias.new(); + | ^^^^^ + | +help: use the path separator to refer to an item + | +LL | let _ = Alias::new(); + | ~~ + +error[E0423]: expected value, found type alias `Alias` + --> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:26:13 + | +LL | let _ = Alias.default; + | ^^^^^ + | +help: use the path separator to refer to an item + | +LL | let _ = Alias::default; + | ~~ + +error[E0423]: expected value, found module `foo` + --> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:30:13 + | +LL | let _ = foo.bar; + | ^^^ + | +help: use the path separator to refer to an item + | +LL | let _ = foo::bar; + | ~~ + error[E0423]: expected value, found struct `std::cell::Cell` - --> $DIR/issue-22692.rs:17:9 + --> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:37:9 | LL | ::std::cell::Cell | ^^^^^^^^^^^^^^^^^ @@ -51,7 +84,7 @@ LL + <Type!()>::get(); | error[E0423]: expected value, found struct `std::cell::Cell` - --> $DIR/issue-22692.rs:17:9 + --> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:37:9 | LL | ::std::cell::Cell | ^^^^^^^^^^^^^^^^^ @@ -67,7 +100,7 @@ LL + <Type! {}>::get; | error[E0423]: expected value, found struct `Vec` - --> $DIR/issue-22692.rs:26:9 + --> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:46:9 | LL | Vec.new() | ^^^ @@ -83,7 +116,7 @@ LL + Vec::new() | error[E0423]: expected value, found struct `Vec` - --> $DIR/issue-22692.rs:31:9 + --> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:51:9 | LL | Vec.new | ^^^ @@ -99,7 +132,7 @@ LL + Vec::new | error[E0423]: expected value, found struct `std::cell::Cell` - --> $DIR/issue-22692.rs:17:9 + --> $DIR/dot-notation-type-namespace-suggest-path-sep.rs:37:9 | LL | ::std::cell::Cell | ^^^^^^^^^^^^^^^^^ @@ -114,6 +147,6 @@ LL - Type!().new(0) LL + <Type!()>::new(0) | -error: aborting due to 8 previous errors +error: aborting due to 11 previous errors For more information about this error, try `rustc --explain E0423`. |
