Skip to content

Commit 5444e03

Browse files
committed
always make tuple elements a coercion site
1 parent 1a8b8dd commit 5444e03

File tree

10 files changed

+38
-54
lines changed

10 files changed

+38
-54
lines changed

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1982,27 +1982,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
19821982

19831983
fn check_expr_tuple(
19841984
&self,
1985-
elts: &'tcx [hir::Expr<'tcx>],
1985+
elements: &'tcx [hir::Expr<'tcx>],
19861986
expected: Expectation<'tcx>,
19871987
expr: &'tcx hir::Expr<'tcx>,
19881988
) -> Ty<'tcx> {
1989-
let flds = expected.only_has_type(self).and_then(|ty| {
1990-
let ty = self.try_structurally_resolve_type(expr.span, ty);
1991-
match ty.kind() {
1992-
ty::Tuple(flds) => Some(&flds[..]),
1993-
_ => None,
1994-
}
1989+
let mut expectations = expected
1990+
.only_has_type(self)
1991+
.and_then(|ty| self.try_structurally_resolve_type(expr.span, ty).tuple())
1992+
.unwrap_or_default()
1993+
.iter();
1994+
1995+
let elements = elements.iter().map(|e| {
1996+
let ty = expectations.next().unwrap_or_else(|| self.next_ty_var(e.span));
1997+
self.check_expr_coercible_to_type(e, ty, None);
1998+
ty
19951999
});
19962000

1997-
let elt_ts_iter = elts.iter().enumerate().map(|(i, e)| match flds {
1998-
Some(fs) if i < fs.len() => {
1999-
let ety = fs[i];
2000-
self.check_expr_coercible_to_type(e, ety, None);
2001-
ety
2002-
}
2003-
_ => self.check_expr_with_expectation(e, NoExpectation),
2004-
});
2005-
let tuple = Ty::new_tup_from_iter(self.tcx, elt_ts_iter);
2001+
let tuple = Ty::new_tup_from_iter(self.tcx, elements);
2002+
20062003
if let Err(guar) = tuple.error_reported() {
20072004
Ty::new_error(self.tcx, guar)
20082005
} else {

compiler/rustc_middle/src/ty/sty.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1591,7 +1591,8 @@ impl<'tcx> Ty<'tcx> {
15911591
}
15921592
}
15931593

1594-
/// Iterates over tuple fields.
1594+
/// Returns a list of tuple fields.
1595+
///
15951596
/// Panics when called on anything but a tuple.
15961597
#[inline]
15971598
pub fn tuple_fields(self) -> &'tcx List<Ty<'tcx>> {
@@ -1601,6 +1602,15 @@ impl<'tcx> Ty<'tcx> {
16011602
}
16021603
}
16031604

1605+
/// Returns a list of tuple type arguments, or `None` if `self` isn't a tuple.
1606+
#[inline]
1607+
pub fn tuple(self) -> Option<&'tcx List<Ty<'tcx>>> {
1608+
match self.kind() {
1609+
Tuple(args) => Some(args),
1610+
_ => None,
1611+
}
1612+
}
1613+
16041614
/// If the type contains variants, returns the valid range of variant indices.
16051615
//
16061616
// FIXME: This requires the optimized MIR in the case of coroutines.

tests/ui/loops/loop-break-value.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,10 @@ LL | break (break, break);
232232
| || |
233233
| || expected because of this `break`
234234
| |expected because of this `break`
235-
| expected `()`, found `(!, !)`
235+
| expected `()`, found `(_, _)`
236236
|
237237
= note: expected unit type `()`
238-
found tuple `(!, !)`
238+
found tuple `(_, _)`
239239

240240
error[E0308]: mismatched types
241241
--> $DIR/loop-break-value.rs:89:15

tests/ui/never_type/diverging-tuple-parts-39485.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ error[E0308]: mismatched types
2222
LL | fn f() -> isize {
2323
| ----- expected `isize` because of return type
2424
LL | (return 1, return 2)
25-
| ^^^^^^^^^^^^^^^^^^^^ expected `isize`, found `(!, !)`
25+
| ^^^^^^^^^^^^^^^^^^^^ expected `isize`, found `(_, _)`
2626
|
2727
= note: expected type `isize`
28-
found tuple `(!, !)`
28+
found tuple `(_, _)`
2929

3030
error: aborting due to 2 previous errors
3131

tests/ui/never_type/issue-10176.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ fn f() -> isize { //~ NOTE expected `isize` because of return type
22
(return 1, return 2)
33
//~^ ERROR mismatched types
44
//~| NOTE expected type `isize`
5-
//~| NOTE found tuple `(!, !)`
6-
//~| NOTE expected `isize`, found `(!, !)`
5+
//~| NOTE found tuple `(_, _)`
6+
//~| NOTE expected `isize`, found `(_, _)`
77
}
88

99
fn main() {}

tests/ui/never_type/issue-10176.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ error[E0308]: mismatched types
44
LL | fn f() -> isize {
55
| ----- expected `isize` because of return type
66
LL | (return 1, return 2)
7-
| ^^^^^^^^^^^^^^^^^^^^ expected `isize`, found `(!, !)`
7+
| ^^^^^^^^^^^^^^^^^^^^ expected `isize`, found `(_, _)`
88
|
99
= note: expected type `isize`
10-
found tuple `(!, !)`
10+
found tuple `(_, _)`
1111

1212
error: aborting due to 1 previous error
1313

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
fn main() {
22
let _tmp = [
33
("C200B40A82", 3),
4-
("C200B40A83", 4) //~ ERROR: expected function, found `(&'static str, {integer})` [E0618]
4+
("C200B40A83", 4) //~ ERROR: expected function, found `(&str, {integer})` [E0618]
55
("C200B40A8537", 5),
66
];
77
}

tests/ui/tuple/array-diagnostics.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0618]: expected function, found `(&'static str, {integer})`
1+
error[E0618]: expected function, found `(&str, {integer})`
22
--> $DIR/array-diagnostics.rs:4:9
33
|
44
LL | ("C200B40A83", 4)

tests/ui/tuple/coercion-never.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
// unifying match arms, for example.
44
//
55
// See also coercion-slice.rs
6+
//
7+
//@ check-pass
68

79
fn main() {
810
let _: ((),) = (loop {},);
911

10-
((),) = (loop {},); //~ error: mismatched types
12+
((),) = (loop {},);
1113

1214
let x = (loop {},);
13-
let _: ((),) = x; //~ error: mismatched types
15+
let _: ((),) = x;
1416
}

tests/ui/tuple/coercion-never.stderr

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)