Skip to content

Commit 83f147b

Browse files
committed
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
2 parents 7b10746 + 564cb87 commit 83f147b

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1026,9 +1026,15 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
10261026

10271027
self.suggest_using_enum_variant(err, source, def_id, span);
10281028
}
1029-
(Res::Def(DefKind::Struct, def_id), _) if ns == ValueNS => {
1029+
(Res::Def(DefKind::Struct, def_id), source) if ns == ValueNS => {
10301030
let (ctor_def, ctor_vis, fields) =
10311031
if let Some(struct_ctor) = self.r.struct_constructors.get(&def_id).cloned() {
1032+
if let PathSource::Expr(Some(parent)) = source {
1033+
if let ExprKind::Field(..) | ExprKind::MethodCall(..) = parent.kind {
1034+
bad_struct_syntax_suggestion(def_id);
1035+
return true;
1036+
}
1037+
}
10321038
struct_ctor
10331039
} else {
10341040
bad_struct_syntax_suggestion(def_id);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
mod module {
2+
pub struct SomeTupleStruct(u8);
3+
pub struct SomeRegularStruct {
4+
foo: u8
5+
}
6+
7+
impl SomeTupleStruct {
8+
pub fn new() -> Self {
9+
Self(0)
10+
}
11+
}
12+
impl SomeRegularStruct {
13+
pub fn new() -> Self {
14+
Self { foo: 0 }
15+
}
16+
}
17+
}
18+
19+
use module::{SomeTupleStruct, SomeRegularStruct};
20+
21+
fn main() {
22+
let _ = SomeTupleStruct.new();
23+
//~^ ERROR expected value, found struct `SomeTupleStruct`
24+
let _ = SomeRegularStruct.new();
25+
//~^ ERROR expected value, found struct `SomeRegularStruct`
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0423]: expected value, found struct `SomeTupleStruct`
2+
--> $DIR/suggest-path-for-tuple-struct.rs:22:13
3+
|
4+
LL | let _ = SomeTupleStruct.new();
5+
| ^^^^^^^^^^^^^^^----
6+
| |
7+
| help: use the path separator to refer to an item: `SomeTupleStruct::new`
8+
9+
error[E0423]: expected value, found struct `SomeRegularStruct`
10+
--> $DIR/suggest-path-for-tuple-struct.rs:24:13
11+
|
12+
LL | let _ = SomeRegularStruct.new();
13+
| ^^^^^^^^^^^^^^^^^----
14+
| |
15+
| help: use the path separator to refer to an item: `SomeRegularStruct::new`
16+
17+
error: aborting due to 2 previous errors
18+
19+
For more information about this error, try `rustc --explain E0423`.

0 commit comments

Comments
 (0)