Skip to content

Commit a09a419

Browse files
committed
derive: emit intrinsics::unreachable for impls on empty enums
fixes #31574
1 parent c433b70 commit a09a419

File tree

1 file changed

+18
-11
lines changed
  • src/libsyntax_ext/deriving/generic

1 file changed

+18
-11
lines changed

src/libsyntax_ext/deriving/generic/mod.rs

+18-11
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,22 @@ fn find_type_parameters(ty: &ast::Ty, ty_param_names: &[ast::Name]) -> Vec<P<ast
381381
visitor.types
382382
}
383383

384+
/// Replacement for expr_unreachable which generates intrinsics::unreachable()
385+
/// instead of unreachable!()
386+
fn expr_unreachable_intrinsic(cx: &ExtCtxt, sp: Span) -> P<Expr> {
387+
let path = cx.std_path(&["intrinsics", "unreachable"]);
388+
let call = cx.expr_call_global(
389+
sp, path, vec![]);
390+
let unreachable = cx.expr_block(P(ast::Block {
391+
stmts: vec![],
392+
expr: Some(call),
393+
id: ast::DUMMY_NODE_ID,
394+
rules: ast::BlockCheckMode::Unsafe(ast::CompilerGenerated),
395+
span: sp }));
396+
397+
unreachable
398+
}
399+
384400
impl<'a> TraitDef<'a> {
385401
pub fn expand(&self,
386402
cx: &mut ExtCtxt,
@@ -1297,16 +1313,7 @@ impl<'a> MethodDef<'a> {
12971313
//Since we know that all the arguments will match if we reach the match expression we
12981314
//add the unreachable intrinsics as the result of the catch all which should help llvm
12991315
//in optimizing it
1300-
let path = cx.std_path(&["intrinsics", "unreachable"]);
1301-
let call = cx.expr_call_global(
1302-
sp, path, vec![]);
1303-
let unreachable = cx.expr_block(P(ast::Block {
1304-
stmts: vec![],
1305-
expr: Some(call),
1306-
id: ast::DUMMY_NODE_ID,
1307-
rules: ast::BlockCheckMode::Unsafe(ast::CompilerGenerated),
1308-
span: sp }));
1309-
match_arms.push(cx.arm(sp, vec![cx.pat_wild(sp)], unreachable));
1316+
match_arms.push(cx.arm(sp, vec![cx.pat_wild(sp)], expr_unreachable_intrinsic(cx, sp)));
13101317

13111318
// Final wrinkle: the self_args are expressions that deref
13121319
// down to desired l-values, but we cannot actually deref
@@ -1382,7 +1389,7 @@ impl<'a> MethodDef<'a> {
13821389
// derive Debug on such a type could here generate code
13831390
// that needs the feature gate enabled.)
13841391

1385-
cx.expr_unreachable(sp)
1392+
expr_unreachable_intrinsic(cx, sp)
13861393
}
13871394
else {
13881395

0 commit comments

Comments
 (0)