Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
call_expr_and_args.map_or(expr.span, |(e, _)| e.span),
expr.span,
expr.hir_id,
call_expr_and_args.is_some(),
)
.0
}
Expand Down
56 changes: 45 additions & 11 deletions compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
span: Span,
path_span: Span,
hir_id: HirId,
has_args: bool,
) -> (Ty<'tcx>, Res) {
let tcx = self.tcx;

Expand Down Expand Up @@ -1253,17 +1254,50 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
"the `Self` constructor can only be used with tuple or unit structs",
);
if let Some(adt_def) = ty.normalized.ty_adt_def() {
match adt_def.adt_kind() {
AdtKind::Enum => {
err.help("did you mean to use one of the enum's variants?");
}
AdtKind::Struct | AdtKind::Union => {
err.span_suggestion(
span,
"use curly brackets",
"Self { /* fields */ }",
Applicability::HasPlaceholders,
);
let def_id = self.body_def_id.to_def_id();
if !has_args
&& let Some(assoc) = tcx.opt_associated_item(def_id)
&& assoc.is_method()
{
let self_ty =
tcx.fn_sig(def_id).instantiate_identity().skip_binder().inputs()[0];
let applicability = if let ty::Adt(..) = self_ty.kind() {
// We're within a method that takes ownership of `Self`, likely a
// builder, so this is most likely a typo.
Applicability::MachineApplicable
} else {
// We still might have meant `self` instead of `Self`.
Applicability::MaybeIncorrect
};
err.span_suggestion_verbose(
span,
format!(
"you might have meant to refer to the `self` binding of type \
`{self_ty}`",
),
"self".to_string(),
applicability,
);
} else {
match adt_def.adt_kind() {
AdtKind::Enum => {
err.span_help(
tcx.def_span(adt_def.did()),
if adt_def.variants().is_empty() {
"the enum is unconstructable because it has no variants"
} else {
"you might have meant to use one of the enum's variants"
},
);
}
AdtKind::Struct | AdtKind::Union => {
err.span_suggestion_verbose(
span,
"use curly brackets",
"Self { /* fields */ }",
Applicability::HasPlaceholders,
);
}
}
}
}
Expand Down
11 changes: 7 additions & 4 deletions compiler/rustc_hir_typeck/src/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
rustc_hir::PatExprKind::Path(qpath) => {
let (res, opt_ty, segments) =
self.resolve_ty_and_res_fully_qualified_call(qpath, lt.hir_id, lt.span);
self.instantiate_value_path(segments, opt_ty, res, lt.span, lt.span, lt.hir_id).0
self.instantiate_value_path(
segments, opt_ty, res, lt.span, lt.span, lt.hir_id, false,
)
.0
}
};
self.write_ty(lt.hir_id, ty);
Expand Down Expand Up @@ -1624,7 +1627,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

// Find the type of the path pattern, for later checking.
let (pat_ty, pat_res) =
self.instantiate_value_path(segments, opt_ty, res, span, span, path_id);
self.instantiate_value_path(segments, opt_ty, res, span, span, path_id, false);
Ok(ResolvedPat { ty: pat_ty, kind: ResolvedPatKind::Path { res, pat_res, segments } })
}

Expand Down Expand Up @@ -1784,8 +1787,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}

// Type-check the path.
let (pat_ty, res) =
self.instantiate_value_path(segments, opt_ty, res, pat.span, pat.span, pat.hir_id);
let (pat_ty, res) = self
.instantiate_value_path(segments, opt_ty, res, pat.span, pat.span, pat.hir_id, false);
if !pat_ty.is_fn() {
return report_unexpected_res(res);
}
Expand Down
8 changes: 7 additions & 1 deletion tests/ui/structs/invalid-self-constructor-56835.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ error: the `Self` constructor can only be used with tuple or unit structs
--> $DIR/invalid-self-constructor-56835.rs:5:12
|
LL | fn bar(Self(foo): Self) {}
| ^^^^^^^^^ help: use curly brackets: `Self { /* fields */ }`
| ^^^^^^^^^
|
help: use curly brackets
|
LL - fn bar(Self(foo): Self) {}
LL + fn bar(Self { /* fields */ }: Self) {}
|

error[E0164]: expected tuple struct or tuple variant, found self constructor `Self`
--> $DIR/invalid-self-constructor-56835.rs:5:12
Expand Down
31 changes: 31 additions & 0 deletions tests/ui/typeck/self-constructor-type-error-56199.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// https://github.com/rust-lang/rust/issues/56199
enum Foo {}
enum Lab {
Qux,
}
struct Bar {}

impl Foo {
Expand All @@ -9,6 +12,12 @@ impl Foo {
let _ = Self();
//~^ ERROR the `Self` constructor can only be used with tuple or unit structs
}
fn foo_method(self) {
let _ = Self;
//~^ ERROR the `Self` constructor can only be used with tuple or unit structs
let _ = Self();
//~^ ERROR the `Self` constructor can only be used with tuple or unit structs
}
}

impl Bar {
Expand All @@ -18,6 +27,28 @@ impl Bar {
let _ = Self();
//~^ ERROR the `Self` constructor can only be used with tuple or unit structs
}
fn bar_method(self) {
let _ = Self;
//~^ ERROR the `Self` constructor can only be used with tuple or unit structs
let _ = Self();
//~^ ERROR the `Self` constructor can only be used with tuple or unit structs
}
}

impl Lab {
fn lab() {
let _ = Self;
//~^ ERROR the `Self` constructor can only be used with tuple or unit structs
let _ = Self();
//~^ ERROR the `Self` constructor can only be used with tuple or unit structs
}
fn lab_method(self) {
let _ = Self;
//~^ ERROR the `Self` constructor can only be used with tuple or unit structs
let _ = Self();
//~^ ERROR the `Self` constructor can only be used with tuple or unit structs
}
}


fn main() {}
129 changes: 122 additions & 7 deletions tests/ui/typeck/self-constructor-type-error-56199.stderr
Original file line number Diff line number Diff line change
@@ -1,30 +1,145 @@
error: the `Self` constructor can only be used with tuple or unit structs
--> $DIR/self-constructor-type-error-56199.rs:7:17
--> $DIR/self-constructor-type-error-56199.rs:10:17
|
LL | let _ = Self;
| ^^^^
|
= help: did you mean to use one of the enum's variants?
help: the enum is unconstructable because it has no variants
--> $DIR/self-constructor-type-error-56199.rs:2:1
|
LL | enum Foo {}
| ^^^^^^^^

error: the `Self` constructor can only be used with tuple or unit structs
--> $DIR/self-constructor-type-error-56199.rs:9:17
--> $DIR/self-constructor-type-error-56199.rs:12:17
|
LL | let _ = Self();
| ^^^^^^
|
= help: did you mean to use one of the enum's variants?
help: the enum is unconstructable because it has no variants
--> $DIR/self-constructor-type-error-56199.rs:2:1
|
LL | enum Foo {}
| ^^^^^^^^

error: the `Self` constructor can only be used with tuple or unit structs
--> $DIR/self-constructor-type-error-56199.rs:16:17
|
LL | let _ = Self;
| ^^^^ help: use curly brackets: `Self { /* fields */ }`
| ^^^^
|
help: you might have meant to refer to the `self` binding of type `Foo` (notice the capitalization)
|
LL - let _ = Self;
LL + let _ = self;
|

error: the `Self` constructor can only be used with tuple or unit structs
--> $DIR/self-constructor-type-error-56199.rs:18:17
|
LL | let _ = Self();
| ^^^^^^ help: use curly brackets: `Self { /* fields */ }`
| ^^^^^^
|
help: the enum is unconstructable because it has no variants
--> $DIR/self-constructor-type-error-56199.rs:2:1
|
LL | enum Foo {}
| ^^^^^^^^

error: the `Self` constructor can only be used with tuple or unit structs
--> $DIR/self-constructor-type-error-56199.rs:25:17
|
LL | let _ = Self;
| ^^^^
|
help: use curly brackets
|
LL | let _ = Self { /* fields */ };
| ++++++++++++++++

error: the `Self` constructor can only be used with tuple or unit structs
--> $DIR/self-constructor-type-error-56199.rs:27:17
|
LL | let _ = Self();
| ^^^^^^
|
help: use curly brackets
|
LL - let _ = Self();
LL + let _ = Self { /* fields */ };
|

error: the `Self` constructor can only be used with tuple or unit structs
--> $DIR/self-constructor-type-error-56199.rs:31:17
|
LL | let _ = Self;
| ^^^^
|
help: you might have meant to refer to the `self` binding of type `Bar` (notice the capitalization)
|
LL - let _ = Self;
LL + let _ = self;
|

error: the `Self` constructor can only be used with tuple or unit structs
--> $DIR/self-constructor-type-error-56199.rs:33:17
|
LL | let _ = Self();
| ^^^^^^
|
help: use curly brackets
|
LL - let _ = Self();
LL + let _ = Self { /* fields */ };
|

error: the `Self` constructor can only be used with tuple or unit structs
--> $DIR/self-constructor-type-error-56199.rs:40:17
|
LL | let _ = Self;
| ^^^^
|
help: you might have meant to use one of the enum's variants
--> $DIR/self-constructor-type-error-56199.rs:3:1
|
LL | enum Lab {
| ^^^^^^^^

error: the `Self` constructor can only be used with tuple or unit structs
--> $DIR/self-constructor-type-error-56199.rs:42:17
|
LL | let _ = Self();
| ^^^^^^
|
help: you might have meant to use one of the enum's variants
--> $DIR/self-constructor-type-error-56199.rs:3:1
|
LL | enum Lab {
| ^^^^^^^^

error: the `Self` constructor can only be used with tuple or unit structs
--> $DIR/self-constructor-type-error-56199.rs:46:17
|
LL | let _ = Self;
| ^^^^
|
help: you might have meant to refer to the `self` binding of type `Lab` (notice the capitalization)
|
LL - let _ = Self;
LL + let _ = self;
|

error: the `Self` constructor can only be used with tuple or unit structs
--> $DIR/self-constructor-type-error-56199.rs:48:17
|
LL | let _ = Self();
| ^^^^^^
|
help: you might have meant to use one of the enum's variants
--> $DIR/self-constructor-type-error-56199.rs:3:1
|
LL | enum Lab {
| ^^^^^^^^

error: aborting due to 4 previous errors
error: aborting due to 12 previous errors

Loading