Skip to content

Commit b2f5cc7

Browse files
Rollup merge of #149098 - Jamesbarford:fix/148919-tuple-struct-lint, r=JonathanBrouwer
Fix error message for calling a non-tuple struct This feels a bit odd checking for `"0"` but I can't see how else to check for it being a Tuple closes #148919
2 parents 629a283 + 3d33942 commit b2f5cc7

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -823,8 +823,16 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
823823
}
824824

825825
if let Some(Res::Def(DefKind::Struct, def_id)) = res {
826-
self.update_err_for_private_tuple_struct_fields(err, &source, def_id);
827-
err.note("constructor is not visible here due to private fields");
826+
let private_fields = self.has_private_fields(def_id);
827+
let adjust_error_message =
828+
private_fields && self.is_struct_with_fn_ctor(def_id);
829+
if adjust_error_message {
830+
self.update_err_for_private_tuple_struct_fields(err, &source, def_id);
831+
}
832+
833+
if private_fields {
834+
err.note("constructor is not visible here due to private fields");
835+
}
828836
} else {
829837
err.span_suggestion(
830838
call_span,
@@ -1642,6 +1650,19 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
16421650
}
16431651
}
16441652

1653+
fn is_struct_with_fn_ctor(&mut self, def_id: DefId) -> bool {
1654+
def_id
1655+
.as_local()
1656+
.and_then(|local_id| self.r.struct_constructors.get(&local_id))
1657+
.map(|struct_ctor| {
1658+
matches!(
1659+
struct_ctor.0,
1660+
def::Res::Def(DefKind::Ctor(CtorOf::Struct, CtorKind::Fn), _)
1661+
)
1662+
})
1663+
.unwrap_or(false)
1664+
}
1665+
16451666
fn update_err_for_private_tuple_struct_fields(
16461667
&mut self,
16471668
err: &mut Diag<'_>,
@@ -1674,7 +1695,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
16741695
*call_span,
16751696
&args[..],
16761697
);
1677-
// Use spans of the tuple struct definition.
1698+
16781699
self.r
16791700
.field_idents(def_id)
16801701
.map(|fields| fields.iter().map(|f| f.span).collect::<Vec<_>>())
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
struct Bar {}
2+
3+
impl Bar {
4+
fn into_self(self) -> Bar {
5+
Bar(self)
6+
//~^ ERROR expected function, tuple struct or tuple variant, found struct `Bar` [E0423]
7+
}
8+
}
9+
10+
fn main() {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0423]: expected function, tuple struct or tuple variant, found struct `Bar`
2+
--> $DIR/regression-struct-called-as-function-148919.rs:5:9
3+
|
4+
LL | Bar(self)
5+
| ^^^
6+
7+
error: aborting due to 1 previous error
8+
9+
For more information about this error, try `rustc --explain E0423`.

0 commit comments

Comments
 (0)