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: 9 additions & 1 deletion compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1817,12 +1817,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
expr: &'tcx hir::Expr<'tcx>,
) -> Ty<'tcx> {
let element_ty = if !args.is_empty() {
// This shouldn't happen unless there's another error
// (e.g., never patterns in inappropriate contexts).
if self.diverges.get() != Diverges::Maybe {
self.dcx()
.struct_span_err(expr.span, "unexpected divergence state in checking array")
.delay_as_bug();
}

let coerce_to = expected
.to_option(self)
.and_then(|uty| self.try_structurally_resolve_type(expr.span, uty).builtin_index())
.unwrap_or_else(|| self.next_ty_var(expr.span));
let mut coerce = CoerceMany::with_coercion_sites(coerce_to, args);
assert_eq!(self.diverges.get(), Diverges::Maybe);

for e in args {
let e_ty = self.check_expr_with_hint(e, coerce_to);
let cause = self.misc(e.span);
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ pub(crate) struct FnCtxt<'a, 'tcx> {
/// the diverges flag is set to something other than `Maybe`.
pub(super) diverges: Cell<Diverges>,

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

/// Whether the currently checked node is the whole body of the function.
Expand Down
24 changes: 24 additions & 0 deletions tests/ui/never_type/never-pattern-as-closure-param-141592.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#![feature(never_patterns)]
#![allow(incomplete_features)]

enum Never {}

fn example(x: Never) -> [i32; 1] {
let ! = x;
[1]
}

fn function_param_never(!: Never) -> [i32; 1] {
[1]
}

fn generic_never<T>(!: T) -> [i32; 1] //~ ERROR mismatched types
where
T: Copy,
{
[1]
}

fn main() {
let _ = "12".lines().map(|!| [1]); //~ ERROR mismatched types
}
18 changes: 18 additions & 0 deletions tests/ui/never_type/never-pattern-as-closure-param-141592.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error: mismatched types
--> $DIR/never-pattern-as-closure-param-141592.rs:15:21
|
LL | fn generic_never<T>(!: T) -> [i32; 1]
| ^ a never pattern must be used on an uninhabited type
|
= note: the matched value is of type `T`

error: mismatched types
--> $DIR/never-pattern-as-closure-param-141592.rs:23:31
|
LL | let _ = "12".lines().map(|!| [1]);
| ^ a never pattern must be used on an uninhabited type
|
= note: the matched value is of type `str`

error: aborting due to 2 previous errors

Loading