Skip to content

Commit b4884c6

Browse files
katelyn a. martinnikomatsakis
andcommitted
pr review: abi debug assertion, exhaustive matches
### Add debug assertion to check `AbiDatas` ordering This makes a small alteration to `Abi::index`, so that we include a debug assertion to check that the index we are returning corresponds with the same abi in our data array. This will help prevent ordering bugs in the future, which can manifest in rather strange errors. ### Using exhaustive ABI matches This slightly modifies the changes from our previous commits, favoring exhaustive matches in place of `_ => ...` fall-through arms. This should help with maintenance in the future, when additional ABI's are added, or when existing ABI's are modified. Co-authored-by: Niko Matsakis <[email protected]>
1 parent af78f30 commit b4884c6

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

compiler/rustc_middle/src/ty/layout.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2556,7 +2556,24 @@ fn fn_can_unwind(
25562556
C { unwind } | Stdcall { unwind } | System { unwind } | Thiscall { unwind } => {
25572557
unwind
25582558
}
2559-
_ => false,
2559+
Cdecl
2560+
| Fastcall
2561+
| Vectorcall
2562+
| Aapcs
2563+
| Win64
2564+
| SysV64
2565+
| PtxKernel
2566+
| Msp430Interrupt
2567+
| X86Interrupt
2568+
| AmdGpuKernel
2569+
| EfiApi
2570+
| AvrInterrupt
2571+
| AvrNonBlockingInterrupt
2572+
| RustIntrinsic
2573+
| PlatformIntrinsic
2574+
| Unadjusted => false,
2575+
// In the `if` above, we checked for functions with the Rust calling convention.
2576+
Rust | RustCall => unreachable!(),
25602577
}
25612578
}
25622579
}

compiler/rustc_mir_build/src/build/mod.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,22 @@ fn should_abort_on_panic(tcx: TyCtxt<'_>, fn_def_id: LocalDefId, abi: Abi) -> bo
573573
// Rust and `rust-call` functions are allowed to unwind, and should not abort.
574574
Rust | RustCall => false,
575575
// Other ABI's should abort.
576-
_ => true,
576+
Cdecl
577+
| Fastcall
578+
| Vectorcall
579+
| Aapcs
580+
| Win64
581+
| SysV64
582+
| PtxKernel
583+
| Msp430Interrupt
584+
| X86Interrupt
585+
| AmdGpuKernel
586+
| EfiApi
587+
| AvrInterrupt
588+
| AvrNonBlockingInterrupt
589+
| RustIntrinsic
590+
| PlatformIntrinsic
591+
| Unadjusted => true,
577592
}
578593
}
579594
}

compiler/rustc_target/src/spec/abi.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl Abi {
100100
// N.B., this ordering MUST match the AbiDatas array above.
101101
// (This is ensured by the test indices_are_correct().)
102102
use Abi::*;
103-
match self {
103+
let i = match self {
104104
// Cross-platform ABIs
105105
Rust => 0,
106106
C { unwind: false } => 1,
@@ -130,7 +130,18 @@ impl Abi {
130130
RustCall => 23,
131131
PlatformIntrinsic => 24,
132132
Unadjusted => 25,
133-
}
133+
};
134+
debug_assert!(
135+
AbiDatas
136+
.iter()
137+
.enumerate()
138+
.find(|(_, AbiData { abi, .. })| *abi == self)
139+
.map(|(index, _)| index)
140+
.expect("abi variant has associated data")
141+
== i,
142+
"Abi index did not match `AbiDatas` ordering"
143+
);
144+
i
134145
}
135146

136147
#[inline]

0 commit comments

Comments
 (0)