Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c3c5a5c

Browse files
committedJul 21, 2023
Auto merge of rust-lang#113922 - matthiaskrgr:rollup-90cj2vv, r=matthiaskrgr
Rollup of 4 pull requests Successful merges: - rust-lang#113887 (new solver: add a separate cache for coherence) - rust-lang#113910 (Add FnPtr ty to SMIR) - rust-lang#113913 (error/E0691: include alignment in error message) - rust-lang#113914 (rustc_target: drop duplicate code) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 557359f + 1aaf583 commit c3c5a5c

File tree

9 files changed

+444
-245
lines changed

9 files changed

+444
-245
lines changed
 

‎compiler/rustc_error_codes/src/error_codes/E0691.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ struct ForceAlign32;
1111
1212
#[repr(transparent)]
1313
struct Wrapper(f32, ForceAlign32); // error: zero-sized field in transparent
14-
// struct has alignment larger than 1
14+
// struct has alignment of 32, which
15+
// is larger than 1
1516
```
1617

1718
A transparent struct, enum, or union is supposed to be represented exactly like

‎compiler/rustc_hir_analysis/src/check/check.rs‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,9 +1078,9 @@ pub(super) fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, adt: ty::AdtDef<'tcx>)
10781078
// We are currently checking the type this field came from, so it must be local
10791079
let span = tcx.hir().span_if_local(field.did).unwrap();
10801080
let zst = layout.is_ok_and(|layout| layout.is_zst());
1081-
let align1 = layout.is_ok_and(|layout| layout.align.abi.bytes() == 1);
1081+
let align = layout.ok().map(|layout| layout.align.abi.bytes());
10821082
if !zst {
1083-
return (span, zst, align1, None);
1083+
return (span, zst, align, None);
10841084
}
10851085

10861086
fn check_non_exhaustive<'tcx>(
@@ -1115,30 +1115,39 @@ pub(super) fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, adt: ty::AdtDef<'tcx>)
11151115
}
11161116
}
11171117

1118-
(span, zst, align1, check_non_exhaustive(tcx, ty).break_value())
1118+
(span, zst, align, check_non_exhaustive(tcx, ty).break_value())
11191119
});
11201120

11211121
let non_zst_fields = field_infos
11221122
.clone()
1123-
.filter_map(|(span, zst, _align1, _non_exhaustive)| if !zst { Some(span) } else { None });
1123+
.filter_map(|(span, zst, _align, _non_exhaustive)| if !zst { Some(span) } else { None });
11241124
let non_zst_count = non_zst_fields.clone().count();
11251125
if non_zst_count >= 2 {
11261126
bad_non_zero_sized_fields(tcx, adt, non_zst_count, non_zst_fields, tcx.def_span(adt.did()));
11271127
}
11281128
let incompatible_zst_fields =
11291129
field_infos.clone().filter(|(_, _, _, opt)| opt.is_some()).count();
11301130
let incompat = incompatible_zst_fields + non_zst_count >= 2 && non_zst_count < 2;
1131-
for (span, zst, align1, non_exhaustive) in field_infos {
1132-
if zst && !align1 {
1133-
struct_span_err!(
1131+
for (span, zst, align, non_exhaustive) in field_infos {
1132+
if zst && align != Some(1) {
1133+
let mut err = struct_span_err!(
11341134
tcx.sess,
11351135
span,
11361136
E0691,
11371137
"zero-sized field in transparent {} has alignment larger than 1",
11381138
adt.descr(),
1139-
)
1140-
.span_label(span, "has alignment larger than 1")
1141-
.emit();
1139+
);
1140+
1141+
if let Some(align_bytes) = align {
1142+
err.span_label(
1143+
span,
1144+
format!("has alignment of {align_bytes}, which is larger than 1"),
1145+
);
1146+
} else {
1147+
err.span_label(span, "may have alignment larger than 1");
1148+
}
1149+
1150+
err.emit();
11421151
}
11431152
if incompat && let Some((descr, def_id, args, non_exhaustive)) = non_exhaustive {
11441153
tcx.struct_span_lint_hir(

‎compiler/rustc_middle/src/ty/context.rs‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ pub struct GlobalCtxt<'tcx> {
569569

570570
/// Caches the results of goal evaluation in the new solver.
571571
pub new_solver_evaluation_cache: solve::EvaluationCache<'tcx>,
572+
pub new_solver_coherence_evaluation_cache: solve::EvaluationCache<'tcx>,
572573

573574
/// Data layout specification for the current target.
574575
pub data_layout: TargetDataLayout,
@@ -680,10 +681,12 @@ impl<'tcx> TyCtxt<'tcx> {
680681
value.lift_to_tcx(self)
681682
}
682683

683-
/// Creates a type context and call the closure with a `TyCtxt` reference
684-
/// to the context. The closure enforces that the type context and any interned
685-
/// value (types, args, etc.) can only be used while `ty::tls` has a valid
686-
/// reference to the context, to allow formatting values that need it.
684+
/// Creates a type context. To use the context call `fn enter` which
685+
/// provides a `TyCtxt`.
686+
///
687+
/// By only providing the `TyCtxt` inside of the closure we enforce that the type
688+
/// context and any interned alue (types, args, etc.) can only be used while `ty::tls`
689+
/// has a valid reference to the context, to allow formatting values that need it.
687690
pub fn create_global_ctxt(
688691
s: &'tcx Session,
689692
lint_store: Lrc<dyn Any + sync::DynSend + sync::DynSync>,
@@ -721,6 +724,7 @@ impl<'tcx> TyCtxt<'tcx> {
721724
selection_cache: Default::default(),
722725
evaluation_cache: Default::default(),
723726
new_solver_evaluation_cache: Default::default(),
727+
new_solver_coherence_evaluation_cache: Default::default(),
724728
data_layout,
725729
alloc_map: Lock::new(interpret::AllocMap::new()),
726730
}

‎compiler/rustc_smir/src/rustc_internal/mod.rs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ pub fn generator_def(did: DefId) -> stable_mir::ty::GeneratorDef {
4747
with_tables(|t| t.generator_def(did))
4848
}
4949

50+
pub fn param_def(did: DefId) -> stable_mir::ty::ParamDef {
51+
with_tables(|t| t.param_def(did))
52+
}
53+
54+
pub fn br_named_def(did: DefId) -> stable_mir::ty::BrNamedDef {
55+
with_tables(|t| t.br_named_def(did))
56+
}
57+
5058
impl<'tcx> Tables<'tcx> {
5159
pub fn item_def_id(&self, item: &stable_mir::CrateItem) -> DefId {
5260
self.def_ids[item.0]
@@ -76,6 +84,14 @@ impl<'tcx> Tables<'tcx> {
7684
stable_mir::ty::GeneratorDef(self.create_def_id(did))
7785
}
7886

87+
pub fn param_def(&mut self, did: DefId) -> stable_mir::ty::ParamDef {
88+
stable_mir::ty::ParamDef(self.create_def_id(did))
89+
}
90+
91+
pub fn br_named_def(&mut self, did: DefId) -> stable_mir::ty::BrNamedDef {
92+
stable_mir::ty::BrNamedDef(self.create_def_id(did))
93+
}
94+
7995
fn create_def_id(&mut self, did: DefId) -> stable_mir::DefId {
8096
// FIXME: this becomes inefficient when we have too many ids
8197
for (i, &d) in self.def_ids.iter().enumerate() {

‎compiler/rustc_smir/src/rustc_smir/mod.rs‎

Lines changed: 304 additions & 192 deletions
Large diffs are not rendered by default.

‎compiler/rustc_smir/src/stable_mir/ty.rs‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ impl Ty {
1212

1313
type Const = Opaque;
1414
pub(crate) type Region = Opaque;
15+
type Span = Opaque;
1516

1617
#[derive(Clone, Debug)]
1718
pub enum TyKind {
@@ -33,6 +34,7 @@ pub enum RigidTy {
3334
RawPtr(Ty, Mutability),
3435
Ref(Region, Ty, Mutability),
3536
FnDef(FnDef, GenericArgs),
37+
FnPtr(PolyFnSig),
3638
Closure(ClosureDef, GenericArgs),
3739
Generator(GeneratorDef, GenericArgs, Movability),
3840
Never,
@@ -83,6 +85,12 @@ pub struct ClosureDef(pub(crate) DefId);
8385
#[derive(Clone, PartialEq, Eq, Debug)]
8486
pub struct GeneratorDef(pub(crate) DefId);
8587

88+
#[derive(Clone, PartialEq, Eq, Debug)]
89+
pub struct ParamDef(pub(crate) DefId);
90+
91+
#[derive(Clone, PartialEq, Eq, Debug)]
92+
pub struct BrNamedDef(pub(crate) DefId);
93+
8694
#[derive(Clone, PartialEq, Eq, Debug)]
8795
pub struct AdtDef(pub(crate) DefId);
8896

@@ -95,3 +103,74 @@ pub enum GenericArgKind {
95103
Type(Ty),
96104
Const(Const),
97105
}
106+
107+
pub type PolyFnSig = Binder<FnSig>;
108+
109+
#[derive(Clone, Debug)]
110+
pub struct FnSig {
111+
pub inputs_and_output: Vec<Ty>,
112+
pub c_variadic: bool,
113+
pub unsafety: Unsafety,
114+
pub abi: Abi,
115+
}
116+
117+
#[derive(Clone, PartialEq, Eq, Debug)]
118+
pub enum Unsafety {
119+
Unsafe,
120+
Normal,
121+
}
122+
123+
#[derive(Clone, PartialEq, Eq, Debug)]
124+
pub enum Abi {
125+
Rust,
126+
C { unwind: bool },
127+
Cdecl { unwind: bool },
128+
Stdcall { unwind: bool },
129+
Fastcall { unwind: bool },
130+
Vectorcall { unwind: bool },
131+
Thiscall { unwind: bool },
132+
Aapcs { unwind: bool },
133+
Win64 { unwind: bool },
134+
SysV64 { unwind: bool },
135+
PtxKernel,
136+
Msp430Interrupt,
137+
X86Interrupt,
138+
AmdGpuKernel,
139+
EfiApi,
140+
AvrInterrupt,
141+
AvrNonBlockingInterrupt,
142+
CCmseNonSecureCall,
143+
Wasm,
144+
System { unwind: bool },
145+
RustIntrinsic,
146+
RustCall,
147+
PlatformIntrinsic,
148+
Unadjusted,
149+
RustCold,
150+
}
151+
152+
#[derive(Clone, Debug)]
153+
pub struct Binder<T> {
154+
pub value: T,
155+
pub bound_vars: Vec<BoundVariableKind>,
156+
}
157+
158+
#[derive(Clone, Debug)]
159+
pub enum BoundVariableKind {
160+
Ty(BoundTyKind),
161+
Region(BoundRegionKind),
162+
Const,
163+
}
164+
165+
#[derive(Clone, PartialEq, Eq, Debug)]
166+
pub enum BoundTyKind {
167+
Anon,
168+
Param(ParamDef, String),
169+
}
170+
171+
#[derive(Clone, Debug)]
172+
pub enum BoundRegionKind {
173+
BrAnon(Option<Span>),
174+
BrNamed(BrNamedDef, String),
175+
BrEnv,
176+
}

‎compiler/rustc_target/src/abi/mod.rs‎

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -144,24 +144,3 @@ impl<'a, Ty> TyAndLayout<'a, Ty> {
144144
offset
145145
}
146146
}
147-
148-
impl<'a, Ty> TyAndLayout<'a, Ty> {
149-
/// Returns `true` if the layout corresponds to an unsized type.
150-
pub fn is_unsized(&self) -> bool {
151-
self.abi.is_unsized()
152-
}
153-
154-
#[inline]
155-
pub fn is_sized(&self) -> bool {
156-
self.abi.is_sized()
157-
}
158-
159-
/// Returns `true` if the type is a ZST and not unsized.
160-
pub fn is_zst(&self) -> bool {
161-
match self.abi {
162-
Abi::Scalar(_) | Abi::ScalarPair(..) | Abi::Vector { .. } => false,
163-
Abi::Uninhabited => self.size.bytes() == 0,
164-
Abi::Aggregate { sized } => sized && self.size.bytes() == 0,
165-
}
166-
}
167-
}

‎compiler/rustc_trait_selection/src/solve/search_graph/mod.rs‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ use cache::ProvisionalCache;
99
use overflow::OverflowData;
1010
use rustc_index::IndexVec;
1111
use rustc_middle::dep_graph::DepKind;
12-
use rustc_middle::traits::solve::{CanonicalInput, Certainty, MaybeCause, QueryResult};
12+
use rustc_middle::traits::solve::{
13+
CanonicalInput, Certainty, EvaluationCache, MaybeCause, QueryResult,
14+
};
1315
use rustc_middle::ty::TyCtxt;
1416
use std::{collections::hash_map::Entry, mem};
1517

@@ -58,10 +60,10 @@ impl<'tcx> SearchGraph<'tcx> {
5860
///
5961
/// We could add another global cache for coherence instead,
6062
/// but that's effort so let's only do it if necessary.
61-
pub(super) fn should_use_global_cache(&self) -> bool {
63+
pub(super) fn global_cache(&self, tcx: TyCtxt<'tcx>) -> &'tcx EvaluationCache<'tcx> {
6264
match self.mode {
63-
SolverMode::Normal => true,
64-
SolverMode::Coherence => false,
65+
SolverMode::Normal => &tcx.new_solver_evaluation_cache,
66+
SolverMode::Coherence => &tcx.new_solver_coherence_evaluation_cache,
6567
}
6668
}
6769

@@ -213,8 +215,8 @@ impl<'tcx> SearchGraph<'tcx> {
213215
inspect: &mut ProofTreeBuilder<'tcx>,
214216
mut loop_body: impl FnMut(&mut Self, &mut ProofTreeBuilder<'tcx>) -> QueryResult<'tcx>,
215217
) -> QueryResult<'tcx> {
216-
if self.should_use_global_cache() && inspect.use_global_cache() {
217-
if let Some(result) = tcx.new_solver_evaluation_cache.get(&canonical_input, tcx) {
218+
if inspect.use_global_cache() {
219+
if let Some(result) = self.global_cache(tcx).get(&canonical_input, tcx) {
218220
debug!(?canonical_input, ?result, "cache hit");
219221
inspect.cache_hit(CacheHit::Global);
220222
return result;
@@ -278,13 +280,10 @@ impl<'tcx> SearchGraph<'tcx> {
278280
// dependencies, our non-root goal may no longer appear as child of the root goal.
279281
//
280282
// See https://github.com/rust-lang/rust/pull/108071 for some additional context.
281-
let can_cache = !self.overflow_data.did_overflow() || self.stack.is_empty();
282-
if self.should_use_global_cache() && can_cache {
283-
tcx.new_solver_evaluation_cache.insert(
284-
current_goal.input,
285-
dep_node,
286-
current_goal.response,
287-
);
283+
let can_cache = inspect.use_global_cache()
284+
&& (!self.overflow_data.did_overflow() || self.stack.is_empty());
285+
if can_cache {
286+
self.global_cache(tcx).insert(current_goal.input, dep_node, current_goal.response)
288287
}
289288
}
290289

‎tests/ui/repr/repr-transparent.stderr‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ error[E0691]: zero-sized field in transparent struct has alignment larger than 1
2020
--> $DIR/repr-transparent.rs:36:32
2121
|
2222
LL | struct NontrivialAlignZst(u32, [u16; 0]);
23-
| ^^^^^^^^ has alignment larger than 1
23+
| ^^^^^^^^ has alignment of 2, which is larger than 1
2424

2525
error[E0691]: zero-sized field in transparent struct has alignment larger than 1
2626
--> $DIR/repr-transparent.rs:42:24
2727
|
2828
LL | struct GenericAlign<T>(ZstAlign32<T>, u32);
29-
| ^^^^^^^^^^^^^ has alignment larger than 1
29+
| ^^^^^^^^^^^^^ has alignment of 32, which is larger than 1
3030

3131
error[E0084]: unsupported representation for zero-variant enum
3232
--> $DIR/repr-transparent.rs:44:1
@@ -66,13 +66,13 @@ error[E0691]: zero-sized field in transparent enum has alignment larger than 1
6666
--> $DIR/repr-transparent.rs:71:14
6767
|
6868
LL | Foo(u32, [u16; 0]),
69-
| ^^^^^^^^ has alignment larger than 1
69+
| ^^^^^^^^ has alignment of 2, which is larger than 1
7070

7171
error[E0691]: zero-sized field in transparent enum has alignment larger than 1
7272
--> $DIR/repr-transparent.rs:76:11
7373
|
7474
LL | Foo { bar: ZstAlign32<T>, baz: u32 }
75-
| ^^^^^^^^^^^^^^^^^^ has alignment larger than 1
75+
| ^^^^^^^^^^^^^^^^^^ has alignment of 32, which is larger than 1
7676

7777
error[E0690]: transparent union needs at most one non-zero-sized field, but has 2
7878
--> $DIR/repr-transparent.rs:85:1

0 commit comments

Comments
 (0)
Please sign in to comment.