Skip to content

Commit

Permalink
Auto merge of #89293 - TaKO8Ki:fix-confusing-error-for-path-separator…
Browse files Browse the repository at this point in the history
…-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
  • Loading branch information
bors committed Sep 28, 2021
2 parents 7b10746 + 564cb87 commit 83f147b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
8 changes: 7 additions & 1 deletion compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
26 changes: 26 additions & 0 deletions src/test/ui/resolve/suggest-path-for-tuple-struct.rs
Original file line number Diff line number Diff line change
@@ -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`
}
19 changes: 19 additions & 0 deletions src/test/ui/resolve/suggest-path-for-tuple-struct.stderr
Original file line number Diff line number Diff line change
@@ -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`.

0 comments on commit 83f147b

Please sign in to comment.