Skip to content

Commit 883fe10

Browse files
authored
Rollup merge of #65884 - Centril:non-hardcoded-abis, r=petrochenkov
syntax: ABI-oblivious grammar This PR has the following effects: 1. `extern $lit` is now legal where `$lit:literal` and `$lit` is substituted for a string literal. 2. `extern "abi_that_does_not_exist"` is now *syntactically* legal whereas before, the set of ABI strings was hard-coded into the grammar of the language. With this PR, the set of ABIs are instead validated and translated during lowering. That seems more appropriate. 3. `ast::FloatTy` is now distinct from `rustc_target::abi::FloatTy`. The former is used substantially more and the translation between them is only necessary in a single place. 4. As a result of 2-3, libsyntax no longer depends on librustc_target, which should improve pipe-lining somewhat. cc @rust-lang/lang -- the points 1-2 slightly change the definition of the language but in a way which seems consistent with our general principles (in particular wrt. the discussions of turning things into semantic errors). I expect this to be uncontroversial but it's worth letting y'all know. :) r? @varkor
2 parents a3c8572 + 55f76cd commit 883fe10

Some content is hidden

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

46 files changed

+520
-466
lines changed

Cargo.lock

-3
Original file line numberDiff line numberDiff line change
@@ -3762,7 +3762,6 @@ dependencies = [
37623762
"rustc",
37633763
"rustc_codegen_utils",
37643764
"rustc_data_structures",
3765-
"rustc_target",
37663765
"serde_json",
37673766
"syntax",
37683767
"syntax_pos",
@@ -4362,7 +4361,6 @@ dependencies = [
43624361
"rustc_errors",
43634362
"rustc_index",
43644363
"rustc_lexer",
4365-
"rustc_target",
43664364
"scoped-tls",
43674365
"serialize",
43684366
"smallvec 1.0.0",
@@ -4380,7 +4378,6 @@ dependencies = [
43804378
"rustc_errors",
43814379
"rustc_index",
43824380
"rustc_lexer",
4383-
"rustc_target",
43844381
"scoped-tls",
43854382
"serialize",
43864383
"smallvec 1.0.0",

src/librustc/error_codes.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2336,6 +2336,7 @@ the future, [RFC 2091] prohibits their implementation without a follow-up RFC.
23362336
E0657, // `impl Trait` can only capture lifetimes bound at the fn level
23372337
E0687, // in-band lifetimes cannot be used in `fn`/`Fn` syntax
23382338
E0688, // in-band lifetimes cannot be mixed with explicit lifetime binders
2339+
E0703, // invalid ABI
23392340
// E0707, // multiple elided lifetimes used in arguments of `async fn`
23402341
E0708, // `async` non-`move` closures with parameters are not currently
23412342
// supported

src/librustc/hir/lowering.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1216,7 +1216,7 @@ impl<'a> LoweringContext<'a> {
12161216
ImplTraitContext::disallowed(),
12171217
),
12181218
unsafety: this.lower_unsafety(f.unsafety),
1219-
abi: f.abi,
1219+
abi: this.lower_abi(f.abi),
12201220
decl: this.lower_fn_decl(&f.decl, None, false, None),
12211221
param_names: this.lower_fn_params_to_names(&f.decl),
12221222
}))

src/librustc/hir/lowering/item.rs

+23-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::hir::def::{Res, DefKind};
1212
use crate::util::nodemap::NodeMap;
1313

1414
use rustc_data_structures::thin_vec::ThinVec;
15+
use rustc_target::spec::abi;
1516

1617
use std::collections::BTreeSet;
1718
use smallvec::SmallVec;
@@ -735,7 +736,7 @@ impl LoweringContext<'_> {
735736

736737
fn lower_foreign_mod(&mut self, fm: &ForeignMod) -> hir::ForeignMod {
737738
hir::ForeignMod {
738-
abi: fm.abi,
739+
abi: self.lower_abi(fm.abi),
739740
items: fm.items
740741
.iter()
741742
.map(|x| self.lower_foreign_item(x))
@@ -1291,10 +1292,30 @@ impl LoweringContext<'_> {
12911292
unsafety: self.lower_unsafety(h.unsafety),
12921293
asyncness: self.lower_asyncness(h.asyncness.node),
12931294
constness: self.lower_constness(h.constness),
1294-
abi: h.abi,
1295+
abi: self.lower_abi(h.abi),
12951296
}
12961297
}
12971298

1299+
pub(super) fn lower_abi(&mut self, abi: Abi) -> abi::Abi {
1300+
abi::lookup(&abi.symbol.as_str()).unwrap_or_else(|| {
1301+
self.error_on_invalid_abi(abi);
1302+
abi::Abi::Rust
1303+
})
1304+
}
1305+
1306+
fn error_on_invalid_abi(&self, abi: Abi) {
1307+
struct_span_err!(
1308+
self.sess,
1309+
abi.span,
1310+
E0703,
1311+
"invalid ABI: found `{}`",
1312+
abi.symbol
1313+
)
1314+
.span_label(abi.span, "invalid ABI")
1315+
.help(&format!("valid ABIs: {}", abi::all_names().join(", ")))
1316+
.emit();
1317+
}
1318+
12981319
pub(super) fn lower_unsafety(&mut self, u: Unsafety) -> hir::Unsafety {
12991320
match u {
13001321
Unsafety::Unsafe => hir::Unsafety::Unsafe,

src/librustc/ich/impls_syntax.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ for ::syntax::attr::StabilityLevel {
124124

125125
impl_stable_hash_for!(struct ::syntax::attr::RustcDeprecation { since, reason, suggestion });
126126

127-
128127
impl_stable_hash_for!(enum ::syntax::attr::IntType {
129128
SignedInt(int_ty),
130129
UnsignedInt(uint_ty)
@@ -136,6 +135,11 @@ impl_stable_hash_for!(enum ::syntax::ast::LitIntType {
136135
Unsuffixed
137136
});
138137

138+
impl_stable_hash_for!(enum ::syntax::ast::LitFloatType {
139+
Suffixed(float_ty),
140+
Unsuffixed
141+
});
142+
139143
impl_stable_hash_for!(struct ::syntax::ast::Lit {
140144
kind,
141145
token,
@@ -148,8 +152,7 @@ impl_stable_hash_for!(enum ::syntax::ast::LitKind {
148152
Byte(value),
149153
Char(value),
150154
Int(value, lit_int_type),
151-
Float(value, float_ty),
152-
FloatUnsuffixed(value),
155+
Float(value, lit_float_type),
153156
Bool(value),
154157
Err(value)
155158
});
@@ -159,6 +162,7 @@ impl_stable_hash_for_spanned!(::syntax::ast::LitKind);
159162
impl_stable_hash_for!(enum ::syntax::ast::IntTy { Isize, I8, I16, I32, I64, I128 });
160163
impl_stable_hash_for!(enum ::syntax::ast::UintTy { Usize, U8, U16, U32, U64, U128 });
161164
impl_stable_hash_for!(enum ::syntax::ast::FloatTy { F32, F64 });
165+
impl_stable_hash_for!(enum ::rustc_target::abi::FloatTy { F32, F64 });
162166
impl_stable_hash_for!(enum ::syntax::ast::Unsafety { Unsafe, Normal });
163167
impl_stable_hash_for!(enum ::syntax::ast::Constness { Const, NotConst });
164168
impl_stable_hash_for!(enum ::syntax::ast::Defaultness { Default, Final });

src/librustc/ty/layout.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,10 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
538538
ty::Uint(ity) => {
539539
scalar(Int(Integer::from_attr(dl, attr::UnsignedInt(ity)), false))
540540
}
541-
ty::Float(fty) => scalar(Float(fty)),
541+
ty::Float(fty) => scalar(Float(match fty {
542+
ast::FloatTy::F32 => FloatTy::F32,
543+
ast::FloatTy::F64 => FloatTy::F64,
544+
})),
542545
ty::FnPtr(_) => {
543546
let mut ptr = scalar_unit(Pointer);
544547
ptr.valid_range = 1..=*ptr.valid_range.end();

src/librustc/ty/print/obsolete.rs

+3-15
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use rustc::ty::{self, Const, Instance, Ty, TyCtxt};
1212
use rustc::{bug, hir};
1313
use std::fmt::Write;
1414
use std::iter;
15-
use syntax::ast;
1615

1716
/// Same as `unique_type_name()` but with the result pushed onto the given
1817
/// `output` parameter.
@@ -39,20 +38,9 @@ impl DefPathBasedNames<'tcx> {
3938
ty::Char => output.push_str("char"),
4039
ty::Str => output.push_str("str"),
4140
ty::Never => output.push_str("!"),
42-
ty::Int(ast::IntTy::Isize) => output.push_str("isize"),
43-
ty::Int(ast::IntTy::I8) => output.push_str("i8"),
44-
ty::Int(ast::IntTy::I16) => output.push_str("i16"),
45-
ty::Int(ast::IntTy::I32) => output.push_str("i32"),
46-
ty::Int(ast::IntTy::I64) => output.push_str("i64"),
47-
ty::Int(ast::IntTy::I128) => output.push_str("i128"),
48-
ty::Uint(ast::UintTy::Usize) => output.push_str("usize"),
49-
ty::Uint(ast::UintTy::U8) => output.push_str("u8"),
50-
ty::Uint(ast::UintTy::U16) => output.push_str("u16"),
51-
ty::Uint(ast::UintTy::U32) => output.push_str("u32"),
52-
ty::Uint(ast::UintTy::U64) => output.push_str("u64"),
53-
ty::Uint(ast::UintTy::U128) => output.push_str("u128"),
54-
ty::Float(ast::FloatTy::F32) => output.push_str("f32"),
55-
ty::Float(ast::FloatTy::F64) => output.push_str("f64"),
41+
ty::Int(ty) => output.push_str(ty.name_str()),
42+
ty::Uint(ty) => output.push_str(ty.name_str()),
43+
ty::Float(ty) => output.push_str(ty.name_str()),
5644
ty::Adt(adt_def, substs) => {
5745
self.push_def_path(adt_def.did, output);
5846
self.push_generic_params(substs, iter::empty(), output, debug);

src/librustc/ty/print/pretty.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,9 @@ pub trait PrettyPrinter<'tcx>:
466466
match ty.kind {
467467
ty::Bool => p!(write("bool")),
468468
ty::Char => p!(write("char")),
469-
ty::Int(t) => p!(write("{}", t.ty_to_string())),
470-
ty::Uint(t) => p!(write("{}", t.ty_to_string())),
471-
ty::Float(t) => p!(write("{}", t.ty_to_string())),
469+
ty::Int(t) => p!(write("{}", t.name_str())),
470+
ty::Uint(t) => p!(write("{}", t.name_str())),
471+
ty::Float(t) => p!(write("{}", t.name_str())),
472472
ty::RawPtr(ref tm) => {
473473
p!(write("*{} ", match tm.mutbl {
474474
hir::MutMutable => "mut",
@@ -895,10 +895,11 @@ pub trait PrettyPrinter<'tcx>:
895895
let bit_size = Integer::from_attr(&self.tcx(), UnsignedInt(*ui)).size();
896896
let max = truncate(u128::max_value(), bit_size);
897897

898+
let ui_str = ui.name_str();
898899
if data == max {
899-
p!(write("std::{}::MAX", ui))
900+
p!(write("std::{}::MAX", ui_str))
900901
} else {
901-
p!(write("{}{}", data, ui))
902+
p!(write("{}{}", data, ui_str))
902903
};
903904
},
904905
(ConstValue::Scalar(Scalar::Raw { data, .. }), ty::Int(i)) => {
@@ -911,10 +912,11 @@ pub trait PrettyPrinter<'tcx>:
911912
let size = self.tcx().layout_of(ty::ParamEnv::empty().and(ty))
912913
.unwrap()
913914
.size;
915+
let i_str = i.name_str();
914916
match data {
915-
d if d == min => p!(write("std::{}::MIN", i)),
916-
d if d == max => p!(write("std::{}::MAX", i)),
917-
_ => p!(write("{}{}", sign_extend(data, size) as i128, i))
917+
d if d == min => p!(write("std::{}::MIN", i_str)),
918+
d if d == max => p!(write("std::{}::MAX", i_str)),
919+
_ => p!(write("{}{}", sign_extend(data, size) as i128, i_str))
918920
}
919921
},
920922
(ConstValue::Scalar(Scalar::Raw { data, .. }), ty::Char) =>

src/librustc_codegen_llvm/debuginfo/metadata.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -843,13 +843,13 @@ fn basic_type_metadata(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>) -> &'ll DIType {
843843
ty::Bool => ("bool", DW_ATE_boolean),
844844
ty::Char => ("char", DW_ATE_unsigned_char),
845845
ty::Int(int_ty) => {
846-
(int_ty.ty_to_string(), DW_ATE_signed)
846+
(int_ty.name_str(), DW_ATE_signed)
847847
},
848848
ty::Uint(uint_ty) => {
849-
(uint_ty.ty_to_string(), DW_ATE_unsigned)
849+
(uint_ty.name_str(), DW_ATE_unsigned)
850850
},
851851
ty::Float(float_ty) => {
852-
(float_ty.ty_to_string(), DW_ATE_float)
852+
(float_ty.name_str(), DW_ATE_float)
853853
},
854854
_ => bug!("debuginfo::basic_type_metadata - t is invalid type")
855855
};

src/librustc_codegen_llvm/intrinsic.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use rustc::ty::layout::{self, LayoutOf, HasTyCtxt, Primitive};
1818
use rustc::mir::interpret::GlobalId;
1919
use rustc_codegen_ssa::common::{IntPredicate, TypeKind};
2020
use rustc::hir;
21-
use syntax::ast::{self, FloatTy};
22-
use rustc_target::abi::HasDataLayout;
21+
use rustc_target::abi::{FloatTy, HasDataLayout};
22+
use syntax::ast;
2323

2424
use rustc_codegen_ssa::common::span_invalid_monomorphization_error;
2525
use rustc_codegen_ssa::traits::*;
@@ -1335,7 +1335,7 @@ fn generic_simd_intrinsic(
13351335
},
13361336
ty::Float(f) => {
13371337
return_error!("unsupported element type `{}` of floating-point vector `{}`",
1338-
f, in_ty);
1338+
f.name_str(), in_ty);
13391339
},
13401340
_ => {
13411341
return_error!("`{}` is not a floating-point type", in_ty);

src/librustc_codegen_ssa/debuginfo/type_names.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ pub fn push_debuginfo_type_name<'tcx>(
3737
ty::Char => output.push_str("char"),
3838
ty::Str => output.push_str("str"),
3939
ty::Never => output.push_str("!"),
40-
ty::Int(int_ty) => output.push_str(int_ty.ty_to_string()),
41-
ty::Uint(uint_ty) => output.push_str(uint_ty.ty_to_string()),
42-
ty::Float(float_ty) => output.push_str(float_ty.ty_to_string()),
40+
ty::Int(int_ty) => output.push_str(int_ty.name_str()),
41+
ty::Uint(uint_ty) => output.push_str(uint_ty.name_str()),
42+
ty::Float(float_ty) => output.push_str(float_ty.name_str()),
4343
ty::Foreign(def_id) => push_item_name(tcx, def_id, qualified, output),
4444
ty::Adt(def, substs) => {
4545
push_item_name(tcx, def.did, qualified, output);

src/librustc_interface/Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ rustc_codegen_utils = { path = "../librustc_codegen_utils" }
2727
rustc_metadata = { path = "../librustc_metadata" }
2828
rustc_mir = { path = "../librustc_mir" }
2929
rustc_passes = { path = "../librustc_passes" }
30-
rustc_target = { path = "../librustc_target" }
3130
rustc_typeck = { path = "../librustc_typeck" }
3231
rustc_lint = { path = "../librustc_lint" }
3332
rustc_errors = { path = "../librustc_errors" }
@@ -36,3 +35,6 @@ rustc_privacy = { path = "../librustc_privacy" }
3635
rustc_resolve = { path = "../librustc_resolve" }
3736
tempfile = "3.0.5"
3837
once_cell = "1"
38+
39+
[dev-dependencies]
40+
rustc_target = { path = "../librustc_target" }

0 commit comments

Comments
 (0)