From 3a014e28463672f17f1ce7b58d384ce41d833de0 Mon Sep 17 00:00:00 2001 From: GrantBirki Date: Fri, 16 May 2025 14:34:21 -0700 Subject: [PATCH 1/2] use `if matches!` instead of `match` for simplicity --- .../src/check_consts/check.rs | 37 +++++++++---------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/compiler/rustc_const_eval/src/check_consts/check.rs b/compiler/rustc_const_eval/src/check_consts/check.rs index b600b8918dd01..8de7316fde618 100644 --- a/compiler/rustc_const_eval/src/check_consts/check.rs +++ b/compiler/rustc_const_eval/src/check_consts/check.rs @@ -345,7 +345,7 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> { /// initializer. The check assumes that all already existing pointers and references point to /// non-escaping places. fn place_may_escape(&mut self, place: &Place<'_>) -> bool { - let is_transient = match self.const_kind() { + let is_transient = if matches!(self.const_kind(), hir::ConstContext::ConstFn) { // In a const fn all borrows are transient or point to the places given via // references in the arguments (so we already checked them with // TransientMutBorrow/MutBorrow as appropriate). @@ -353,24 +353,23 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> { // NOTE: Once we have heap allocations during CTFE we need to figure out // how to prevent `const fn` to create long-lived allocations that point // to mutable memory. - hir::ConstContext::ConstFn => true, - _ => { - // For indirect places, we are not creating a new permanent borrow, it's just as - // transient as the already existing one. For reborrowing references this is handled - // at the top of `visit_rvalue`, but for raw pointers we handle it here. - // Pointers/references to `static mut` and cases where the `*` is not the first - // projection also end up here. - // Locals with StorageDead do not live beyond the evaluation and can - // thus safely be borrowed without being able to be leaked to the final - // value of the constant. - // Note: This is only sound if every local that has a `StorageDead` has a - // `StorageDead` in every control flow path leading to a `return` terminator. - // If anything slips through, there's no safety net -- safe code can create - // references to variants of `!Freeze` enums as long as that variant is `Freeze`, so - // interning can't protect us here. (There *is* a safety net for mutable references - // though, interning will ICE if we miss something here.) - place.is_indirect() || self.local_is_transient(place.local) - } + true + } else { + // For indirect places, we are not creating a new permanent borrow, it's just as + // transient as the already existing one. For reborrowing references this is handled + // at the top of `visit_rvalue`, but for raw pointers we handle it here. + // Pointers/references to `static mut` and cases where the `*` is not the first + // projection also end up here. + // Locals with StorageDead do not live beyond the evaluation and can + // thus safely be borrowed without being able to be leaked to the final + // value of the constant. + // Note: This is only sound if every local that has a `StorageDead` has a + // `StorageDead` in every control flow path leading to a `return` terminator. + // If anything slips through, there's no safety net -- safe code can create + // references to variants of `!Freeze` enums as long as that variant is `Freeze`, so + // interning can't protect us here. (There *is* a safety net for mutable references + // though, interning will ICE if we miss something here.) + place.is_indirect() || self.local_is_transient(place.local) }; // Transient places cannot possibly escape because the place doesn't exist any more at the // end of evaluation. From 69155ab40b5fed49d0baf40504a208ff1bfed509 Mon Sep 17 00:00:00 2001 From: GrantBirki Date: Fri, 16 May 2025 14:44:46 -0700 Subject: [PATCH 2/2] use `if matches!` in `assert.rs` --- compiler/rustc_builtin_macros/src/assert.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_builtin_macros/src/assert.rs b/compiler/rustc_builtin_macros/src/assert.rs index c659b1cff59b8..481d409d50f7d 100644 --- a/compiler/rustc_builtin_macros/src/assert.rs +++ b/compiler/rustc_builtin_macros/src/assert.rs @@ -139,7 +139,7 @@ fn parse_assert<'a>(cx: &ExtCtxt<'a>, sp: Span, stream: TokenStream) -> PResult< // // Emit an error and suggest inserting a comma. let custom_message = - if let token::Literal(token::Lit { kind: token::Str, .. }) = parser.token.kind { + if matches!(parser.token.kind, token::Literal(token::Lit { kind: token::Str, .. })) { let comma = parser.prev_token.span.shrink_to_hi(); cx.dcx().emit_err(errors::AssertMissingComma { span: parser.token.span, comma });