Skip to content

Commit a427f3f

Browse files
committed
Fix ice 141592, change diverge assertion to struct error with delay
1 parent 2cb4e7d commit a427f3f

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1817,12 +1817,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18171817
expr: &'tcx hir::Expr<'tcx>,
18181818
) -> Ty<'tcx> {
18191819
let element_ty = if !args.is_empty() {
1820+
// This shouldn't happen unless there's another error
1821+
// (e.g., never patterns in inappropriate contexts).
1822+
if self.diverges.get() != Diverges::Maybe {
1823+
self.dcx()
1824+
.struct_span_err(
1825+
expr.span,
1826+
"unexpected divergence state before checking array elements",
1827+
)
1828+
.delay_as_bug();
1829+
}
1830+
18201831
let coerce_to = expected
18211832
.to_option(self)
18221833
.and_then(|uty| self.try_structurally_resolve_type(expr.span, uty).builtin_index())
18231834
.unwrap_or_else(|| self.next_ty_var(expr.span));
18241835
let mut coerce = CoerceMany::with_coercion_sites(coerce_to, args);
1825-
assert_eq!(self.diverges.get(), Diverges::Maybe);
1836+
18261837
for e in args {
18271838
let e_ty = self.check_expr_with_hint(e, coerce_to);
18281839
let cause = self.misc(e.span);

compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ pub(crate) struct FnCtxt<'a, 'tcx> {
104104
/// the diverges flag is set to something other than `Maybe`.
105105
pub(super) diverges: Cell<Diverges>,
106106

107-
/// If one of the function arguments is a never pattern, this counts as diverging code. This
108-
/// affect typechecking of the function body.
107+
/// If one of the function arguments is a never pattern, this counts as diverging code.
108+
/// This affect typechecking of the function body.
109109
pub(super) function_diverges_because_of_empty_arguments: Cell<Diverges>,
110110

111111
/// Whether the currently checked node is the whole body of the function.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#![feature(never_patterns)]
2+
#![allow(incomplete_features)]
3+
4+
enum Never {}
5+
6+
fn example(x: Never) -> [i32; 1] {
7+
let ! = x;
8+
[1]
9+
}
10+
11+
fn function_param_never(!: Never) -> [i32; 1] {
12+
[1]
13+
}
14+
15+
fn generic_never<T>(!: T) -> [i32; 1] //~ ERROR mismatched types
16+
where
17+
T: Copy,
18+
{
19+
[1]
20+
}
21+
22+
fn main() {
23+
let _ = "12".lines().map(|!| [1]); //~ ERROR mismatched types
24+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: mismatched types
2+
--> $DIR/never-pattern-as-closure-param-141592.rs:15:21
3+
|
4+
LL | fn generic_never<T>(!: T) -> [i32; 1]
5+
| ^ a never pattern must be used on an uninhabited type
6+
|
7+
= note: the matched value is of type `T`
8+
9+
error: mismatched types
10+
--> $DIR/never-pattern-as-closure-param-141592.rs:23:31
11+
|
12+
LL | let _ = "12".lines().map(|!| [1]);
13+
| ^ a never pattern must be used on an uninhabited type
14+
|
15+
= note: the matched value is of type `str`
16+
17+
error: aborting due to 2 previous errors
18+

0 commit comments

Comments
 (0)