From b145213ceead916028d84db38f19b4288c85ea15 Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Sat, 18 Oct 2025 23:35:23 +0000 Subject: [PATCH 1/2] Parse `const unsafe trait` properly Previously, this was greedily stolen by the `fn` parsing logic. --- compiler/rustc_parse/src/parser/item.rs | 5 ++++- .../traits/const-traits/parse-const-unsafe-trait.rs | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 tests/ui/traits/const-traits/parse-const-unsafe-trait.rs diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index aa03d5956cd23..6f0e3b81cf2a6 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -2664,7 +2664,10 @@ impl<'a> Parser<'a> { // Rule out `unsafe extern {`. && !self.is_unsafe_foreign_mod() // Rule out `async gen {` and `async gen move {` - && !self.is_async_gen_block()) + && !self.is_async_gen_block() + // Rule out `const unsafe auto` and `const unsafe trait`. + && !self.is_keyword_ahead(2, &[kw::Auto, kw::Trait]) + ) }) // `extern ABI fn` || self.check_keyword_case(exp!(Extern), case) diff --git a/tests/ui/traits/const-traits/parse-const-unsafe-trait.rs b/tests/ui/traits/const-traits/parse-const-unsafe-trait.rs new file mode 100644 index 0000000000000..3d62405d9ae20 --- /dev/null +++ b/tests/ui/traits/const-traits/parse-const-unsafe-trait.rs @@ -0,0 +1,13 @@ +// Test that `const unsafe trait` and `const unsafe auto trait` works. + +//@ check-pass + +#![feature(const_trait_impl)] +#![feature(auto_traits)] + +pub const unsafe trait Owo {} +const unsafe trait OwO {} +pub const unsafe auto trait UwU {} +const unsafe auto trait Uwu {} + +fn main() {} From a1e42f96f68dede87bda42e58c7c5373e0c5e9e0 Mon Sep 17 00:00:00 2001 From: Camille Gillot Date: Sat, 20 Sep 2025 23:01:48 +0000 Subject: [PATCH 2/2] Simplify new_local. --- compiler/rustc_mir_transform/src/patch.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_mir_transform/src/patch.rs b/compiler/rustc_mir_transform/src/patch.rs index 2c535d011a0e2..015bae56cf57e 100644 --- a/compiler/rustc_mir_transform/src/patch.rs +++ b/compiler/rustc_mir_transform/src/patch.rs @@ -24,6 +24,8 @@ pub(crate) struct MirPatch<'tcx> { // Cached block for UnwindTerminate (with reason) terminate_block: Option<(BasicBlock, UnwindTerminateReason)>, body_span: Span, + /// The number of locals at the start of the transformation. New locals + /// get appended at the end. next_local: usize, /// The number of blocks at the start of the transformation. New blocks /// get appended at the end. @@ -176,8 +178,7 @@ impl<'tcx> MirPatch<'tcx> { span: Span, local_info: LocalInfo<'tcx>, ) -> Local { - let index = self.next_local; - self.next_local += 1; + let index = self.next_local + self.new_locals.len(); let mut new_decl = LocalDecl::new(ty, span); **new_decl.local_info.as_mut().unwrap_crate_local() = local_info; self.new_locals.push(new_decl); @@ -186,8 +187,7 @@ impl<'tcx> MirPatch<'tcx> { /// Queues the addition of a new temporary. pub(crate) fn new_temp(&mut self, ty: Ty<'tcx>, span: Span) -> Local { - let index = self.next_local; - self.next_local += 1; + let index = self.next_local + self.new_locals.len(); self.new_locals.push(LocalDecl::new(ty, span)); Local::new(index) } @@ -195,8 +195,8 @@ impl<'tcx> MirPatch<'tcx> { /// Returns the type of a local that's newly-added in the patch. pub(crate) fn local_ty(&self, local: Local) -> Ty<'tcx> { let local = local.as_usize(); - assert!(local < self.next_local); - let new_local_idx = self.new_locals.len() - (self.next_local - local); + assert!(local < self.next_local + self.new_locals.len()); + let new_local_idx = local - self.next_local; self.new_locals[new_local_idx].ty }