Skip to content

Commit d6b5ffb

Browse files
committed
Auto merge of #77821 - tmiasko:discriminant-value-is-safe, r=jonas-schievink
Remove unnecessary unsafe block around calls to discriminant_value Since 63793 the discriminant_value intrinsic is safe to call. Remove unnecessary unsafe block around calls to this intrinsic in built-in derive macros.
2 parents d9b9316 + 50da126 commit d6b5ffb

File tree

2 files changed

+16
-15
lines changed
  • compiler/rustc_builtin_macros/src/deriving

2 files changed

+16
-15
lines changed

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

+8-14
Original file line numberDiff line numberDiff line change
@@ -1137,12 +1137,9 @@ impl<'a> MethodDef<'a> {
11371137
/// for each of the self-args, carried in precomputed variables.
11381138
11391139
/// ```{.text}
1140-
/// let __self0_vi = unsafe {
1141-
/// std::intrinsics::discriminant_value(&self) };
1142-
/// let __self1_vi = unsafe {
1143-
/// std::intrinsics::discriminant_value(&arg1) };
1144-
/// let __self2_vi = unsafe {
1145-
/// std::intrinsics::discriminant_value(&arg2) };
1140+
/// let __self0_vi = std::intrinsics::discriminant_value(&self);
1141+
/// let __self1_vi = std::intrinsics::discriminant_value(&arg1);
1142+
/// let __self2_vi = std::intrinsics::discriminant_value(&arg2);
11461143
///
11471144
/// if __self0_vi == __self1_vi && __self0_vi == __self2_vi && ... {
11481145
/// match (...) {
@@ -1325,7 +1322,7 @@ impl<'a> MethodDef<'a> {
13251322
// Since we know that all the arguments will match if we reach
13261323
// the match expression we add the unreachable intrinsics as the
13271324
// result of the catch all which should help llvm in optimizing it
1328-
Some(deriving::call_intrinsic(cx, sp, sym::unreachable, vec![]))
1325+
Some(deriving::call_unreachable(cx, sp))
13291326
}
13301327
_ => None,
13311328
};
@@ -1356,12 +1353,9 @@ impl<'a> MethodDef<'a> {
13561353
// with three Self args, builds three statements:
13571354
//
13581355
// ```
1359-
// let __self0_vi = unsafe {
1360-
// std::intrinsics::discriminant_value(&self) };
1361-
// let __self1_vi = unsafe {
1362-
// std::intrinsics::discriminant_value(&arg1) };
1363-
// let __self2_vi = unsafe {
1364-
// std::intrinsics::discriminant_value(&arg2) };
1356+
// let __self0_vi = std::intrinsics::discriminant_value(&self);
1357+
// let __self1_vi = std::intrinsics::discriminant_value(&arg1);
1358+
// let __self2_vi = std::intrinsics::discriminant_value(&arg2);
13651359
// ```
13661360
let mut index_let_stmts: Vec<ast::Stmt> = Vec::with_capacity(vi_idents.len() + 1);
13671361

@@ -1474,7 +1468,7 @@ impl<'a> MethodDef<'a> {
14741468
// derive Debug on such a type could here generate code
14751469
// that needs the feature gate enabled.)
14761470

1477-
deriving::call_intrinsic(cx, sp, sym::unreachable, vec![])
1471+
deriving::call_unreachable(cx, sp)
14781472
} else {
14791473
// Final wrinkle: the self_args are expressions that deref
14801474
// down to desired places, but we cannot actually deref

compiler/rustc_builtin_macros/src/deriving/mod.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,14 @@ fn call_intrinsic(
6868
) -> P<ast::Expr> {
6969
let span = cx.with_def_site_ctxt(span);
7070
let path = cx.std_path(&[sym::intrinsics, intrinsic]);
71-
let call = cx.expr_call_global(span, path, args);
71+
cx.expr_call_global(span, path, args)
72+
}
73+
74+
/// Constructs an expression that calls the `unreachable` intrinsic.
75+
fn call_unreachable(cx: &ExtCtxt<'_>, span: Span) -> P<ast::Expr> {
76+
let span = cx.with_def_site_ctxt(span);
77+
let path = cx.std_path(&[sym::intrinsics, sym::unreachable]);
78+
let call = cx.expr_call_global(span, path, vec![]);
7279

7380
cx.expr_block(P(ast::Block {
7481
stmts: vec![cx.stmt_expr(call)],

0 commit comments

Comments
 (0)