Skip to content

Commit

Permalink
Auto merge of rust-lang#137502 - compiler-errors:global-asm-aint-mir-…
Browse files Browse the repository at this point in the history
…body, r=<try>

Don't include global asm in `mir_keys`, fix error body synthesis

r? oli-obk

Fixes rust-lang#137470
Fixes rust-lang#137471
Fixes rust-lang#137472
Fixes rust-lang#137473

try-job: aarch64-apple
  • Loading branch information
bors committed Feb 26, 2025
2 parents cb06d12 + f68ff69 commit e4f2b0d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
3 changes: 2 additions & 1 deletion compiler/rustc_mir_build/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,8 @@ fn construct_error(tcx: TyCtxt<'_>, def_id: LocalDefId, guar: ErrorGuaranteed) -
| DefKind::AssocConst
| DefKind::AnonConst
| DefKind::InlineConst
| DefKind::Static { .. } => (vec![], tcx.type_of(def_id).instantiate_identity(), None),
| DefKind::Static { .. }
| DefKind::GlobalAsm => (vec![], tcx.type_of(def_id).instantiate_identity(), None),
DefKind::Ctor(..) | DefKind::Fn | DefKind::AssocFn => {
let sig = tcx.liberate_late_bound_regions(
def_id.to_def_id(),
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,10 @@ fn mir_keys(tcx: TyCtxt<'_>, (): ()) -> FxIndexSet<LocalDefId> {
// All body-owners have MIR associated with them.
let mut set: FxIndexSet<_> = tcx.hir_body_owners().collect();

// Remove the fake bodies for `global_asm!`, since they're not useful
// to be emitted (`--emit=mir`) or encoded (in metadata).
set.retain(|&def_id| !matches!(tcx.def_kind(def_id), DefKind::GlobalAsm));

// Coroutine-closures (e.g. async closures) have an additional by-move MIR
// body that isn't in the HIR.
for body_owner in tcx.hir_body_owners() {
Expand Down
22 changes: 22 additions & 0 deletions tests/ui/asm/global-asm-isnt-really-a-mir-body.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//@ revisions: emit_mir instrument cfi

// Make sure we don't try to emit MIR for it.
//@[emit_mir] compile-flags: --emit=mir

// Make sure we don't try to instrument it.
//@[instrument] compile-flags: -Cinstrument-coverage -Zno-profiler-runtime

// Make sure we don't try to CFI encode it.
//@[cfi] compile-flags: -Zsanitizer=cfi -Ccodegen-units=1 -Clto -Clink-dead-code=true
//@[cfi] needs-sanitizer-cfi
//@[cfi] no-prefer-dynamic

//@ build-pass

use std::arch::global_asm;

fn foo() {}

global_asm!("/* {} */", sym foo);

fn main() {}
9 changes: 9 additions & 0 deletions tests/ui/asm/global-asm-with-error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Ensure that we don't ICE when constructing the fake MIR body for a global
// asm when the body has errors. See #137470.

use std::arch::global_asm;

global_asm!("/* {} */", sym a);
//~^ ERROR cannot find value `a` in this scope

fn main() {}
9 changes: 9 additions & 0 deletions tests/ui/asm/global-asm-with-error.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0425]: cannot find value `a` in this scope
--> $DIR/global-asm-with-error.rs:6:29
|
LL | global_asm!("/* {} */", sym a);
| ^ not found in this scope

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0425`.

0 comments on commit e4f2b0d

Please sign in to comment.