Skip to content

Commit 7bb6de0

Browse files
committed
closures cannot be constants
During type collection, error if a closures is found in constant position, catching that before they go causing ICEs. Fixes rust-lang#50600. Fixes rust-lang#48838.
1 parent e5f80f2 commit 7bb6de0

File tree

5 files changed

+51
-2
lines changed

5 files changed

+51
-2
lines changed

src/librustc_mir/interpret/const_eval.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,9 +444,18 @@ pub fn const_eval_provider<'a, 'tcx>(
444444
}
445445

446446
if let Some(id) = tcx.hir.as_local_node_id(def_id) {
447-
let tables = tcx.typeck_tables_of(def_id);
448447
let span = tcx.def_span(def_id);
448+
449+
// Closures in constant position can hit this in type collection.
450+
if !tcx.has_typeck_tables(def_id) {
451+
return Err(ConstEvalErr {
452+
kind: Lrc::new(TypeckError),
453+
span,
454+
});
455+
}
449456

457+
let tables = tcx.typeck_tables_of(def_id);
458+
450459
// Do match-check before building MIR
451460
if tcx.check_match(def_id).is_err() {
452461
return Err(ConstEvalErr {

src/librustc_typeck/collect.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1085,12 +1085,16 @@ fn type_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
10851085

10861086
NodeField(field) => icx.to_ty(&field.ty),
10871087

1088-
NodeExpr(&hir::Expr { node: hir::ExprClosure(.., gen), .. }) => {
1088+
NodeExpr(&hir::Expr { node: hir::ExprClosure(.., gen), span, .. }) => {
10891089
if gen.is_some() {
10901090
let hir_id = tcx.hir.node_to_hir_id(node_id);
10911091
return tcx.typeck_tables_of(def_id).node_id_to_type(hir_id);
10921092
}
10931093

1094+
if !tcx.has_typeck_tables(def_id) {
1095+
span_err!(tcx.sess, span, E0912, "closures cannot be constants");
1096+
}
1097+
10941098
let substs = ty::ClosureSubsts {
10951099
substs: Substs::for_item(
10961100
tcx,

src/librustc_typeck/diagnostics.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4770,4 +4770,5 @@ register_diagnostics! {
47704770
E0641, // cannot cast to/from a pointer with an unknown kind
47714771
E0645, // trait aliases not finished
47724772
E0907, // type inside generator must be known in this context
4773+
E0912, // closures cannot be constants
47734774
}

src/test/ui/const-eval/issue-50600.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Testing that these do not ICE.
12+
13+
struct Foo ([u8; |x: u8| { }]);
14+
//~^ ERROR closures cannot be constants
15+
enum Functions {
16+
Square = |x:i32| { },
17+
//~^ ERROR closures cannot be constants
18+
}
19+
20+
fn main() {}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0912]: closures cannot be constants
2+
--> $DIR/issue-50600.rs:13:18
3+
|
4+
LL | struct Foo ([u8; |x: u8| { }]);
5+
| ^^^^^^^^^^^
6+
7+
error[E0912]: closures cannot be constants
8+
--> $DIR/issue-50600.rs:16:14
9+
|
10+
LL | Square = |x:i32| { },
11+
| ^^^^^^^^^^^
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0912`.

0 commit comments

Comments
 (0)