Skip to content

Commit c140e25

Browse files
authored
Rollup merge of #110283 - saethlin:check-panics-before-alignment, r=bjorn3
Only emit alignment checks if we have a panic_impl Fixes #109996 r? `@bjorn3` because you commented that this situation could impact you as well
2 parents 759d4e8 + 4061eb5 commit c140e25

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

compiler/rustc_mir_transform/src/check_alignment.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::MirPass;
22
use rustc_hir::def_id::DefId;
3+
use rustc_hir::lang_items::LangItem;
34
use rustc_index::vec::IndexVec;
45
use rustc_middle::mir::*;
56
use rustc_middle::mir::{
@@ -17,6 +18,12 @@ impl<'tcx> MirPass<'tcx> for CheckAlignment {
1718
}
1819

1920
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
21+
// This pass emits new panics. If for whatever reason we do not have a panic
22+
// implementation, running this pass may cause otherwise-valid code to not compile.
23+
if tcx.lang_items().get(LangItem::PanicImpl).is_none() {
24+
return;
25+
}
26+
2027
let basic_blocks = body.basic_blocks.as_mut();
2128
let local_decls = &mut body.local_decls;
2229

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Ensures that the alignment check we insert for raw pointer dereferences
2+
// does not prevent crates without a panic_impl from compiling.
3+
// See rust-lang/rust#109996
4+
5+
// build-pass
6+
// compile-flags: -Cdebug-assertions=yes
7+
8+
#![crate_type = "lib"]
9+
10+
#![feature(lang_items)]
11+
#![feature(no_core)]
12+
#![no_core]
13+
14+
#[lang = "sized"]
15+
trait Foo {}
16+
17+
pub unsafe fn foo(x: *const i32) -> &'static i32 { unsafe { &*x } }

0 commit comments

Comments
 (0)