1
Fork 0

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.
This commit is contained in:
Zachary S 2025-01-30 16:28:00 -06:00
parent 3b022d8cee
commit fe37adab4b
4 changed files with 69 additions and 19 deletions

View file

@ -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 {

View file

@ -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

View file

@ -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 {

View file

@ -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`.