Skip to content
Open
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
10 changes: 10 additions & 0 deletions compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Type check the pattern. Override if necessary to avoid knock-on errors.
self.check_pat_top(decl.pat, decl_ty, ty_span, origin_expr, Some(decl.origin));
let pat_ty = self.node_ty(decl.pat.hir_id);
if decl.ty.is_none()
&& decl.init.is_none()
&& !matches!(decl.pat.kind, hir::PatKind::Binding(.., None) | hir::PatKind::Wild)
{
self.register_wf_obligation(
decl_ty.into(),
decl.pat.span,
ObligationCauseCode::WellFormed(None),
);
}
self.overwrite_local_ty_if_err(decl.hir_id, decl.pat, pat_ty);

if let Some(blk) = decl.origin.try_get_else() {
Expand Down
58 changes: 58 additions & 0 deletions tests/ui/wf/let-pat-inferred-non-wf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Regression test for https://github.com/rust-lang/rust/issues/150040
// When a `let PAT;` has no explicit type, later assignments can infer a non-well-formed
// pattern type such as `[str; 2]` or `(str, i32)`. We must reject those array and tuple
// patterns instead of accepting the invalid type or causing ICE.

#![allow(unused)]

struct S<T: ?Sized>(T);

fn should_fail_1() {
let ref y @ [ref x, _]; //~ ERROR E0277
x = "";
}

fn should_fail_2() {
let [ref x]; //~ ERROR E0277
x = "";
}

fn should_fail_3() {
let [[ref x], [_, y @ ..]]; //~ ERROR E0277
x = "";
y = [];
}

fn should_fail_4() {
let [(ref a, b), x]; //~ ERROR E0277
a = "";
b = 5;
}

fn should_fail_5() {
let (ref a, b); //~ ERROR E0277
a = "";
b = 5;
}

fn should_fail_6() {
let [S(ref x)]; //~ ERROR E0277
x = "";
}

fn should_pass_1() {
let ref x;
x = "";
}

fn should_pass_2() {
let ref y @ (ref x,);
x = "";
}

fn should_pass_3() {
let S(ref x);
x = "";
}

fn main() {}
62 changes: 62 additions & 0 deletions tests/ui/wf/let-pat-inferred-non-wf.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
error[E0277]: the size for values of type `str` cannot be known at compilation time
--> $DIR/let-pat-inferred-non-wf.rs:11:9
|
LL | let ref y @ [ref x, _];
| ^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `str`
= note: slice and array elements must have `Sized` type

error[E0277]: the size for values of type `str` cannot be known at compilation time
--> $DIR/let-pat-inferred-non-wf.rs:16:9
|
LL | let [ref x];
| ^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `str`
= note: slice and array elements must have `Sized` type

error[E0277]: the size for values of type `str` cannot be known at compilation time
--> $DIR/let-pat-inferred-non-wf.rs:21:9
|
LL | let [[ref x], [_, y @ ..]];
| ^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `str`
= note: slice and array elements must have `Sized` type

error[E0277]: the size for values of type `str` cannot be known at compilation time
--> $DIR/let-pat-inferred-non-wf.rs:27:9
|
LL | let [(ref a, b), x];
| ^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `str`
= note: only the last element of a tuple may have a dynamically sized type

error[E0277]: the size for values of type `str` cannot be known at compilation time
--> $DIR/let-pat-inferred-non-wf.rs:33:9
|
LL | let (ref a, b);
| ^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `str`
= note: only the last element of a tuple may have a dynamically sized type

error[E0277]: the size for values of type `str` cannot be known at compilation time
--> $DIR/let-pat-inferred-non-wf.rs:39:9
|
LL | let [S(ref x)];
| ^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: within `S<str>`, the trait `Sized` is not implemented for `str`
note: required because it appears within the type `S<str>`
--> $DIR/let-pat-inferred-non-wf.rs:8:8
|
LL | struct S<T: ?Sized>(T);
| ^
= note: slice and array elements must have `Sized` type

error: aborting due to 6 previous errors

For more information about this error, try `rustc --explain E0277`.
Loading