Skip to content

Commit 83995f3

Browse files
committed
Auto merge of rust-lang#115354 - matthiaskrgr:rollup-4cotcxz, r=matthiaskrgr
Rollup of 6 pull requests Successful merges: - rust-lang#111580 (Don't ICE on layout computation failure) - rust-lang#114923 (doc: update lld-flavor ref) - rust-lang#115174 (tests: add test for rust-lang#67992) - rust-lang#115187 (Add new interface to smir) - rust-lang#115300 (Tweaks and improvements on SMIR around generics_of and predicates_of) - rust-lang#115340 (some more is_zst that should be is_1zst) r? `@ghost` `@rustbot` modify labels: rollup
2 parents b2515fa + a644f37 commit 83995f3

File tree

21 files changed

+203
-117
lines changed

21 files changed

+203
-117
lines changed

Cargo.lock

+3
Original file line numberDiff line numberDiff line change
@@ -4335,8 +4335,11 @@ dependencies = [
43354335
name = "rustc_smir"
43364336
version = "0.0.0"
43374337
dependencies = [
4338+
"rustc_driver",
43384339
"rustc_hir",
4340+
"rustc_interface",
43394341
"rustc_middle",
4342+
"rustc_session",
43404343
"rustc_span",
43414344
"rustc_target",
43424345
"scoped-tls",

compiler/rustc_codegen_cranelift/src/common.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ impl<'tcx> LayoutOfHelpers<'tcx> for RevealAllLayoutCx<'tcx> {
480480
if let LayoutError::SizeOverflow(_) | LayoutError::ReferencesError(_) = err {
481481
self.0.sess.span_fatal(span, err.to_string())
482482
} else {
483-
span_bug!(span, "failed to get layout for `{}`: {}", ty, err)
483+
self.0.sess.span_fatal(span, format!("failed to get layout for `{}`: {}", ty, err))
484484
}
485485
}
486486
}

compiler/rustc_codegen_gcc/src/context.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_codegen_ssa::traits::{
77
BaseTypeMethods,
88
MiscMethods,
99
};
10+
use rustc_codegen_ssa::errors as ssa_errors;
1011
use rustc_data_structures::base_n;
1112
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1213
use rustc_middle::span_bug;
@@ -479,7 +480,7 @@ impl<'gcc, 'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'gcc, 'tcx> {
479480
if let LayoutError::SizeOverflow(_) | LayoutError::ReferencesError(_) = err {
480481
self.sess().emit_fatal(respan(span, err.into_diagnostic()))
481482
} else {
482-
span_bug!(span, "failed to get layout for `{}`: {}", ty, err)
483+
self.tcx.sess.emit_fatal(ssa_errors::FailedToGetLayout { span, ty, err })
483484
}
484485
}
485486
}

compiler/rustc_codegen_llvm/src/context.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::value::Value;
1010

1111
use cstr::cstr;
1212
use rustc_codegen_ssa::base::{wants_msvc_seh, wants_wasm_eh};
13+
use rustc_codegen_ssa::errors as ssa_errors;
1314
use rustc_codegen_ssa::traits::*;
1415
use rustc_data_structures::base_n;
1516
use rustc_data_structures::fx::FxHashMap;
@@ -1000,7 +1001,7 @@ impl<'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'_, 'tcx> {
10001001
if let LayoutError::SizeOverflow(_) | LayoutError::ReferencesError(_) = err {
10011002
self.sess().emit_fatal(Spanned { span, node: err.into_diagnostic() })
10021003
} else {
1003-
span_bug!(span, "failed to get layout for `{ty}`: {err:?}")
1004+
self.tcx.sess.emit_fatal(ssa_errors::FailedToGetLayout { span, ty, err })
10041005
}
10051006
}
10061007
}

compiler/rustc_codegen_ssa/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ codegen_ssa_extract_bundled_libs_parse_archive = failed to parse archive '{$rlib
3535
codegen_ssa_extract_bundled_libs_read_entry = failed to read entry '{$rlib}': {$error}
3636
codegen_ssa_extract_bundled_libs_write_file = failed to write file '{$rlib}': {$error}
3737
38+
codegen_ssa_failed_to_get_layout = failed to get layout for {$ty}: {$err}
39+
3840
codegen_ssa_failed_to_write = failed to write {$path}: {$error}
3941
4042
codegen_ssa_ignoring_emit_path = ignoring emit path because multiple .{$extension} files were produced

compiler/rustc_codegen_ssa/src/errors.rs

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_errors::{
77
IntoDiagnosticArg,
88
};
99
use rustc_macros::Diagnostic;
10+
use rustc_middle::ty::layout::LayoutError;
1011
use rustc_middle::ty::Ty;
1112
use rustc_span::{Span, Symbol};
1213
use rustc_type_ir::FloatTy;
@@ -1030,6 +1031,15 @@ pub struct TargetFeatureSafeTrait {
10301031
pub def: Span,
10311032
}
10321033

1034+
#[derive(Diagnostic)]
1035+
#[diag(codegen_ssa_failed_to_get_layout)]
1036+
pub struct FailedToGetLayout<'tcx> {
1037+
#[primary_span]
1038+
pub span: Span,
1039+
pub ty: Ty<'tcx>,
1040+
pub err: LayoutError<'tcx>,
1041+
}
1042+
10331043
#[derive(Diagnostic)]
10341044
#[diag(codegen_ssa_error_creating_remark_dir)]
10351045
pub struct ErrorCreatingRemarkDir {

compiler/rustc_hir_analysis/src/coherence/builtin.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ fn visit_implementation_of_dispatch_from_dyn(tcx: TyCtxt<'_>, impl_did: LocalDef
195195
let ty_b = field.ty(tcx, args_b);
196196

197197
if let Ok(layout) = tcx.layout_of(param_env.and(ty_a)) {
198-
if layout.is_zst() && layout.align.abi.bytes() == 1 {
199-
// ignore ZST fields with alignment of 1 byte
198+
if layout.is_1zst() {
199+
// ignore 1-ZST fields
200200
return false;
201201
}
202202
}

compiler/rustc_lint/src/types.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ pub(crate) fn nonnull_optimization_guaranteed<'tcx>(
804804
tcx.has_attr(def.did(), sym::rustc_nonnull_optimization_guaranteed)
805805
}
806806

807-
/// `repr(transparent)` structs can have a single non-ZST field, this function returns that
807+
/// `repr(transparent)` structs can have a single non-1-ZST field, this function returns that
808808
/// field.
809809
pub fn transparent_newtype_field<'a, 'tcx>(
810810
tcx: TyCtxt<'tcx>,
@@ -813,8 +813,8 @@ pub fn transparent_newtype_field<'a, 'tcx>(
813813
let param_env = tcx.param_env(variant.def_id);
814814
variant.fields.iter().find(|field| {
815815
let field_ty = tcx.type_of(field.did).instantiate_identity();
816-
let is_zst = tcx.layout_of(param_env.and(field_ty)).is_ok_and(|layout| layout.is_zst());
817-
!is_zst
816+
let is_1zst = tcx.layout_of(param_env.and(field_ty)).is_ok_and(|layout| layout.is_1zst());
817+
!is_1zst
818818
})
819819
}
820820

compiler/rustc_middle/src/ty/layout.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use crate::query::TyCtxtAt;
44
use crate::ty::normalize_erasing_regions::NormalizationError;
55
use crate::ty::{self, ConstKind, ReprOptions, Ty, TyCtxt, TypeVisitableExt};
66
use rustc_error_messages::DiagnosticMessage;
7-
use rustc_errors::{DiagnosticBuilder, Handler, IntoDiagnostic};
7+
use rustc_errors::{
8+
DiagnosticArgValue, DiagnosticBuilder, Handler, IntoDiagnostic, IntoDiagnosticArg,
9+
};
810
use rustc_hir as hir;
911
use rustc_hir::def_id::DefId;
1012
use rustc_index::IndexVec;
@@ -265,6 +267,12 @@ impl<'tcx> fmt::Display for LayoutError<'tcx> {
265267
}
266268
}
267269

270+
impl<'tcx> IntoDiagnosticArg for LayoutError<'tcx> {
271+
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
272+
self.to_string().into_diagnostic_arg()
273+
}
274+
}
275+
268276
#[derive(Clone, Copy)]
269277
pub struct LayoutCx<'tcx, C> {
270278
pub tcx: C,

compiler/rustc_smir/Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ rustc_hir = { path = "../rustc_hir", optional = true }
99
rustc_middle = { path = "../rustc_middle", optional = true }
1010
rustc_span = { path = "../rustc_span", optional = true }
1111
rustc_target = { path = "../rustc_target", optional = true }
12+
rustc_driver = { path = "../rustc_driver", optional = true }
13+
rustc_interface = { path = "../rustc_interface", optional = true}
14+
rustc_session = {path = "../rustc_session", optional = true}
1215
tracing = "0.1"
1316
scoped-tls = "1.0"
1417

@@ -18,4 +21,7 @@ default = [
1821
"rustc_middle",
1922
"rustc_span",
2023
"rustc_target",
24+
"rustc_driver",
25+
"rustc_interface",
26+
"rustc_session",
2127
]

compiler/rustc_smir/src/rustc_internal/mod.rs

+52-17
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44
//! until stable MIR is complete.
55
66
use std::fmt::Debug;
7+
use std::ops::Index;
78
use std::string::ToString;
89

10+
use crate::rustc_internal;
911
use crate::{
1012
rustc_smir::Tables,
1113
stable_mir::{self, with},
1214
};
15+
use rustc_driver::{Callbacks, Compilation, RunCompiler};
16+
use rustc_interface::{interface, Queries};
1317
use rustc_middle::ty::TyCtxt;
18+
use rustc_session::EarlyErrorHandler;
1419
pub use rustc_span::def_id::{CrateNum, DefId};
1520

1621
fn with_tables<R>(mut f: impl FnMut(&mut Tables<'_>) -> R) -> R {
@@ -20,7 +25,7 @@ fn with_tables<R>(mut f: impl FnMut(&mut Tables<'_>) -> R) -> R {
2025
}
2126

2227
pub fn item_def_id(item: &stable_mir::CrateItem) -> DefId {
23-
with_tables(|t| t.item_def_id(item))
28+
with_tables(|t| t[item.0])
2429
}
2530

2631
pub fn crate_item(did: DefId) -> stable_mir::CrateItem {
@@ -67,23 +72,16 @@ pub fn impl_def(did: DefId) -> stable_mir::ty::ImplDef {
6772
with_tables(|t| t.impl_def(did))
6873
}
6974

70-
impl<'tcx> Tables<'tcx> {
71-
pub fn item_def_id(&self, item: &stable_mir::CrateItem) -> DefId {
72-
self.def_ids[item.0]
73-
}
75+
impl<'tcx> Index<stable_mir::DefId> for Tables<'tcx> {
76+
type Output = DefId;
7477

75-
pub fn trait_def_id(&self, trait_def: &stable_mir::ty::TraitDef) -> DefId {
76-
self.def_ids[trait_def.0]
77-
}
78-
79-
pub fn impl_trait_def_id(&self, impl_def: &stable_mir::ty::ImplDef) -> DefId {
80-
self.def_ids[impl_def.0]
81-
}
82-
83-
pub fn generic_def_id(&self, generic_def: &stable_mir::ty::GenericDef) -> DefId {
84-
self.def_ids[generic_def.0]
78+
#[inline(always)]
79+
fn index(&self, index: stable_mir::DefId) -> &Self::Output {
80+
&self.def_ids[index.0]
8581
}
82+
}
8683

84+
impl<'tcx> Tables<'tcx> {
8785
pub fn crate_item(&mut self, did: DefId) -> stable_mir::CrateItem {
8886
stable_mir::CrateItem(self.create_def_id(did))
8987
}
@@ -140,12 +138,12 @@ impl<'tcx> Tables<'tcx> {
140138
// FIXME: this becomes inefficient when we have too many ids
141139
for (i, &d) in self.def_ids.iter().enumerate() {
142140
if d == did {
143-
return i;
141+
return stable_mir::DefId(i);
144142
}
145143
}
146144
let id = self.def_ids.len();
147145
self.def_ids.push(did);
148-
id
146+
stable_mir::DefId(id)
149147
}
150148
}
151149

@@ -163,3 +161,40 @@ pub type Opaque = impl Debug + ToString + Clone;
163161
pub(crate) fn opaque<T: Debug>(value: &T) -> Opaque {
164162
format!("{value:?}")
165163
}
164+
165+
pub struct StableMir {
166+
args: Vec<String>,
167+
callback: fn(TyCtxt<'_>),
168+
}
169+
170+
impl StableMir {
171+
/// Creates a new `StableMir` instance, with given test_function and arguments.
172+
pub fn new(args: Vec<String>, callback: fn(TyCtxt<'_>)) -> Self {
173+
StableMir { args, callback }
174+
}
175+
176+
/// Runs the compiler against given target and tests it with `test_function`
177+
pub fn run(&mut self) {
178+
rustc_driver::catch_fatal_errors(|| {
179+
RunCompiler::new(&self.args.clone(), self).run().unwrap();
180+
})
181+
.unwrap();
182+
}
183+
}
184+
185+
impl Callbacks for StableMir {
186+
/// Called after analysis. Return value instructs the compiler whether to
187+
/// continue the compilation afterwards (defaults to `Compilation::Continue`)
188+
fn after_analysis<'tcx>(
189+
&mut self,
190+
_handler: &EarlyErrorHandler,
191+
_compiler: &interface::Compiler,
192+
queries: &'tcx Queries<'tcx>,
193+
) -> Compilation {
194+
queries.global_ctxt().unwrap().enter(|tcx| {
195+
rustc_internal::run(tcx, || (self.callback)(tcx));
196+
});
197+
// No need to keep going.
198+
Compilation::Stop
199+
}
200+
}

compiler/rustc_smir/src/rustc_smir/mod.rs

+13-16
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
use crate::rustc_internal::{self, opaque};
1111
use crate::stable_mir::mir::{CopyNonOverlapping, UserTypeProjection, VariantIdx};
1212
use crate::stable_mir::ty::{
13-
allocation_filter, new_allocation, Const, FloatTy, GenericDef, GenericParamDef, IntTy,
14-
Movability, RigidTy, TyKind, UintTy,
13+
allocation_filter, new_allocation, Const, FloatTy, GenericParamDef, IntTy, Movability, RigidTy,
14+
TyKind, UintTy,
1515
};
1616
use crate::stable_mir::{self, Context};
1717
use rustc_hir as hir;
@@ -54,7 +54,7 @@ impl<'tcx> Context for Tables<'tcx> {
5454
}
5555

5656
fn trait_decl(&mut self, trait_def: &stable_mir::ty::TraitDef) -> stable_mir::ty::TraitDecl {
57-
let def_id = self.trait_def_id(trait_def);
57+
let def_id = self[trait_def.0];
5858
let trait_def = self.tcx.trait_def(def_id);
5959
trait_def.stable(self)
6060
}
@@ -68,13 +68,13 @@ impl<'tcx> Context for Tables<'tcx> {
6868
}
6969

7070
fn trait_impl(&mut self, impl_def: &stable_mir::ty::ImplDef) -> stable_mir::ty::ImplTrait {
71-
let def_id = self.impl_trait_def_id(impl_def);
71+
let def_id = self[impl_def.0];
7272
let impl_trait = self.tcx.impl_trait_ref(def_id).unwrap();
7373
impl_trait.stable(self)
7474
}
7575

7676
fn mir_body(&mut self, item: &stable_mir::CrateItem) -> stable_mir::mir::Body {
77-
let def_id = self.item_def_id(item);
77+
let def_id = self[item.0];
7878
let mir = self.tcx.optimized_mir(def_id);
7979
stable_mir::mir::Body {
8080
blocks: mir
@@ -102,19 +102,16 @@ impl<'tcx> Context for Tables<'tcx> {
102102
ty.stable(self)
103103
}
104104

105-
fn generics_of(&mut self, generic_def: &GenericDef) -> stable_mir::ty::Generics {
106-
let def_id = self.generic_def_id(generic_def);
107-
let generic_def = self.tcx.generics_of(def_id);
108-
generic_def.stable(self)
105+
fn generics_of(&mut self, def_id: stable_mir::DefId) -> stable_mir::ty::Generics {
106+
let def_id = self[def_id];
107+
let generics = self.tcx.generics_of(def_id);
108+
generics.stable(self)
109109
}
110110

111-
fn predicates_of(
112-
&mut self,
113-
trait_def: &stable_mir::ty::TraitDef,
114-
) -> stable_mir::GenericPredicates {
115-
let trait_def_id = self.trait_def_id(trait_def);
116-
let ty::GenericPredicates { parent, predicates } = self.tcx.predicates_of(trait_def_id);
117-
stable_mir::GenericPredicates {
111+
fn predicates_of(&mut self, def_id: stable_mir::DefId) -> stable_mir::ty::GenericPredicates {
112+
let def_id = self[def_id];
113+
let ty::GenericPredicates { parent, predicates } = self.tcx.predicates_of(def_id);
114+
stable_mir::ty::GenericPredicates {
118115
parent: parent.map(|did| self.trait_def(did)),
119116
predicates: predicates
120117
.iter()

0 commit comments

Comments
 (0)