Skip to content

Commit 0195714

Browse files
committed
Fix ICEs with match/return expressions inside array lengths
1 parent 30fde04 commit 0195714

File tree

4 files changed

+58
-9
lines changed

4 files changed

+58
-9
lines changed

src/librustc/ty/layout.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1118,9 +1118,12 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
11181118
ty::TyParam(_) => {
11191119
return Err(LayoutError::Unknown(ty));
11201120
}
1121-
ty::TyGeneratorWitness(..) | ty::TyInfer(_) | ty::TyError => {
1121+
ty::TyGeneratorWitness(..) | ty::TyInfer(_) => {
11221122
bug!("LayoutDetails::compute: unexpected type `{}`", ty)
11231123
}
1124+
ty::TyError => {
1125+
return Err(LayoutError::Unknown(ty));
1126+
}
11241127
})
11251128
}
11261129

src/librustc_mir/hair/pattern/mod.rs

+16-8
Original file line numberDiff line numberDiff line change
@@ -743,8 +743,10 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
743743
);
744744
*self.const_to_pat(instance, val, expr.hir_id, lit.span).kind
745745
},
746-
Err(()) => {
747-
self.errors.push(PatternError::FloatBug);
746+
Err(float_bug) => {
747+
if float_bug {
748+
self.errors.push(PatternError::FloatBug);
749+
}
748750
PatternKind::Wild
749751
},
750752
}
@@ -764,8 +766,10 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
764766
);
765767
*self.const_to_pat(instance, val, expr.hir_id, lit.span).kind
766768
},
767-
Err(()) => {
768-
self.errors.push(PatternError::FloatBug);
769+
Err(float_bug) => {
770+
if float_bug {
771+
self.errors.push(PatternError::FloatBug);
772+
}
769773
PatternKind::Wild
770774
},
771775
}
@@ -1123,7 +1127,7 @@ fn lit_to_const<'a, 'tcx>(lit: &'tcx ast::LitKind,
11231127
tcx: TyCtxt<'a, 'tcx, 'tcx>,
11241128
ty: Ty<'tcx>,
11251129
neg: bool)
1126-
-> Result<&'tcx ty::Const<'tcx>, ()> {
1130+
-> Result<&'tcx ty::Const<'tcx>, bool> {
11271131
use syntax::ast::*;
11281132

11291133
use rustc::mir::interpret::*;
@@ -1152,7 +1156,11 @@ fn lit_to_const<'a, 'tcx>(lit: &'tcx ast::LitKind,
11521156
ty::TyInt(other) => Int::Signed(other),
11531157
ty::TyUint(UintTy::Usize) => Int::Unsigned(tcx.sess.target.usize_ty),
11541158
ty::TyUint(other) => Int::Unsigned(other),
1155-
_ => bug!(),
1159+
ty::TyError => {
1160+
// Avoid ICE
1161+
return Err(false);
1162+
}
1163+
_ => bug!("{:?}", ty.sty),
11561164
};
11571165
// This converts from LitKind::Int (which is sign extended) to
11581166
// Scalar::Bytes (which is zero extended)
@@ -1182,14 +1190,14 @@ fn lit_to_const<'a, 'tcx>(lit: &'tcx ast::LitKind,
11821190
})
11831191
},
11841192
LitKind::Float(n, fty) => {
1185-
parse_float(n, fty, neg)?
1193+
parse_float(n, fty, neg).map_err(|_| true)?
11861194
}
11871195
LitKind::FloatUnsuffixed(n) => {
11881196
let fty = match ty.sty {
11891197
ty::TyFloat(fty) => fty,
11901198
_ => bug!()
11911199
};
1192-
parse_float(n, fty, neg)?
1200+
parse_float(n, fty, neg).map_err(|_| true)?
11931201
}
11941202
LitKind::Bool(b) => ConstValue::Scalar(Scalar::Bits {
11951203
bits: b as u128,
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
fn main() {
12+
[(); return match 0 { n => n }]; //~ ERROR: return statement outside of function body
13+
14+
[(); return match 0 { 0 => 0 }]; //~ ERROR: return statement outside of function body
15+
16+
[(); return match () { 'a' => 0, _ => 0 }]; //~ ERROR: return statement outside of function body
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0572]: return statement outside of function body
2+
--> $DIR/return-match-array-const.rs:12:10
3+
|
4+
LL | [(); return match 0 { n => n }]; //~ ERROR: return statement outside of function body
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
6+
7+
error[E0572]: return statement outside of function body
8+
--> $DIR/return-match-array-const.rs:14:10
9+
|
10+
LL | [(); return match 0 { 0 => 0 }]; //~ ERROR: return statement outside of function body
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error[E0572]: return statement outside of function body
14+
--> $DIR/return-match-array-const.rs:16:10
15+
|
16+
LL | [(); return match () { 'a' => 0, _ => 0 }]; //~ ERROR: return statement outside of function body
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
19+
error: aborting due to 3 previous errors
20+
21+
For more information about this error, try `rustc --explain E0572`.

0 commit comments

Comments
 (0)