Skip to content

Commit 23b8c06

Browse files
authored
Rollup merge of #67044 - Centril:67037, r=estebank
E0023: handle expected != tuple pattern type Fixes #67037. r? @estebank
2 parents 7945dcd + 4746d37 commit 23b8c06

File tree

4 files changed

+70
-10
lines changed

4 files changed

+70
-10
lines changed

src/librustc_typeck/check/demand.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
6565
}
6666
}
6767

68-
pub fn demand_eqtype_pat(
68+
pub fn demand_eqtype_pat_diag(
6969
&self,
7070
cause_span: Span,
7171
expected: Ty<'tcx>,
7272
actual: Ty<'tcx>,
7373
match_expr_span: Option<Span>,
74-
) {
74+
) -> Option<DiagnosticBuilder<'tcx>> {
7575
let cause = if let Some(span) = match_expr_span {
7676
self.cause(
7777
cause_span,
@@ -80,9 +80,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
8080
} else {
8181
self.misc(cause_span)
8282
};
83-
self.demand_eqtype_with_origin(&cause, expected, actual).map(|mut err| err.emit());
83+
self.demand_eqtype_with_origin(&cause, expected, actual)
8484
}
8585

86+
pub fn demand_eqtype_pat(
87+
&self,
88+
cause_span: Span,
89+
expected: Ty<'tcx>,
90+
actual: Ty<'tcx>,
91+
match_expr_span: Option<Span>,
92+
) {
93+
self.demand_eqtype_pat_diag(cause_span, expected, actual, match_expr_span)
94+
.map(|mut err| err.emit());
95+
}
8696

8797
pub fn demand_coerce(&self,
8898
expr: &hir::Expr,

src/librustc_typeck/check/pat.rs

+14-7
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
703703
let pat_ty = pat_ty.fn_sig(tcx).output();
704704
let pat_ty = pat_ty.no_bound_vars().expect("expected fn type");
705705

706-
self.demand_eqtype_pat(pat.span, expected, pat_ty, match_arm_pat_span);
706+
// Type-check the tuple struct pattern against the expected type.
707+
let diag = self.demand_eqtype_pat_diag(pat.span, expected, pat_ty, match_arm_pat_span);
708+
let had_err = diag.is_some();
709+
diag.map(|mut err| err.emit());
707710

708711
// Type-check subpatterns.
709712
if subpats.len() == variant.fields.len()
@@ -721,7 +724,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
721724
}
722725
} else {
723726
// Pattern has wrong number of fields.
724-
self.e0023(pat.span, res, qpath, subpats, &variant.fields, expected);
727+
self.e0023(pat.span, res, qpath, subpats, &variant.fields, expected, had_err);
725728
on_error();
726729
return tcx.types.err;
727730
}
@@ -734,8 +737,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
734737
res: Res,
735738
qpath: &hir::QPath,
736739
subpats: &'tcx [P<Pat>],
737-
fields: &[ty::FieldDef],
738-
expected: Ty<'tcx>
740+
fields: &'tcx [ty::FieldDef],
741+
expected: Ty<'tcx>,
742+
had_err: bool,
739743
) {
740744
let subpats_ending = pluralize!(subpats.len());
741745
let fields_ending = pluralize!(fields.len());
@@ -763,9 +767,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
763767
// More generally, the expected type wants a tuple variant with one field of an
764768
// N-arity-tuple, e.g., `V_i((p_0, .., p_N))`. Meanwhile, the user supplied a pattern
765769
// with the subpatterns directly in the tuple variant pattern, e.g., `V_i(p_0, .., p_N)`.
766-
let missing_parenthesis = match expected.kind {
767-
ty::Adt(_, substs) if fields.len() == 1 => {
768-
let field_ty = fields[0].ty(self.tcx, substs);
770+
let missing_parenthesis = match (&expected.kind, fields, had_err) {
771+
// #67037: only do this if we could sucessfully type-check the expected type against
772+
// the tuple struct pattern. Otherwise the substs could get out of range on e.g.,
773+
// `let P() = U;` where `P != U` with `struct P<T>(T);`.
774+
(ty::Adt(_, substs), [field], false) => {
775+
let field_ty = self.field_ty(pat_span, field, substs);
769776
match field_ty.kind {
770777
ty::Tuple(_) => field_ty.tuple_fields().count() == subpats.len(),
771778
_ => false,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Regression test for #67037.
2+
//
3+
// In type checking patterns, E0023 occurs when the tuple pattern and the expected
4+
// tuple pattern have different number of fields. For example, as below, `P()`,
5+
// the tuple struct pattern, has 0 fields, but requires 1 field.
6+
//
7+
// In emitting E0023, we try to see if this is a case of e.g., `Some(a, b, c)` but where
8+
// the scrutinee was of type `Some((a, b, c))`, and suggest that parenthesis be added.
9+
//
10+
// However, we did not account for the expected type being different than the tuple pattern type.
11+
// This caused an issue when the tuple pattern type (`P<T>`) was generic.
12+
// Specifically, we tried deriving the 0th field's type using the `substs` of the expected type.
13+
// When attempting to substitute `T`, there was no such substitution, so "out of range" occured.
14+
15+
struct U {} // 0 type parameters offered
16+
struct P<T>(T); // 1 type parameter wanted
17+
18+
fn main() {
19+
let P() = U {}; //~ ERROR mismatched types
20+
//~^ ERROR this pattern has 0 fields, but the corresponding tuple struct has 1 field
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-67037-pat-tup-scrut-ty-diff-less-fields.rs:19:9
3+
|
4+
LL | let P() = U {};
5+
| ^^^ expected struct `U`, found struct `P`
6+
|
7+
= note: expected struct `U`
8+
found struct `P<_>`
9+
10+
error[E0023]: this pattern has 0 fields, but the corresponding tuple struct has 1 field
11+
--> $DIR/issue-67037-pat-tup-scrut-ty-diff-less-fields.rs:19:9
12+
|
13+
LL | struct P<T>(T); // 1 type parameter wanted
14+
| --------------- tuple struct defined here
15+
...
16+
LL | let P() = U {};
17+
| ^^^ expected 1 field, found 0
18+
19+
error: aborting due to 2 previous errors
20+
21+
Some errors have detailed explanations: E0023, E0308.
22+
For more information about an error, try `rustc --explain E0023`.

0 commit comments

Comments
 (0)