Skip to content

Commit dfde455

Browse files
authored
Unrolled build for rust-lang#136603
Rollup merge of rust-lang#136603 - workingjubilee:move-abi-versioning-into-ast, r=compiler-errors compiler: gate `extern "{abi}"` in ast_lowering I don't believe low-level crates like `rustc_abi` should have to know or care about higher-level concerns like whether the ABI string is stable for users. These implementation details can be made less open to public inspection. This way the code that governs stability is near the code that enforces stability, and compiled together. It also abstracts away certain error messages instead of constantly repeating them. A few error messages are simply deleted outright, instead of made uniform, because they are either too dated to be useful or redundant with other diagnostic improvements we could make. These can be pursued in followups: my first concern was making sure there wasn't unnecessary diagnostics-related code in `rustc_abi`, which is not well-positioned to understand what kind of errors are going to be generated based on how it is used. r? ``@ghost``
2 parents ffa9afe + cd9d39e commit dfde455

File tree

62 files changed

+395
-420
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+395
-420
lines changed

Diff for: Cargo.lock

+3-1
Original file line numberDiff line numberDiff line change
@@ -3317,7 +3317,6 @@ dependencies = [
33173317
"rand 0.8.5",
33183318
"rand_xoshiro",
33193319
"rustc_data_structures",
3320-
"rustc_feature",
33213320
"rustc_index",
33223321
"rustc_macros",
33233322
"rustc_serialize",
@@ -3379,6 +3378,7 @@ dependencies = [
33793378
"rustc_ast_pretty",
33803379
"rustc_data_structures",
33813380
"rustc_errors",
3381+
"rustc_feature",
33823382
"rustc_fluent_macro",
33833383
"rustc_hir",
33843384
"rustc_index",
@@ -3683,6 +3683,7 @@ version = "0.0.0"
36833683
dependencies = [
36843684
"ctrlc",
36853685
"libc",
3686+
"rustc_abi",
36863687
"rustc_ast",
36873688
"rustc_ast_lowering",
36883689
"rustc_ast_passes",
@@ -4337,6 +4338,7 @@ version = "0.0.0"
43374338
dependencies = [
43384339
"rustc_abi",
43394340
"rustc_ast",
4341+
"rustc_ast_lowering",
43404342
"rustc_ast_pretty",
43414343
"rustc_attr_parsing",
43424344
"rustc_data_structures",

Diff for: compiler/rustc_abi/Cargo.toml

-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ bitflags = "2.4.1"
99
rand = { version = "0.8.4", default-features = false, optional = true }
1010
rand_xoshiro = { version = "0.6.0", optional = true }
1111
rustc_data_structures = { path = "../rustc_data_structures", optional = true }
12-
rustc_feature = { path = "../rustc_feature", optional = true }
1312
rustc_index = { path = "../rustc_index", default-features = false }
1413
rustc_macros = { path = "../rustc_macros", optional = true }
1514
rustc_serialize = { path = "../rustc_serialize", optional = true }
@@ -24,7 +23,6 @@ default = ["nightly", "randomize"]
2423
# without depending on rustc_data_structures, rustc_macros and rustc_serialize
2524
nightly = [
2625
"dep:rustc_data_structures",
27-
"dep:rustc_feature",
2826
"dep:rustc_macros",
2927
"dep:rustc_serialize",
3028
"dep:rustc_span",

Diff for: compiler/rustc_abi/src/callconv.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#[cfg(feature = "nightly")]
2-
use crate::{BackendRepr, FieldsShape, TyAbiInterface, TyAndLayout};
3-
use crate::{Primitive, Size, Variants};
2+
use crate::{BackendRepr, FieldsShape, Primitive, Size, TyAbiInterface, TyAndLayout, Variants};
43

54
mod reg;
65

Diff for: compiler/rustc_abi/src/extern_abi.rs

+9-121
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::fmt;
22

33
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
4-
use rustc_span::{Span, Symbol, sym};
54

65
#[cfg(test)]
76
mod tests;
@@ -95,14 +94,14 @@ impl Abi {
9594

9695
#[derive(Copy, Clone)]
9796
pub struct AbiData {
98-
abi: Abi,
97+
pub abi: Abi,
9998

10099
/// Name of this ABI as we like it called.
101-
name: &'static str,
100+
pub name: &'static str,
102101
}
103102

104103
#[allow(non_upper_case_globals)]
105-
const AbiDatas: &[AbiData] = &[
104+
pub const AbiDatas: &[AbiData] = &[
106105
AbiData { abi: Abi::Rust, name: "Rust" },
107106
AbiData { abi: Abi::C { unwind: false }, name: "C" },
108107
AbiData { abi: Abi::C { unwind: true }, name: "C-unwind" },
@@ -142,129 +141,18 @@ const AbiDatas: &[AbiData] = &[
142141
];
143142

144143
#[derive(Copy, Clone, Debug)]
145-
pub enum AbiUnsupported {
146-
Unrecognized,
147-
Reason { explain: &'static str },
148-
}
149-
144+
pub struct AbiUnsupported {}
150145
/// Returns the ABI with the given name (if any).
151146
pub fn lookup(name: &str) -> Result<Abi, AbiUnsupported> {
152-
AbiDatas.iter().find(|abi_data| name == abi_data.name).map(|&x| x.abi).ok_or_else(|| match name {
153-
"riscv-interrupt" => AbiUnsupported::Reason {
154-
explain: "please use one of riscv-interrupt-m or riscv-interrupt-s for machine- or supervisor-level interrupts, respectively",
155-
},
156-
"riscv-interrupt-u" => AbiUnsupported::Reason {
157-
explain: "user-mode interrupt handlers have been removed from LLVM pending standardization, see: https://reviews.llvm.org/D149314",
158-
},
159-
"wasm" => AbiUnsupported::Reason {
160-
explain: "non-standard wasm ABI is no longer supported",
161-
},
162-
163-
_ => AbiUnsupported::Unrecognized,
164-
165-
})
166-
}
167-
168-
pub fn all_names() -> Vec<&'static str> {
169-
AbiDatas.iter().map(|d| d.name).collect()
170-
}
171-
172-
pub fn enabled_names(features: &rustc_feature::Features, span: Span) -> Vec<&'static str> {
173147
AbiDatas
174148
.iter()
175-
.map(|d| d.name)
176-
.filter(|name| is_enabled(features, span, name).is_ok())
177-
.collect()
149+
.find(|abi_data| name == abi_data.name)
150+
.map(|&x| x.abi)
151+
.ok_or_else(|| AbiUnsupported {})
178152
}
179153

180-
pub enum AbiDisabled {
181-
Unstable { feature: Symbol, explain: &'static str },
182-
Unrecognized,
183-
}
184-
185-
pub fn is_enabled(
186-
features: &rustc_feature::Features,
187-
span: Span,
188-
name: &str,
189-
) -> Result<(), AbiDisabled> {
190-
let s = is_stable(name);
191-
if let Err(AbiDisabled::Unstable { feature, .. }) = s {
192-
if features.enabled(feature) || span.allows_unstable(feature) {
193-
return Ok(());
194-
}
195-
}
196-
s
197-
}
198-
199-
/// Returns whether the ABI is stable to use.
200-
///
201-
/// Note that there is a separate check determining whether the ABI is even supported
202-
/// on the current target; see `fn is_abi_supported` in `rustc_target::spec`.
203-
pub fn is_stable(name: &str) -> Result<(), AbiDisabled> {
204-
match name {
205-
// Stable
206-
"Rust" | "C" | "C-unwind" | "cdecl" | "cdecl-unwind" | "stdcall" | "stdcall-unwind"
207-
| "fastcall" | "fastcall-unwind" | "aapcs" | "aapcs-unwind" | "win64" | "win64-unwind"
208-
| "sysv64" | "sysv64-unwind" | "system" | "system-unwind" | "efiapi" | "thiscall"
209-
| "thiscall-unwind" => Ok(()),
210-
"rust-intrinsic" => Err(AbiDisabled::Unstable {
211-
feature: sym::intrinsics,
212-
explain: "intrinsics are subject to change",
213-
}),
214-
"vectorcall" => Err(AbiDisabled::Unstable {
215-
feature: sym::abi_vectorcall,
216-
explain: "vectorcall is experimental and subject to change",
217-
}),
218-
"vectorcall-unwind" => Err(AbiDisabled::Unstable {
219-
feature: sym::abi_vectorcall,
220-
explain: "vectorcall-unwind ABI is experimental and subject to change",
221-
}),
222-
"rust-call" => Err(AbiDisabled::Unstable {
223-
feature: sym::unboxed_closures,
224-
explain: "rust-call ABI is subject to change",
225-
}),
226-
"rust-cold" => Err(AbiDisabled::Unstable {
227-
feature: sym::rust_cold_cc,
228-
explain: "rust-cold is experimental and subject to change",
229-
}),
230-
"ptx-kernel" => Err(AbiDisabled::Unstable {
231-
feature: sym::abi_ptx,
232-
explain: "PTX ABIs are experimental and subject to change",
233-
}),
234-
"unadjusted" => Err(AbiDisabled::Unstable {
235-
feature: sym::abi_unadjusted,
236-
explain: "unadjusted ABI is an implementation detail and perma-unstable",
237-
}),
238-
"msp430-interrupt" => Err(AbiDisabled::Unstable {
239-
feature: sym::abi_msp430_interrupt,
240-
explain: "msp430-interrupt ABI is experimental and subject to change",
241-
}),
242-
"x86-interrupt" => Err(AbiDisabled::Unstable {
243-
feature: sym::abi_x86_interrupt,
244-
explain: "x86-interrupt ABI is experimental and subject to change",
245-
}),
246-
"gpu-kernel" => Err(AbiDisabled::Unstable {
247-
feature: sym::abi_gpu_kernel,
248-
explain: "gpu-kernel ABI is experimental and subject to change",
249-
}),
250-
"avr-interrupt" | "avr-non-blocking-interrupt" => Err(AbiDisabled::Unstable {
251-
feature: sym::abi_avr_interrupt,
252-
explain: "avr-interrupt and avr-non-blocking-interrupt ABIs are experimental and subject to change",
253-
}),
254-
"riscv-interrupt-m" | "riscv-interrupt-s" => Err(AbiDisabled::Unstable {
255-
feature: sym::abi_riscv_interrupt,
256-
explain: "riscv-interrupt ABIs are experimental and subject to change",
257-
}),
258-
"C-cmse-nonsecure-call" => Err(AbiDisabled::Unstable {
259-
feature: sym::abi_c_cmse_nonsecure_call,
260-
explain: "C-cmse-nonsecure-call ABI is experimental and subject to change",
261-
}),
262-
"C-cmse-nonsecure-entry" => Err(AbiDisabled::Unstable {
263-
feature: sym::cmse_nonsecure_entry,
264-
explain: "C-cmse-nonsecure-entry ABI is experimental and subject to change",
265-
}),
266-
_ => Err(AbiDisabled::Unrecognized),
267-
}
154+
pub fn all_names() -> Vec<&'static str> {
155+
AbiDatas.iter().map(|d| d.name).collect()
268156
}
269157

270158
impl Abi {

Diff for: compiler/rustc_abi/src/extern_abi/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn lookup_cdecl() {
1818
#[test]
1919
fn lookup_baz() {
2020
let abi = lookup("baz");
21-
assert_matches!(abi, Err(AbiUnsupported::Unrecognized));
21+
assert_matches!(abi, Err(AbiUnsupported {}));
2222
}
2323

2424
#[test]

Diff for: compiler/rustc_abi/src/lib.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@ mod extern_abi;
6666

6767
pub use callconv::{Heterogeneous, HomogeneousAggregate, Reg, RegKind};
6868
#[cfg(feature = "nightly")]
69-
pub use extern_abi::{
70-
AbiDisabled, AbiUnsupported, ExternAbi, all_names, enabled_names, is_enabled, is_stable, lookup,
71-
};
69+
pub use extern_abi::{AbiDatas, AbiUnsupported, ExternAbi, all_names, lookup};
7270
#[cfg(feature = "nightly")]
7371
pub use layout::{FIRST_VARIANT, FieldIdx, Layout, TyAbiInterface, TyAndLayout, VariantIdx};
7472
pub use layout::{LayoutCalculator, LayoutCalculatorError};

Diff for: compiler/rustc_ast_lowering/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ rustc_ast = { path = "../rustc_ast" }
1313
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
1414
rustc_data_structures = { path = "../rustc_data_structures" }
1515
rustc_errors = { path = "../rustc_errors" }
16+
rustc_feature = { path = "../rustc_feature" }
1617
rustc_fluent_macro = { path = "../rustc_fluent_macro" }
1718
rustc_hir = { path = "../rustc_hir" }
1819
rustc_index = { path = "../rustc_index" }

Diff for: compiler/rustc_ast_lowering/src/delegation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ use std::iter;
4141
use ast::visit::Visitor;
4242
use hir::def::{DefKind, PartialRes, Res};
4343
use hir::{BodyId, HirId};
44+
use rustc_abi::ExternAbi;
4445
use rustc_ast::*;
4546
use rustc_errors::ErrorGuaranteed;
4647
use rustc_hir::def_id::DefId;
4748
use rustc_middle::span_bug;
4849
use rustc_middle::ty::{Asyncness, ResolverAstLowering};
4950
use rustc_span::{Ident, Span};
50-
use rustc_target::spec::abi;
5151
use {rustc_ast as ast, rustc_hir as hir};
5252

5353
use super::{GenericArgsMode, ImplTraitContext, LoweringContext, ParamMode};
@@ -398,7 +398,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
398398
safety: hir::Safety::Safe.into(),
399399
constness: hir::Constness::NotConst,
400400
asyncness: hir::IsAsync::NotAsync,
401-
abi: abi::Abi::Rust,
401+
abi: ExternAbi::Rust,
402402
}
403403
}
404404

Diff for: compiler/rustc_ast_lowering/src/errors.rs

+1-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
use rustc_errors::DiagArgFromDisplay;
12
use rustc_errors::codes::*;
2-
use rustc_errors::{Diag, DiagArgFromDisplay, EmissionGuarantee, SubdiagMessageOp, Subdiagnostic};
33
use rustc_macros::{Diagnostic, Subdiagnostic};
44
use rustc_span::{Ident, Span, Symbol};
55

@@ -32,8 +32,6 @@ pub(crate) struct InvalidAbi {
3232
pub abi: Symbol,
3333
pub command: String,
3434
#[subdiagnostic]
35-
pub explain: Option<InvalidAbiReason>,
36-
#[subdiagnostic]
3735
pub suggestion: Option<InvalidAbiSuggestion>,
3836
}
3937

@@ -45,19 +43,6 @@ pub(crate) struct TupleStructWithDefault {
4543
pub span: Span,
4644
}
4745

48-
pub(crate) struct InvalidAbiReason(pub &'static str);
49-
50-
impl Subdiagnostic for InvalidAbiReason {
51-
fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
52-
self,
53-
diag: &mut Diag<'_, G>,
54-
_: &F,
55-
) {
56-
#[allow(rustc::untranslatable_diagnostic)]
57-
diag.note(self.0);
58-
}
59-
}
60-
6146
#[derive(Subdiagnostic)]
6247
#[suggestion(
6348
ast_lowering_invalid_abi_suggestion,

Diff for: compiler/rustc_ast_lowering/src/item.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ use thin_vec::ThinVec;
1717
use tracing::instrument;
1818

1919
use super::errors::{
20-
InvalidAbi, InvalidAbiReason, InvalidAbiSuggestion, MisplacedRelaxTraitBound,
21-
TupleStructWithDefault,
20+
InvalidAbi, InvalidAbiSuggestion, MisplacedRelaxTraitBound, TupleStructWithDefault,
2221
};
22+
use super::stability::{enabled_names, gate_unstable_abi};
2323
use super::{
2424
AstOwner, FnDeclKind, ImplTraitContext, ImplTraitPosition, LoweringContext, ParamMode,
2525
ResolverAstLoweringExt,
@@ -1479,11 +1479,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
14791479
}
14801480
}
14811481

1482-
pub(super) fn lower_abi(&mut self, abi: StrLit) -> ExternAbi {
1483-
rustc_abi::lookup(abi.symbol_unescaped.as_str()).unwrap_or_else(|err| {
1484-
self.error_on_invalid_abi(abi, err);
1482+
pub(super) fn lower_abi(&mut self, abi_str: StrLit) -> ExternAbi {
1483+
let ast::StrLit { symbol_unescaped, span, .. } = abi_str;
1484+
let extern_abi = rustc_abi::lookup(symbol_unescaped.as_str()).unwrap_or_else(|_| {
1485+
self.error_on_invalid_abi(abi_str);
14851486
ExternAbi::Rust
1486-
})
1487+
});
1488+
let sess = self.tcx.sess;
1489+
let features = self.tcx.features();
1490+
gate_unstable_abi(sess, features, span, extern_abi);
1491+
extern_abi
14871492
}
14881493

14891494
pub(super) fn lower_extern(&mut self, ext: Extern) -> ExternAbi {
@@ -1494,19 +1499,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
14941499
}
14951500
}
14961501

1497-
fn error_on_invalid_abi(&self, abi: StrLit, err: rustc_abi::AbiUnsupported) {
1498-
let abi_names = rustc_abi::enabled_names(self.tcx.features(), abi.span)
1502+
fn error_on_invalid_abi(&self, abi: StrLit) {
1503+
let abi_names = enabled_names(self.tcx.features(), abi.span)
14991504
.iter()
15001505
.map(|s| Symbol::intern(s))
15011506
.collect::<Vec<_>>();
15021507
let suggested_name = find_best_match_for_name(&abi_names, abi.symbol_unescaped, None);
15031508
self.dcx().emit_err(InvalidAbi {
15041509
abi: abi.symbol_unescaped,
15051510
span: abi.span,
1506-
explain: match err {
1507-
rustc_abi::AbiUnsupported::Reason { explain } => Some(InvalidAbiReason(explain)),
1508-
_ => None,
1509-
},
15101511
suggestion: suggested_name.map(|suggested_name| InvalidAbiSuggestion {
15111512
span: abi.span,
15121513
suggestion: format!("\"{suggested_name}\""),

Diff for: compiler/rustc_ast_lowering/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ mod index;
8484
mod item;
8585
mod pat;
8686
mod path;
87+
pub mod stability;
8788

8889
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
8990

0 commit comments

Comments
 (0)