Skip to content

Commit f973a15

Browse files
committed
Auto merge of rust-lang#124547 - matthiaskrgr:rollup-9tv8upg, r=matthiaskrgr
Rollup of 4 pull requests Successful merges: - rust-lang#124519 (adapt a codegen test for llvm 19) - rust-lang#124524 (Add StaticForeignItem and use it on ForeignItemKind) - rust-lang#124540 (Give proof tree visitors the ability to instantiate nested goals directly) - rust-lang#124543 (codegen tests: Tolerate `range()` qualifications in enum tests) r? `@ghost` `@rustbot` modify labels: rollup
2 parents f9dca46 + d81e444 commit f973a15

File tree

15 files changed

+149
-99
lines changed

15 files changed

+149
-99
lines changed

compiler/rustc_ast/src/ast.rs

+36-7
Original file line numberDiff line numberDiff line change
@@ -3126,6 +3126,35 @@ pub struct StaticItem {
31263126
pub expr: Option<P<Expr>>,
31273127
}
31283128

3129+
/// A static item in `extern` block.
3130+
// This struct is identical to StaticItem for now but it's going to have a safety attribute.
3131+
#[derive(Clone, Encodable, Decodable, Debug)]
3132+
pub struct StaticForeignItem {
3133+
pub ty: P<Ty>,
3134+
pub mutability: Mutability,
3135+
pub expr: Option<P<Expr>>,
3136+
}
3137+
3138+
impl From<StaticItem> for StaticForeignItem {
3139+
fn from(static_item: StaticItem) -> StaticForeignItem {
3140+
StaticForeignItem {
3141+
ty: static_item.ty,
3142+
mutability: static_item.mutability,
3143+
expr: static_item.expr,
3144+
}
3145+
}
3146+
}
3147+
3148+
impl From<StaticForeignItem> for StaticItem {
3149+
fn from(static_item: StaticForeignItem) -> StaticItem {
3150+
StaticItem {
3151+
ty: static_item.ty,
3152+
mutability: static_item.mutability,
3153+
expr: static_item.expr,
3154+
}
3155+
}
3156+
}
3157+
31293158
#[derive(Clone, Encodable, Decodable, Debug)]
31303159
pub struct ConstItem {
31313160
pub defaultness: Defaultness,
@@ -3329,7 +3358,7 @@ impl TryFrom<ItemKind> for AssocItemKind {
33293358
#[derive(Clone, Encodable, Decodable, Debug)]
33303359
pub enum ForeignItemKind {
33313360
/// A foreign static item (`static FOO: u8`).
3332-
Static(P<Ty>, Mutability, Option<P<Expr>>),
3361+
Static(Box<StaticForeignItem>),
33333362
/// An foreign function.
33343363
Fn(Box<Fn>),
33353364
/// An foreign type.
@@ -3341,8 +3370,8 @@ pub enum ForeignItemKind {
33413370
impl From<ForeignItemKind> for ItemKind {
33423371
fn from(foreign_item_kind: ForeignItemKind) -> ItemKind {
33433372
match foreign_item_kind {
3344-
ForeignItemKind::Static(a, b, c) => {
3345-
ItemKind::Static(StaticItem { ty: a, mutability: b, expr: c }.into())
3373+
ForeignItemKind::Static(box static_foreign_item) => {
3374+
ItemKind::Static(Box::new(static_foreign_item.into()))
33463375
}
33473376
ForeignItemKind::Fn(fn_kind) => ItemKind::Fn(fn_kind),
33483377
ForeignItemKind::TyAlias(ty_alias_kind) => ItemKind::TyAlias(ty_alias_kind),
@@ -3356,8 +3385,8 @@ impl TryFrom<ItemKind> for ForeignItemKind {
33563385

33573386
fn try_from(item_kind: ItemKind) -> Result<ForeignItemKind, ItemKind> {
33583387
Ok(match item_kind {
3359-
ItemKind::Static(box StaticItem { ty: a, mutability: b, expr: c }) => {
3360-
ForeignItemKind::Static(a, b, c)
3388+
ItemKind::Static(box static_item) => {
3389+
ForeignItemKind::Static(Box::new(static_item.into()))
33613390
}
33623391
ItemKind::Fn(fn_kind) => ForeignItemKind::Fn(fn_kind),
33633392
ItemKind::TyAlias(ty_alias_kind) => ForeignItemKind::TyAlias(ty_alias_kind),
@@ -3382,8 +3411,8 @@ mod size_asserts {
33823411
static_assert_size!(Expr, 72);
33833412
static_assert_size!(ExprKind, 40);
33843413
static_assert_size!(Fn, 160);
3385-
static_assert_size!(ForeignItem, 96);
3386-
static_assert_size!(ForeignItemKind, 24);
3414+
static_assert_size!(ForeignItem, 88);
3415+
static_assert_size!(ForeignItemKind, 16);
33873416
static_assert_size!(GenericArg, 24);
33883417
static_assert_size!(GenericBound, 88);
33893418
static_assert_size!(Generics, 40);

compiler/rustc_ast/src/mut_visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1261,7 +1261,7 @@ pub fn noop_flat_map_item<K: NoopVisitItemKind>(
12611261
impl NoopVisitItemKind for ForeignItemKind {
12621262
fn noop_visit(&mut self, visitor: &mut impl MutVisitor) {
12631263
match self {
1264-
ForeignItemKind::Static(ty, _, expr) => {
1264+
ForeignItemKind::Static(box StaticForeignItem { ty, mutability: _, expr }) => {
12651265
visitor.visit_ty(ty);
12661266
visit_opt(expr, |expr| visitor.visit_expr(expr));
12671267
}

compiler/rustc_ast/src/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ impl WalkItemKind for ForeignItemKind {
642642
) -> V::Result {
643643
let &Item { id, span, ident, ref vis, .. } = item;
644644
match self {
645-
ForeignItemKind::Static(ty, _, expr) => {
645+
ForeignItemKind::Static(box StaticForeignItem { ty, mutability: _, expr }) => {
646646
try_visit!(visitor.visit_ty(ty));
647647
visit_opt!(visitor, visit_expr, expr);
648648
}

compiler/rustc_ast_lowering/src/item.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -662,10 +662,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
662662

663663
hir::ForeignItemKind::Fn(fn_dec, fn_args, generics)
664664
}
665-
ForeignItemKind::Static(t, m, _) => {
666-
let ty =
667-
self.lower_ty(t, ImplTraitContext::Disallowed(ImplTraitPosition::StaticTy));
668-
hir::ForeignItemKind::Static(ty, *m)
665+
ForeignItemKind::Static(box StaticForeignItem { ty, mutability, expr: _ }) => {
666+
let ty = self
667+
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::StaticTy));
668+
hir::ForeignItemKind::Static(ty, *mutability)
669669
}
670670
ForeignItemKind::TyAlias(..) => hir::ForeignItemKind::Type,
671671
ForeignItemKind::MacCall(_) => panic!("macro shouldn't exist here"),

compiler/rustc_ast_passes/src/ast_validation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1185,8 +1185,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11851185
self.check_foreign_ty_genericless(generics, where_clauses);
11861186
self.check_foreign_item_ascii_only(fi.ident);
11871187
}
1188-
ForeignItemKind::Static(_, _, body) => {
1189-
self.check_foreign_kind_bodyless(fi.ident, "static", body.as_ref().map(|b| b.span));
1188+
ForeignItemKind::Static(box StaticForeignItem { ty: _, mutability: _, expr }) => {
1189+
self.check_foreign_kind_bodyless(fi.ident, "static", expr.as_ref().map(|b| b.span));
11901190
self.check_foreign_item_ascii_only(fi.ident);
11911191
}
11921192
ForeignItemKind::MacCall(..) => {}

compiler/rustc_ast_pretty/src/pprust/state/item.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,17 @@ impl<'a> State<'a> {
3030
ast::ForeignItemKind::Fn(box ast::Fn { defaultness, sig, generics, body }) => {
3131
self.print_fn_full(sig, ident, generics, vis, *defaultness, body.as_deref(), attrs);
3232
}
33-
ast::ForeignItemKind::Static(ty, mutbl, body) => self.print_item_const(
34-
ident,
35-
Some(*mutbl),
36-
&ast::Generics::default(),
37-
ty,
38-
body.as_deref(),
39-
vis,
40-
ast::Defaultness::Final,
41-
),
33+
ast::ForeignItemKind::Static(box ast::StaticForeignItem { ty, mutability, expr }) => {
34+
self.print_item_const(
35+
ident,
36+
Some(*mutability),
37+
&ast::Generics::default(),
38+
ty,
39+
expr.as_deref(),
40+
vis,
41+
ast::Defaultness::Final,
42+
)
43+
}
4244
ast::ForeignItemKind::TyAlias(box ast::TyAlias {
4345
defaultness,
4446
generics,

compiler/rustc_middle/src/traits/solve/inspect.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ pub struct GoalEvaluation<'tcx> {
6060
pub evaluation: CanonicalGoalEvaluation<'tcx>,
6161
}
6262

63-
#[derive(Eq, PartialEq)]
63+
#[derive(Eq, PartialEq, Debug)]
6464
pub struct CanonicalGoalEvaluation<'tcx> {
6565
pub goal: CanonicalInput<'tcx>,
6666
pub kind: CanonicalGoalEvaluationKind<'tcx>,
6767
pub result: QueryResult<'tcx>,
6868
}
6969

70-
#[derive(Eq, PartialEq)]
70+
#[derive(Eq, PartialEq, Debug)]
7171
pub enum CanonicalGoalEvaluationKind<'tcx> {
7272
Overflow,
7373
CycleInStack,
@@ -86,7 +86,7 @@ pub struct AddedGoalsEvaluation<'tcx> {
8686
pub result: Result<Certainty, NoSolution>,
8787
}
8888

89-
#[derive(Eq, PartialEq)]
89+
#[derive(Eq, PartialEq, Debug)]
9090
pub struct GoalEvaluationStep<'tcx> {
9191
pub instantiated_goal: QueryInput<'tcx, ty::Predicate<'tcx>>,
9292

compiler/rustc_parse/src/parser/item.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1191,7 +1191,11 @@ impl<'a> Parser<'a> {
11911191
ident_span: ident.span,
11921192
const_span,
11931193
});
1194-
ForeignItemKind::Static(ty, Mutability::Not, expr)
1194+
ForeignItemKind::Static(Box::new(StaticForeignItem {
1195+
ty,
1196+
mutability: Mutability::Not,
1197+
expr,
1198+
}))
11951199
}
11961200
_ => return self.error_bad_item_kind(span, &kind, "`extern` blocks"),
11971201
},

compiler/rustc_resolve/src/def_collector.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ impl<'a, 'b, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'b, 'tcx> {
209209

210210
fn visit_foreign_item(&mut self, fi: &'a ForeignItem) {
211211
let def_kind = match fi.kind {
212-
ForeignItemKind::Static(_, mutability, _) => {
212+
ForeignItemKind::Static(box StaticForeignItem { ty: _, mutability, expr: _ }) => {
213213
DefKind::Static { mutability, nested: false }
214214
}
215215
ForeignItemKind::Fn(_) => DefKind::Fn,

compiler/rustc_trait_selection/src/solve/inspect/analyse.rs

+54-50
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ pub struct InspectConfig {
3434
pub struct InspectGoal<'a, 'tcx> {
3535
infcx: &'a InferCtxt<'tcx>,
3636
depth: usize,
37-
orig_values: &'a [ty::GenericArg<'tcx>],
37+
orig_values: Vec<ty::GenericArg<'tcx>>,
3838
goal: Goal<'tcx, ty::Predicate<'tcx>>,
39-
evaluation: &'a inspect::GoalEvaluation<'tcx>,
39+
evaluation: inspect::CanonicalGoalEvaluation<'tcx>,
4040
}
4141

4242
pub struct InspectCandidate<'a, 'tcx> {
@@ -57,6 +57,10 @@ impl<'a, 'tcx> InspectCandidate<'a, 'tcx> {
5757
self.result.map(|c| c.value.certainty)
5858
}
5959

60+
pub fn goal(&self) -> &'a InspectGoal<'a, 'tcx> {
61+
self.goal
62+
}
63+
6064
/// Certainty passed into `evaluate_added_goals_and_make_canonical_response`.
6165
///
6266
/// If this certainty is `Yes`, then we must be confident that the candidate
@@ -74,46 +78,55 @@ impl<'a, 'tcx> InspectCandidate<'a, 'tcx> {
7478
/// the state of the `infcx`.
7579
pub fn visit_nested_no_probe<V: ProofTreeVisitor<'tcx>>(&self, visitor: &mut V) -> V::Result {
7680
if self.goal.depth < visitor.config().max_depth {
77-
let infcx = self.goal.infcx;
78-
let param_env = self.goal.goal.param_env;
79-
let mut orig_values = self.goal.orig_values.to_vec();
80-
let mut instantiated_goals = vec![];
81-
for goal in &self.nested_goals {
82-
let goal = canonical::instantiate_canonical_state(
81+
for goal in self.instantiate_nested_goals(visitor.span()) {
82+
try_visit!(visitor.visit_goal(&goal));
83+
}
84+
}
85+
86+
V::Result::output()
87+
}
88+
89+
/// Instantiate the nested goals for the candidate without rolling back their
90+
/// inference constraints. This function modifies the state of the `infcx`.
91+
pub fn instantiate_nested_goals(&self, span: Span) -> Vec<InspectGoal<'a, 'tcx>> {
92+
let infcx = self.goal.infcx;
93+
let param_env = self.goal.goal.param_env;
94+
let mut orig_values = self.goal.orig_values.to_vec();
95+
let instantiated_goals: Vec<_> = self
96+
.nested_goals
97+
.iter()
98+
.map(|goal| {
99+
canonical::instantiate_canonical_state(
83100
infcx,
84-
visitor.span(),
101+
span,
85102
param_env,
86103
&mut orig_values,
87104
*goal,
88-
);
89-
instantiated_goals.push(goal);
90-
}
105+
)
106+
})
107+
.collect();
91108

92-
let () = canonical::instantiate_canonical_state(
93-
infcx,
94-
visitor.span(),
95-
param_env,
96-
&mut orig_values,
97-
self.final_state,
98-
);
109+
let () = canonical::instantiate_canonical_state(
110+
infcx,
111+
span,
112+
param_env,
113+
&mut orig_values,
114+
self.final_state,
115+
);
99116

100-
for &goal in &instantiated_goals {
117+
instantiated_goals
118+
.into_iter()
119+
.map(|goal| {
101120
let proof_tree = match goal.predicate.kind().no_bound_vars() {
102121
Some(ty::PredicateKind::NormalizesTo(ty::NormalizesTo { alias, term })) => {
103122
let unconstrained_term = match term.unpack() {
104123
ty::TermKind::Ty(_) => infcx
105-
.next_ty_var(TypeVariableOrigin {
106-
param_def_id: None,
107-
span: visitor.span(),
108-
})
124+
.next_ty_var(TypeVariableOrigin { param_def_id: None, span })
109125
.into(),
110126
ty::TermKind::Const(ct) => infcx
111127
.next_const_var(
112128
ct.ty(),
113-
ConstVariableOrigin {
114-
param_def_id: None,
115-
span: visitor.span(),
116-
},
129+
ConstVariableOrigin { param_def_id: None, span },
117130
)
118131
.into(),
119132
};
@@ -129,22 +142,16 @@ impl<'a, 'tcx> InspectCandidate<'a, 'tcx> {
129142
})
130143
.1;
131144
let InferOk { value: (), obligations: _ } = infcx
132-
.at(&ObligationCause::dummy(), param_env)
145+
.at(&ObligationCause::dummy_with_span(span), param_env)
133146
.eq(DefineOpaqueTypes::Yes, term, unconstrained_term)
134147
.unwrap();
135148
proof_tree
136149
}
137150
_ => infcx.evaluate_root_goal(goal, GenerateProofTree::Yes).1,
138151
};
139-
try_visit!(visitor.visit_goal(&InspectGoal::new(
140-
infcx,
141-
self.goal.depth + 1,
142-
&proof_tree.unwrap(),
143-
)));
144-
}
145-
}
146-
147-
V::Result::output()
152+
InspectGoal::new(infcx, self.goal.depth + 1, proof_tree.unwrap())
153+
})
154+
.collect()
148155
}
149156

150157
/// Visit all nested goals of this candidate, rolling back
@@ -164,7 +171,7 @@ impl<'a, 'tcx> InspectGoal<'a, 'tcx> {
164171
}
165172

166173
pub fn result(&self) -> Result<Certainty, NoSolution> {
167-
self.evaluation.evaluation.result.map(|c| c.value.certainty)
174+
self.evaluation.result.map(|c| c.value.certainty)
168175
}
169176

170177
fn candidates_recur(
@@ -221,7 +228,7 @@ impl<'a, 'tcx> InspectGoal<'a, 'tcx> {
221228

222229
pub fn candidates(&'a self) -> Vec<InspectCandidate<'a, 'tcx>> {
223230
let mut candidates = vec![];
224-
let last_eval_step = match self.evaluation.evaluation.kind {
231+
let last_eval_step = match self.evaluation.kind {
225232
inspect::CanonicalGoalEvaluationKind::Overflow
226233
| inspect::CanonicalGoalEvaluationKind::CycleInStack
227234
| inspect::CanonicalGoalEvaluationKind::ProvisionalCacheHit => {
@@ -254,18 +261,15 @@ impl<'a, 'tcx> InspectGoal<'a, 'tcx> {
254261
candidates.pop().filter(|_| candidates.is_empty())
255262
}
256263

257-
fn new(
258-
infcx: &'a InferCtxt<'tcx>,
259-
depth: usize,
260-
root: &'a inspect::GoalEvaluation<'tcx>,
261-
) -> Self {
262-
match root.kind {
263-
inspect::GoalEvaluationKind::Root { ref orig_values } => InspectGoal {
264+
fn new(infcx: &'a InferCtxt<'tcx>, depth: usize, root: inspect::GoalEvaluation<'tcx>) -> Self {
265+
let inspect::GoalEvaluation { uncanonicalized_goal, kind, evaluation } = root;
266+
match kind {
267+
inspect::GoalEvaluationKind::Root { orig_values } => InspectGoal {
264268
infcx,
265269
depth,
266270
orig_values,
267-
goal: root.uncanonicalized_goal.fold_with(&mut EagerResolver::new(infcx)),
268-
evaluation: root,
271+
goal: uncanonicalized_goal.fold_with(&mut EagerResolver::new(infcx)),
272+
evaluation,
269273
},
270274
inspect::GoalEvaluationKind::Nested { .. } => unreachable!(),
271275
}
@@ -294,6 +298,6 @@ impl<'tcx> InferCtxt<'tcx> {
294298
) -> V::Result {
295299
let (_, proof_tree) = self.evaluate_root_goal(goal, GenerateProofTree::Yes);
296300
let proof_tree = proof_tree.unwrap();
297-
visitor.visit_goal(&InspectGoal::new(self, 0, &proof_tree))
301+
visitor.visit_goal(&InspectGoal::new(self, 0, proof_tree))
298302
}
299303
}

src/tools/clippy/clippy_utils/src/ast_utils.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,18 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
446446
pub fn eq_foreign_item_kind(l: &ForeignItemKind, r: &ForeignItemKind) -> bool {
447447
use ForeignItemKind::*;
448448
match (l, r) {
449-
(Static(lt, lm, le), Static(rt, rm, re)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
449+
(
450+
Static(box StaticForeignItem {
451+
ty: lt,
452+
mutability: lm,
453+
expr: le,
454+
}),
455+
Static(box StaticForeignItem {
456+
ty: rt,
457+
mutability: rm,
458+
expr: re,
459+
}),
460+
) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
450461
(
451462
Fn(box ast::Fn {
452463
defaultness: ld,

0 commit comments

Comments
 (0)