diff options
| author | bors <bors@rust-lang.org> | 2021-09-28 08:58:41 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2021-09-28 08:58:41 +0000 |
| commit | 83f147b3baf21acfc367a6da1045d212cd3957e4 (patch) | |
| tree | cfde71072b82197f6f93ba50c16abef38054706c | |
| parent | 7b10746ef08041885989eccd2dd6cd3c2f6f0f49 (diff) | |
| parent | 564cb87e270f70310e25ae9f459c642e949e8213 (diff) | |
| download | rust-83f147b3baf21acfc367a6da1045d212cd3957e4.tar.gz rust-83f147b3baf21acfc367a6da1045d212cd3957e4.zip | |
Auto merge of #89293 - TaKO8Ki:fix-confusing-error-for-path-separator-to-refer-to-an-struct-item, r=estebank
Suggest using the path separator for tuple struct Fix confusing error message `constructor is not visible here due to private fields` for tuple struct closes #83450
| -rw-r--r-- | compiler/rustc_resolve/src/late/diagnostics.rs | 8 | ||||
| -rw-r--r-- | src/test/ui/resolve/suggest-path-for-tuple-struct.rs | 26 | ||||
| -rw-r--r-- | src/test/ui/resolve/suggest-path-for-tuple-struct.stderr | 19 |
3 files changed, 52 insertions, 1 deletions
diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 19136c6ceeb..84219873d55 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -1026,9 +1026,15 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { self.suggest_using_enum_variant(err, source, def_id, span); } - (Res::Def(DefKind::Struct, def_id), _) if ns == ValueNS => { + (Res::Def(DefKind::Struct, def_id), source) if ns == ValueNS => { let (ctor_def, ctor_vis, fields) = if let Some(struct_ctor) = self.r.struct_constructors.get(&def_id).cloned() { + if let PathSource::Expr(Some(parent)) = source { + if let ExprKind::Field(..) | ExprKind::MethodCall(..) = parent.kind { + bad_struct_syntax_suggestion(def_id); + return true; + } + } struct_ctor } else { bad_struct_syntax_suggestion(def_id); diff --git a/src/test/ui/resolve/suggest-path-for-tuple-struct.rs b/src/test/ui/resolve/suggest-path-for-tuple-struct.rs new file mode 100644 index 00000000000..c8bc3e79fe2 --- /dev/null +++ b/src/test/ui/resolve/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/src/test/ui/resolve/suggest-path-for-tuple-struct.stderr b/src/test/ui/resolve/suggest-path-for-tuple-struct.stderr new file mode 100644 index 00000000000..957045ca74b --- /dev/null +++ b/src/test/ui/resolve/suggest-path-for-tuple-struct.stderr @@ -0,0 +1,19 @@ +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: `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: `SomeRegularStruct::new` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0423`. |
