Calculate the TargetDataLayout correctly for the selected target
This commit is contained in:
parent
9ed1829f1f
commit
33591cd3f4
@ -162,6 +162,7 @@ pub fn parse_with_proc_macros(
|
||||
Ok(Vec::new()),
|
||||
false,
|
||||
origin,
|
||||
None,
|
||||
);
|
||||
let prev = crates.insert(crate_name.clone(), crate_id);
|
||||
assert!(prev.is_none());
|
||||
@ -197,6 +198,7 @@ pub fn parse_with_proc_macros(
|
||||
Ok(Vec::new()),
|
||||
false,
|
||||
CrateOrigin::CratesIo { repo: None, name: None },
|
||||
None,
|
||||
);
|
||||
} else {
|
||||
for (from, to, prelude) in crate_deps {
|
||||
@ -234,6 +236,7 @@ pub fn parse_with_proc_macros(
|
||||
Ok(Vec::new()),
|
||||
false,
|
||||
CrateOrigin::Lang(LangCrateOrigin::Core),
|
||||
None,
|
||||
);
|
||||
|
||||
for krate in all_crates {
|
||||
@ -271,6 +274,7 @@ pub fn parse_with_proc_macros(
|
||||
Ok(proc_macro),
|
||||
true,
|
||||
CrateOrigin::CratesIo { repo: None, name: None },
|
||||
None,
|
||||
);
|
||||
|
||||
for krate in all_crates {
|
||||
|
@ -270,6 +270,7 @@ pub struct CrateData {
|
||||
pub display_name: Option<CrateDisplayName>,
|
||||
pub cfg_options: CfgOptions,
|
||||
pub potential_cfg_options: CfgOptions,
|
||||
pub target_layout: Option<Arc<str>>,
|
||||
pub env: Env,
|
||||
pub dependencies: Vec<Dependency>,
|
||||
pub proc_macro: ProcMacroLoadResult,
|
||||
@ -328,6 +329,7 @@ pub fn add_crate_root(
|
||||
proc_macro: ProcMacroLoadResult,
|
||||
is_proc_macro: bool,
|
||||
origin: CrateOrigin,
|
||||
target_layout: Option<Arc<str>>,
|
||||
) -> CrateId {
|
||||
let data = CrateData {
|
||||
root_file_id,
|
||||
@ -340,6 +342,7 @@ pub fn add_crate_root(
|
||||
proc_macro,
|
||||
dependencies: Vec::new(),
|
||||
origin,
|
||||
target_layout,
|
||||
is_proc_macro,
|
||||
};
|
||||
let crate_id = CrateId(self.arena.len() as u32);
|
||||
@ -649,6 +652,7 @@ fn detect_cyclic_dependency_indirect() {
|
||||
Ok(Vec::new()),
|
||||
false,
|
||||
CrateOrigin::CratesIo { repo: None, name: None },
|
||||
None,
|
||||
);
|
||||
let crate2 = graph.add_crate_root(
|
||||
FileId(2u32),
|
||||
@ -661,6 +665,7 @@ fn detect_cyclic_dependency_indirect() {
|
||||
Ok(Vec::new()),
|
||||
false,
|
||||
CrateOrigin::CratesIo { repo: None, name: None },
|
||||
None,
|
||||
);
|
||||
let crate3 = graph.add_crate_root(
|
||||
FileId(3u32),
|
||||
@ -673,6 +678,7 @@ fn detect_cyclic_dependency_indirect() {
|
||||
Ok(Vec::new()),
|
||||
false,
|
||||
CrateOrigin::CratesIo { repo: None, name: None },
|
||||
None,
|
||||
);
|
||||
assert!(graph
|
||||
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2))
|
||||
@ -699,6 +705,7 @@ fn detect_cyclic_dependency_direct() {
|
||||
Ok(Vec::new()),
|
||||
false,
|
||||
CrateOrigin::CratesIo { repo: None, name: None },
|
||||
None,
|
||||
);
|
||||
let crate2 = graph.add_crate_root(
|
||||
FileId(2u32),
|
||||
@ -711,6 +718,7 @@ fn detect_cyclic_dependency_direct() {
|
||||
Ok(Vec::new()),
|
||||
false,
|
||||
CrateOrigin::CratesIo { repo: None, name: None },
|
||||
None,
|
||||
);
|
||||
assert!(graph
|
||||
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2))
|
||||
@ -734,6 +742,7 @@ fn it_works() {
|
||||
Ok(Vec::new()),
|
||||
false,
|
||||
CrateOrigin::CratesIo { repo: None, name: None },
|
||||
None,
|
||||
);
|
||||
let crate2 = graph.add_crate_root(
|
||||
FileId(2u32),
|
||||
@ -746,6 +755,7 @@ fn it_works() {
|
||||
Ok(Vec::new()),
|
||||
false,
|
||||
CrateOrigin::CratesIo { repo: None, name: None },
|
||||
None,
|
||||
);
|
||||
let crate3 = graph.add_crate_root(
|
||||
FileId(3u32),
|
||||
@ -758,6 +768,7 @@ fn it_works() {
|
||||
Ok(Vec::new()),
|
||||
false,
|
||||
CrateOrigin::CratesIo { repo: None, name: None },
|
||||
None,
|
||||
);
|
||||
assert!(graph
|
||||
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2))
|
||||
@ -781,6 +792,7 @@ fn dashes_are_normalized() {
|
||||
Ok(Vec::new()),
|
||||
false,
|
||||
CrateOrigin::CratesIo { repo: None, name: None },
|
||||
None,
|
||||
);
|
||||
let crate2 = graph.add_crate_root(
|
||||
FileId(2u32),
|
||||
@ -793,6 +805,7 @@ fn dashes_are_normalized() {
|
||||
Ok(Vec::new()),
|
||||
false,
|
||||
CrateOrigin::CratesIo { repo: None, name: None },
|
||||
None,
|
||||
);
|
||||
assert!(graph
|
||||
.add_dep(
|
||||
|
@ -6,7 +6,7 @@
|
||||
pub use rustc_abi::{
|
||||
Abi, AbiAndPrefAlign, AddressSpace, Align, Endian, FieldsShape, Integer, IntegerType,
|
||||
LayoutCalculator, Niche, Primitive, ReprFlags, ReprOptions, Scalar, Size, StructKind,
|
||||
TargetDataLayout, WrappingRange,
|
||||
TargetDataLayout, TargetDataLayoutErrors, WrappingRange,
|
||||
};
|
||||
|
||||
use crate::LocalEnumVariantId;
|
||||
|
@ -64,8 +64,8 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
|
||||
#[salsa::cycle(crate::layout::layout_of_adt_recover)]
|
||||
fn layout_of_adt(&self, def: AdtId, subst: Substitution) -> Result<Layout, LayoutError>;
|
||||
|
||||
#[salsa::invoke(crate::layout::current_target_data_layout_query)]
|
||||
fn current_target_data_layout(&self) -> Arc<TargetDataLayout>;
|
||||
#[salsa::invoke(crate::layout::target_data_layout_query)]
|
||||
fn target_data_layout(&self, krate: CrateId) -> Arc<TargetDataLayout>;
|
||||
|
||||
#[salsa::invoke(crate::lower::callable_item_sig)]
|
||||
fn callable_item_signature(&self, def: CallableDefId) -> PolyFnSig;
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use base_db::CrateId;
|
||||
use chalk_ir::{AdtId, TyKind};
|
||||
use hir_def::{
|
||||
layout::{
|
||||
@ -17,7 +18,7 @@
|
||||
use self::adt::struct_variant_idx;
|
||||
pub use self::{
|
||||
adt::{layout_of_adt_query, layout_of_adt_recover},
|
||||
target::current_target_data_layout_query,
|
||||
target::target_data_layout_query,
|
||||
};
|
||||
|
||||
macro_rules! user_error {
|
||||
@ -31,6 +32,7 @@ macro_rules! user_error {
|
||||
|
||||
struct LayoutCx<'a> {
|
||||
db: &'a dyn HirDatabase,
|
||||
krate: CrateId,
|
||||
}
|
||||
|
||||
impl LayoutCalculator for LayoutCx<'_> {
|
||||
@ -41,7 +43,7 @@ fn delay_bug(&self, txt: &str) {
|
||||
}
|
||||
|
||||
fn current_data_layout(&self) -> Arc<TargetDataLayout> {
|
||||
self.db.current_target_data_layout()
|
||||
self.db.target_data_layout(self.krate)
|
||||
}
|
||||
}
|
||||
|
||||
@ -53,9 +55,9 @@ fn scalar(dl: &TargetDataLayout, value: Primitive) -> Layout {
|
||||
Layout::scalar(dl, scalar_unit(dl, value))
|
||||
}
|
||||
|
||||
pub fn layout_of_ty(db: &dyn HirDatabase, ty: &Ty) -> Result<Layout, LayoutError> {
|
||||
let dl = &*db.current_target_data_layout();
|
||||
let cx = LayoutCx { db };
|
||||
pub fn layout_of_ty(db: &dyn HirDatabase, ty: &Ty, krate: CrateId) -> Result<Layout, LayoutError> {
|
||||
let cx = LayoutCx { db, krate };
|
||||
let dl = &*cx.current_data_layout();
|
||||
Ok(match ty.kind(Interner) {
|
||||
TyKind::Adt(AdtId(def), subst) => db.layout_of_adt(*def, subst.clone())?,
|
||||
TyKind::Scalar(s) => match s {
|
||||
@ -84,7 +86,7 @@ pub fn layout_of_ty(db: &dyn HirDatabase, ty: &Ty) -> Result<Layout, LayoutError
|
||||
chalk_ir::IntTy::I64 => Integer::I64,
|
||||
chalk_ir::IntTy::I128 => Integer::I128,
|
||||
},
|
||||
false,
|
||||
true,
|
||||
),
|
||||
),
|
||||
chalk_ir::Scalar::Uint(i) => scalar(
|
||||
@ -98,7 +100,7 @@ pub fn layout_of_ty(db: &dyn HirDatabase, ty: &Ty) -> Result<Layout, LayoutError
|
||||
chalk_ir::UintTy::U64 => Integer::I64,
|
||||
chalk_ir::UintTy::U128 => Integer::I128,
|
||||
},
|
||||
true,
|
||||
false,
|
||||
),
|
||||
),
|
||||
chalk_ir::Scalar::Float(f) => scalar(
|
||||
@ -114,7 +116,7 @@ pub fn layout_of_ty(db: &dyn HirDatabase, ty: &Ty) -> Result<Layout, LayoutError
|
||||
|
||||
let fields = tys
|
||||
.iter(Interner)
|
||||
.map(|k| layout_of_ty(db, k.assert_ty_ref(Interner)))
|
||||
.map(|k| layout_of_ty(db, k.assert_ty_ref(Interner), krate))
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
let fields = fields.iter().collect::<Vec<_>>();
|
||||
let fields = fields.iter().collect::<Vec<_>>();
|
||||
@ -132,7 +134,7 @@ pub fn layout_of_ty(db: &dyn HirDatabase, ty: &Ty) -> Result<Layout, LayoutError
|
||||
},
|
||||
_ => return Err(LayoutError::HasPlaceholder),
|
||||
};
|
||||
let element = layout_of_ty(db, element)?;
|
||||
let element = layout_of_ty(db, element, krate)?;
|
||||
let size = element.size.checked_mul(count, dl).ok_or(LayoutError::SizeOverflow)?;
|
||||
|
||||
let abi = if count != 0 && matches!(element.abi, Abi::Uninhabited) {
|
||||
@ -153,7 +155,7 @@ pub fn layout_of_ty(db: &dyn HirDatabase, ty: &Ty) -> Result<Layout, LayoutError
|
||||
}
|
||||
}
|
||||
TyKind::Slice(element) => {
|
||||
let element = layout_of_ty(db, element)?;
|
||||
let element = layout_of_ty(db, element, krate)?;
|
||||
Layout {
|
||||
variants: Variants::Single { index: struct_variant_idx() },
|
||||
fields: FieldsShape::Array { stride: element.size, count: 0 },
|
||||
|
@ -5,7 +5,7 @@
|
||||
use hir_def::{
|
||||
adt::VariantData,
|
||||
layout::{Integer, IntegerExt, Layout, LayoutCalculator, LayoutError, RustcEnumVariantIdx},
|
||||
AdtId, EnumVariantId, LocalEnumVariantId, VariantId,
|
||||
AdtId, EnumVariantId, HasModule, LocalEnumVariantId, VariantId,
|
||||
};
|
||||
use la_arena::RawIdx;
|
||||
use smallvec::SmallVec;
|
||||
@ -23,12 +23,12 @@ pub fn layout_of_adt_query(
|
||||
def: AdtId,
|
||||
subst: Substitution,
|
||||
) -> Result<Layout, LayoutError> {
|
||||
let dl = db.current_target_data_layout();
|
||||
let cx = LayoutCx { db };
|
||||
let cx = LayoutCx { db, krate: def.module(db.upcast()).krate() };
|
||||
let dl = cx.current_data_layout();
|
||||
let handle_variant = |def: VariantId, var: &VariantData| {
|
||||
var.fields()
|
||||
.iter()
|
||||
.map(|(fd, _)| layout_of_ty(db, &field_ty(db, def, fd, &subst)))
|
||||
.map(|(fd, _)| layout_of_ty(db, &field_ty(db, def, fd, &subst), cx.krate))
|
||||
.collect::<Result<Vec<_>, _>>()
|
||||
};
|
||||
let (variants, is_enum, is_union, repr) = match def {
|
||||
|
@ -2,45 +2,130 @@
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use hir_def::layout::TargetDataLayout;
|
||||
use base_db::CrateId;
|
||||
use hir_def::layout::{TargetDataLayout, TargetDataLayoutErrors};
|
||||
|
||||
use crate::db::HirDatabase;
|
||||
|
||||
use hir_def::layout::{AbiAndPrefAlign, AddressSpace, Align, Endian, Integer, Size};
|
||||
use hir_def::layout::{AbiAndPrefAlign, AddressSpace, Align, Endian, Size};
|
||||
|
||||
pub fn current_target_data_layout_query(db: &dyn HirDatabase) -> Arc<TargetDataLayout> {
|
||||
pub fn target_data_layout_query(db: &dyn HirDatabase, krate: CrateId) -> Arc<TargetDataLayout> {
|
||||
let crate_graph = db.crate_graph();
|
||||
let cfg_options = &crate_graph[crate_graph.iter().next().unwrap()].cfg_options;
|
||||
let endian = match cfg_options.get_cfg_values("target_endian").next() {
|
||||
Some(x) if x.as_str() == "big" => Endian::Big,
|
||||
_ => Endian::Little,
|
||||
};
|
||||
let pointer_size =
|
||||
Size::from_bytes(match cfg_options.get_cfg_values("target_pointer_width").next() {
|
||||
Some(x) => match x.as_str() {
|
||||
"16" => 2,
|
||||
"32" => 4,
|
||||
_ => 8,
|
||||
},
|
||||
_ => 8,
|
||||
});
|
||||
// FIXME: These values are incorrect for many architectures, at least for aarch64 and riscv64,
|
||||
// use `rustc +nightly -Z unstable-options --print target-spec-json` or something similar instead.
|
||||
Arc::new(TargetDataLayout {
|
||||
endian,
|
||||
i1_align: AbiAndPrefAlign::new(Align::from_bytes(1).unwrap()),
|
||||
i8_align: AbiAndPrefAlign::new(Align::from_bytes(1).unwrap()),
|
||||
i16_align: AbiAndPrefAlign::new(Align::from_bytes(2).unwrap()),
|
||||
i32_align: AbiAndPrefAlign::new(Align::from_bytes(4).unwrap()),
|
||||
i64_align: AbiAndPrefAlign::new(Align::from_bytes(8).unwrap()),
|
||||
i128_align: AbiAndPrefAlign::new(Align::from_bytes(8).unwrap()),
|
||||
f32_align: AbiAndPrefAlign::new(Align::from_bytes(4).unwrap()),
|
||||
f64_align: AbiAndPrefAlign::new(Align::from_bytes(8).unwrap()),
|
||||
pointer_size,
|
||||
pointer_align: AbiAndPrefAlign::new(Align::from_bytes(pointer_size.bytes()).unwrap()),
|
||||
aggregate_align: AbiAndPrefAlign::new(Align::from_bytes(1).unwrap()),
|
||||
vector_align: vec![],
|
||||
instruction_address_space: AddressSpace(0),
|
||||
c_enum_min_size: Integer::I32,
|
||||
})
|
||||
let target_layout = &crate_graph[krate].target_layout;
|
||||
let cfg_options = &crate_graph[krate].cfg_options;
|
||||
Arc::new(
|
||||
target_layout
|
||||
.as_ref()
|
||||
.and_then(|it| parse_from_llvm_datalayout_string(it).ok())
|
||||
.unwrap_or_else(|| {
|
||||
let endian = match cfg_options.get_cfg_values("target_endian").next() {
|
||||
Some(x) if x.as_str() == "big" => Endian::Big,
|
||||
_ => Endian::Little,
|
||||
};
|
||||
let pointer_size = Size::from_bytes(
|
||||
match cfg_options.get_cfg_values("target_pointer_width").next() {
|
||||
Some(x) => match x.as_str() {
|
||||
"16" => 2,
|
||||
"32" => 4,
|
||||
_ => 8,
|
||||
},
|
||||
_ => 8,
|
||||
},
|
||||
);
|
||||
TargetDataLayout { endian, pointer_size, ..TargetDataLayout::default() }
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
/// copied from rustc as it is not exposed yet
|
||||
fn parse_from_llvm_datalayout_string<'a>(
|
||||
input: &'a str,
|
||||
) -> Result<TargetDataLayout, TargetDataLayoutErrors<'a>> {
|
||||
// Parse an address space index from a string.
|
||||
let parse_address_space = |s: &'a str, cause: &'a str| {
|
||||
s.parse::<u32>().map(AddressSpace).map_err(|err| {
|
||||
TargetDataLayoutErrors::InvalidAddressSpace { addr_space: s, cause, err }
|
||||
})
|
||||
};
|
||||
|
||||
// Parse a bit count from a string.
|
||||
let parse_bits = |s: &'a str, kind: &'a str, cause: &'a str| {
|
||||
s.parse::<u64>().map_err(|err| TargetDataLayoutErrors::InvalidBits {
|
||||
kind,
|
||||
bit: s,
|
||||
cause,
|
||||
err,
|
||||
})
|
||||
};
|
||||
|
||||
// Parse a size string.
|
||||
let size = |s: &'a str, cause: &'a str| parse_bits(s, "size", cause).map(Size::from_bits);
|
||||
|
||||
// Parse an alignment string.
|
||||
let align = |s: &[&'a str], cause: &'a str| {
|
||||
if s.is_empty() {
|
||||
return Err(TargetDataLayoutErrors::MissingAlignment { cause });
|
||||
}
|
||||
let align_from_bits = |bits| {
|
||||
Align::from_bits(bits)
|
||||
.map_err(|err| TargetDataLayoutErrors::InvalidAlignment { cause, err })
|
||||
};
|
||||
let abi = parse_bits(s[0], "alignment", cause)?;
|
||||
let pref = s.get(1).map_or(Ok(abi), |pref| parse_bits(pref, "alignment", cause))?;
|
||||
Ok(AbiAndPrefAlign { abi: align_from_bits(abi)?, pref: align_from_bits(pref)? })
|
||||
};
|
||||
|
||||
let mut dl = TargetDataLayout::default();
|
||||
let mut i128_align_src = 64;
|
||||
for spec in input.split('-') {
|
||||
let spec_parts = spec.split(':').collect::<Vec<_>>();
|
||||
|
||||
match &*spec_parts {
|
||||
["e"] => dl.endian = Endian::Little,
|
||||
["E"] => dl.endian = Endian::Big,
|
||||
[p] if p.starts_with('P') => {
|
||||
dl.instruction_address_space = parse_address_space(&p[1..], "P")?
|
||||
}
|
||||
["a", ref a @ ..] => dl.aggregate_align = align(a, "a")?,
|
||||
["f32", ref a @ ..] => dl.f32_align = align(a, "f32")?,
|
||||
["f64", ref a @ ..] => dl.f64_align = align(a, "f64")?,
|
||||
[p @ "p", s, ref a @ ..] | [p @ "p0", s, ref a @ ..] => {
|
||||
dl.pointer_size = size(s, p)?;
|
||||
dl.pointer_align = align(a, p)?;
|
||||
}
|
||||
[s, ref a @ ..] if s.starts_with('i') => {
|
||||
let Ok(bits) = s[1..].parse::<u64>() else {
|
||||
size(&s[1..], "i")?; // For the user error.
|
||||
continue;
|
||||
};
|
||||
let a = align(a, s)?;
|
||||
match bits {
|
||||
1 => dl.i1_align = a,
|
||||
8 => dl.i8_align = a,
|
||||
16 => dl.i16_align = a,
|
||||
32 => dl.i32_align = a,
|
||||
64 => dl.i64_align = a,
|
||||
_ => {}
|
||||
}
|
||||
if bits >= i128_align_src && bits <= 128 {
|
||||
// Default alignment for i128 is decided by taking the alignment of
|
||||
// largest-sized i{64..=128}.
|
||||
i128_align_src = bits;
|
||||
dl.i128_align = a;
|
||||
}
|
||||
}
|
||||
[s, ref a @ ..] if s.starts_with('v') => {
|
||||
let v_size = size(&s[1..], "v")?;
|
||||
let a = align(a, s)?;
|
||||
if let Some(v) = dl.vector_align.iter_mut().find(|v| v.0 == v_size) {
|
||||
v.1 = a;
|
||||
continue;
|
||||
}
|
||||
// No existing entry, add a new one.
|
||||
dl.vector_align.push((v_size, a));
|
||||
}
|
||||
_ => {} // Ignore everything else.
|
||||
}
|
||||
}
|
||||
Ok(dl)
|
||||
}
|
||||
|
@ -34,15 +34,17 @@ fn eval_goal(ra_fixture: &str) -> Result<Layout, LayoutError> {
|
||||
})
|
||||
.unwrap();
|
||||
let goal_ty = TyKind::Adt(AdtId(adt_id), Substitution::empty(Interner)).intern(Interner);
|
||||
layout_of_ty(&db, &goal_ty)
|
||||
layout_of_ty(&db, &goal_ty, module_id.krate())
|
||||
}
|
||||
|
||||
#[track_caller]
|
||||
fn check_size_and_align(ra_fixture: &str, size: u64, align: u64) {
|
||||
let l = eval_goal(ra_fixture).unwrap();
|
||||
assert_eq!(l.size.bytes(), size);
|
||||
assert_eq!(l.align.abi.bytes(), align);
|
||||
}
|
||||
|
||||
#[track_caller]
|
||||
fn check_fail(ra_fixture: &str, e: LayoutError) {
|
||||
let r = eval_goal(ra_fixture);
|
||||
assert_eq!(r, Err(e));
|
||||
|
@ -866,7 +866,7 @@ pub fn ty(&self, db: &dyn HirDatabase) -> Type {
|
||||
}
|
||||
|
||||
pub fn layout(&self, db: &dyn HirDatabase) -> Result<Layout, LayoutError> {
|
||||
layout_of_ty(db, &self.ty(db).ty)
|
||||
layout_of_ty(db, &self.ty(db).ty, self.parent.module(db).krate().into())
|
||||
}
|
||||
|
||||
pub fn parent_def(&self, _db: &dyn HirDatabase) -> VariantDef {
|
||||
|
@ -236,6 +236,7 @@ pub fn from_single_file(text: String) -> (Analysis, FileId) {
|
||||
Ok(Vec::new()),
|
||||
false,
|
||||
CrateOrigin::CratesIo { repo: None, name: None },
|
||||
None,
|
||||
);
|
||||
change.change_file(file_id, Some(Arc::new(text)));
|
||||
change.set_crate_graph(crate_graph);
|
||||
|
@ -36,6 +36,7 @@ pub(crate) fn shuffle_crate_graph(db: &mut RootDatabase) {
|
||||
data.proc_macro.clone(),
|
||||
data.is_proc_macro,
|
||||
data.origin.clone(),
|
||||
data.target_layout.clone(),
|
||||
);
|
||||
map.insert(old_id, new_id);
|
||||
}
|
||||
|
@ -50,7 +50,7 @@ fn get_rust_cfgs(
|
||||
cargo_config.envs(extra_env);
|
||||
cargo_config
|
||||
.current_dir(cargo_toml.parent())
|
||||
.args(&["-Z", "unstable-options", "rustc", "--print", "cfg"])
|
||||
.args(&["rustc", "-Z", "unstable-options", "--print", "cfg"])
|
||||
.env("RUSTC_BOOTSTRAP", "1");
|
||||
if let Some(target) = target {
|
||||
cargo_config.args(&["--target", target]);
|
||||
|
@ -29,6 +29,7 @@ fn load_cargo_with_overrides(file: &str, cfg_overrides: CfgOverrides) -> CrateGr
|
||||
rustc_cfg: Vec::new(),
|
||||
cfg_overrides,
|
||||
toolchain: None,
|
||||
target_layout: None,
|
||||
};
|
||||
to_crate_graph(project_workspace)
|
||||
}
|
||||
|
@ -79,6 +79,7 @@ pub enum ProjectWorkspace {
|
||||
rustc_cfg: Vec<CfgFlag>,
|
||||
cfg_overrides: CfgOverrides,
|
||||
toolchain: Option<Version>,
|
||||
target_layout: Option<String>,
|
||||
},
|
||||
/// Project workspace was manually specified using a `rust-project.json` file.
|
||||
Json { project: ProjectJson, sysroot: Option<Sysroot>, rustc_cfg: Vec<CfgFlag> },
|
||||
@ -108,6 +109,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
rustc_cfg,
|
||||
cfg_overrides,
|
||||
toolchain,
|
||||
target_layout: data_layout,
|
||||
} => f
|
||||
.debug_struct("Cargo")
|
||||
.field("root", &cargo.workspace_root().file_name())
|
||||
@ -120,6 +122,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
.field("n_rustc_cfg", &rustc_cfg.len())
|
||||
.field("n_cfg_overrides", &cfg_overrides.len())
|
||||
.field("toolchain", &toolchain)
|
||||
.field("data_layout", &data_layout)
|
||||
.finish(),
|
||||
ProjectWorkspace::Json { project, sysroot, rustc_cfg } => {
|
||||
let mut debug_struct = f.debug_struct("Json");
|
||||
@ -140,6 +143,40 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
}
|
||||
}
|
||||
|
||||
fn data_layout(
|
||||
cargo_toml: Option<&ManifestPath>,
|
||||
target: Option<&str>,
|
||||
extra_env: &FxHashMap<String, String>,
|
||||
) -> Option<String> {
|
||||
let output = (|| {
|
||||
if let Some(cargo_toml) = cargo_toml {
|
||||
let mut cmd = Command::new(toolchain::rustc());
|
||||
cmd.envs(extra_env);
|
||||
cmd.current_dir(cargo_toml.parent())
|
||||
.args(&["-Z", "unstable-options", "rustc", "--print", "target-spec-json"])
|
||||
.env("RUSTC_BOOTSTRAP", "1");
|
||||
if let Some(target) = target {
|
||||
cmd.args(&["--target", target]);
|
||||
}
|
||||
match utf8_stdout(cmd) {
|
||||
Ok(it) => return Ok(it),
|
||||
Err(e) => tracing::debug!("{e:?}: falling back to querying rustc for cfgs"),
|
||||
}
|
||||
}
|
||||
// using unstable cargo features failed, fall back to using plain rustc
|
||||
let mut cmd = Command::new(toolchain::rustc());
|
||||
cmd.envs(extra_env)
|
||||
.args(&["-Z", "unstable-options", "rustc", "--print", "target-spec-json"])
|
||||
.env("RUSTC_BOOTSTRAP", "1");
|
||||
if let Some(target) = target {
|
||||
cmd.args(&["--target", target]);
|
||||
}
|
||||
utf8_stdout(cmd)
|
||||
})()
|
||||
.ok()?;
|
||||
Some(output.split_once(r#""data-layout": "#)?.1.trim_matches('"').to_owned())
|
||||
}
|
||||
|
||||
impl ProjectWorkspace {
|
||||
pub fn load(
|
||||
manifest: ProjectManifest,
|
||||
@ -241,6 +278,8 @@ pub fn load(
|
||||
rustc_cfg::get(Some(&cargo_toml), config.target.as_deref(), &config.extra_env);
|
||||
|
||||
let cfg_overrides = config.cfg_overrides();
|
||||
let data_layout =
|
||||
data_layout(Some(&cargo_toml), config.target.as_deref(), &config.extra_env);
|
||||
ProjectWorkspace::Cargo {
|
||||
cargo,
|
||||
build_scripts: WorkspaceBuildScripts::default(),
|
||||
@ -249,6 +288,7 @@ pub fn load(
|
||||
rustc_cfg,
|
||||
cfg_overrides,
|
||||
toolchain,
|
||||
target_layout: data_layout,
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -435,6 +475,7 @@ pub fn to_roots(&self) -> Vec<PackageRoot> {
|
||||
cfg_overrides: _,
|
||||
build_scripts,
|
||||
toolchain: _,
|
||||
target_layout: _,
|
||||
} => {
|
||||
cargo
|
||||
.packages()
|
||||
@ -530,6 +571,7 @@ pub fn to_crate_graph(
|
||||
project,
|
||||
sysroot,
|
||||
extra_env,
|
||||
None,
|
||||
),
|
||||
ProjectWorkspace::Cargo {
|
||||
cargo,
|
||||
@ -539,6 +581,7 @@ pub fn to_crate_graph(
|
||||
cfg_overrides,
|
||||
build_scripts,
|
||||
toolchain: _,
|
||||
target_layout,
|
||||
} => cargo_to_crate_graph(
|
||||
load_proc_macro,
|
||||
load,
|
||||
@ -548,9 +591,10 @@ pub fn to_crate_graph(
|
||||
rustc_cfg.clone(),
|
||||
cfg_overrides,
|
||||
build_scripts,
|
||||
target_layout.as_deref().map(Arc::from),
|
||||
),
|
||||
ProjectWorkspace::DetachedFiles { files, sysroot, rustc_cfg } => {
|
||||
detached_files_to_crate_graph(rustc_cfg.clone(), load, files, sysroot)
|
||||
detached_files_to_crate_graph(rustc_cfg.clone(), load, files, sysroot, None)
|
||||
}
|
||||
};
|
||||
if crate_graph.patch_cfg_if() {
|
||||
@ -569,11 +613,18 @@ fn project_json_to_crate_graph(
|
||||
project: &ProjectJson,
|
||||
sysroot: &Option<Sysroot>,
|
||||
extra_env: &FxHashMap<String, String>,
|
||||
target_layout: Option<Arc<str>>,
|
||||
) -> CrateGraph {
|
||||
let mut crate_graph = CrateGraph::default();
|
||||
let sysroot_deps = sysroot
|
||||
.as_ref()
|
||||
.map(|sysroot| sysroot_to_crate_graph(&mut crate_graph, sysroot, rustc_cfg.clone(), load));
|
||||
let sysroot_deps = sysroot.as_ref().map(|sysroot| {
|
||||
sysroot_to_crate_graph(
|
||||
&mut crate_graph,
|
||||
sysroot,
|
||||
rustc_cfg.clone(),
|
||||
target_layout.clone(),
|
||||
load,
|
||||
)
|
||||
});
|
||||
|
||||
let mut cfg_cache: FxHashMap<&str, Vec<CfgFlag>> = FxHashMap::default();
|
||||
let crates: NoHashHashMap<CrateId, CrateId> = project
|
||||
@ -625,6 +676,7 @@ fn project_json_to_crate_graph(
|
||||
} else {
|
||||
CrateOrigin::CratesIo { repo: None, name: None }
|
||||
},
|
||||
target_layout.clone(),
|
||||
),
|
||||
)
|
||||
})
|
||||
@ -665,11 +717,18 @@ fn cargo_to_crate_graph(
|
||||
rustc_cfg: Vec<CfgFlag>,
|
||||
override_cfg: &CfgOverrides,
|
||||
build_scripts: &WorkspaceBuildScripts,
|
||||
target_layout: Option<Arc<str>>,
|
||||
) -> CrateGraph {
|
||||
let _p = profile::span("cargo_to_crate_graph");
|
||||
let mut crate_graph = CrateGraph::default();
|
||||
let (public_deps, libproc_macro) = match sysroot {
|
||||
Some(sysroot) => sysroot_to_crate_graph(&mut crate_graph, sysroot, rustc_cfg.clone(), load),
|
||||
Some(sysroot) => sysroot_to_crate_graph(
|
||||
&mut crate_graph,
|
||||
sysroot,
|
||||
rustc_cfg.clone(),
|
||||
target_layout.clone(),
|
||||
load,
|
||||
),
|
||||
None => (SysrootPublicDeps::default(), None),
|
||||
};
|
||||
|
||||
@ -732,6 +791,7 @@ fn cargo_to_crate_graph(
|
||||
file_id,
|
||||
&cargo[tgt].name,
|
||||
cargo[tgt].is_proc_macro,
|
||||
target_layout.clone(),
|
||||
);
|
||||
if cargo[tgt].kind == TargetKind::Lib {
|
||||
lib_tgt = Some((crate_id, cargo[tgt].name.clone()));
|
||||
@ -811,6 +871,7 @@ fn cargo_to_crate_graph(
|
||||
&cfg_options,
|
||||
override_cfg,
|
||||
build_scripts,
|
||||
target_layout,
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -822,11 +883,18 @@ fn detached_files_to_crate_graph(
|
||||
load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
|
||||
detached_files: &[AbsPathBuf],
|
||||
sysroot: &Option<Sysroot>,
|
||||
target_layout: Option<Arc<str>>,
|
||||
) -> CrateGraph {
|
||||
let _p = profile::span("detached_files_to_crate_graph");
|
||||
let mut crate_graph = CrateGraph::default();
|
||||
let (public_deps, _libproc_macro) = match sysroot {
|
||||
Some(sysroot) => sysroot_to_crate_graph(&mut crate_graph, sysroot, rustc_cfg.clone(), load),
|
||||
Some(sysroot) => sysroot_to_crate_graph(
|
||||
&mut crate_graph,
|
||||
sysroot,
|
||||
rustc_cfg.clone(),
|
||||
target_layout.clone(),
|
||||
load,
|
||||
),
|
||||
None => (SysrootPublicDeps::default(), None),
|
||||
};
|
||||
|
||||
@ -859,6 +927,7 @@ fn detached_files_to_crate_graph(
|
||||
repo: None,
|
||||
name: display_name.map(|n| n.canonical_name().to_string()),
|
||||
},
|
||||
target_layout.clone(),
|
||||
);
|
||||
|
||||
public_deps.add_to_crate_graph(&mut crate_graph, detached_file_crate);
|
||||
@ -879,6 +948,7 @@ fn handle_rustc_crates(
|
||||
cfg_options: &CfgOptions,
|
||||
override_cfg: &CfgOverrides,
|
||||
build_scripts: &WorkspaceBuildScripts,
|
||||
target_layout: Option<Arc<str>>,
|
||||
) {
|
||||
let mut rustc_pkg_crates = FxHashMap::default();
|
||||
// The root package of the rustc-dev component is rustc_driver, so we match that
|
||||
@ -935,6 +1005,7 @@ fn handle_rustc_crates(
|
||||
file_id,
|
||||
&rustc_workspace[tgt].name,
|
||||
rustc_workspace[tgt].is_proc_macro,
|
||||
target_layout.clone(),
|
||||
);
|
||||
pkg_to_lib_crate.insert(pkg, crate_id);
|
||||
// Add dependencies on core / std / alloc for this crate
|
||||
@ -999,6 +1070,7 @@ fn add_target_crate_root(
|
||||
file_id: FileId,
|
||||
cargo_name: &str,
|
||||
is_proc_macro: bool,
|
||||
target_layout: Option<Arc<str>>,
|
||||
) -> CrateId {
|
||||
let edition = pkg.edition;
|
||||
let mut potential_cfg_options = cfg_options.clone();
|
||||
@ -1045,6 +1117,7 @@ fn add_target_crate_root(
|
||||
proc_macro,
|
||||
is_proc_macro,
|
||||
CrateOrigin::CratesIo { repo: pkg.repository.clone(), name: Some(pkg.name.clone()) },
|
||||
target_layout,
|
||||
)
|
||||
}
|
||||
|
||||
@ -1066,6 +1139,7 @@ fn sysroot_to_crate_graph(
|
||||
crate_graph: &mut CrateGraph,
|
||||
sysroot: &Sysroot,
|
||||
rustc_cfg: Vec<CfgFlag>,
|
||||
target_layout: Option<Arc<str>>,
|
||||
load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
|
||||
) -> (SysrootPublicDeps, Option<CrateId>) {
|
||||
let _p = profile::span("sysroot_to_crate_graph");
|
||||
@ -1089,6 +1163,7 @@ fn sysroot_to_crate_graph(
|
||||
Err("no proc macro loaded for sysroot crate".into()),
|
||||
false,
|
||||
CrateOrigin::Lang(LangCrateOrigin::from(&*sysroot[krate].name)),
|
||||
target_layout.clone(),
|
||||
);
|
||||
Some((krate, crate_id))
|
||||
})
|
||||
|
@ -226,6 +226,7 @@ fn eq_ignore_build_data<'a>(
|
||||
|
||||
build_scripts: _,
|
||||
toolchain: _,
|
||||
target_layout: _,
|
||||
} => Some((cargo, sysroot, rustc, rustc_cfg, cfg_overrides)),
|
||||
_ => None,
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user