Skip to content

Commit 7a0070e

Browse files
committed
Auto merge of rust-lang#112228 - compiler-errors:rollup-97i0pli, r=compiler-errors
Rollup of 6 pull requests Successful merges: - rust-lang#109609 (Separate AnonConst from ConstBlock in HIR.) - rust-lang#112166 (bootstrap: Rename profile = user to profile = dist) - rust-lang#112168 (Lower `unchecked_div`/`_rem` to MIR's `BinOp::Div`/`Rem`) - rust-lang#112183 (Normalize anon consts in new solver) - rust-lang#112211 (pass `--lib` to `x doc`) - rust-lang#112223 (Don't ICE in new solver when auto traits have associated types) r? `@ghost` `@rustbot` modify labels: rollup
2 parents dd5d7c7 + 18763cb commit 7a0070e

File tree

63 files changed

+408
-229
lines changed

Some content is hidden

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

63 files changed

+408
-229
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
7171

7272
let kind = match &e.kind {
7373
ExprKind::Array(exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)),
74-
ExprKind::ConstBlock(anon_const) => {
75-
let anon_const = self.lower_anon_const(anon_const);
76-
hir::ExprKind::ConstBlock(anon_const)
74+
ExprKind::ConstBlock(c) => {
75+
let c = self.with_new_scopes(|this| hir::ConstBlock {
76+
def_id: this.local_def_id(c.id),
77+
hir_id: this.lower_node_id(c.id),
78+
body: this.lower_const_body(c.value.span, Some(&c.value)),
79+
});
80+
hir::ExprKind::ConstBlock(c)
7781
}
7882
ExprKind::Repeat(expr, count) => {
7983
let expr = self.lower_expr(expr);

compiler/rustc_ast_lowering/src/index.rs

+8
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,14 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
223223
});
224224
}
225225

226+
fn visit_inline_const(&mut self, constant: &'hir ConstBlock) {
227+
self.insert(DUMMY_SP, constant.hir_id, Node::ConstBlock(constant));
228+
229+
self.with_parent(constant.hir_id, |this| {
230+
intravisit::walk_inline_const(this, constant);
231+
});
232+
}
233+
226234
fn visit_expr(&mut self, expr: &'hir Expr<'hir>) {
227235
self.insert(expr.span, expr.hir_id, Node::Expr(expr));
228236

compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -475,9 +475,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
475475
sym::unchecked_add
476476
| sym::unchecked_sub
477477
| sym::unchecked_mul
478-
| sym::unchecked_div
479478
| sym::exact_div
480-
| sym::unchecked_rem
481479
| sym::unchecked_shl
482480
| sym::unchecked_shr => {
483481
intrinsic_args!(fx, args => (x, y); intrinsic);
@@ -487,8 +485,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
487485
sym::unchecked_add => BinOp::Add,
488486
sym::unchecked_sub => BinOp::Sub,
489487
sym::unchecked_mul => BinOp::Mul,
490-
sym::unchecked_div | sym::exact_div => BinOp::Div,
491-
sym::unchecked_rem => BinOp::Rem,
488+
sym::exact_div => BinOp::Div,
492489
sym::unchecked_shl => BinOp::Shl,
493490
sym::unchecked_shr => BinOp::Shr,
494491
_ => unreachable!(),

compiler/rustc_codegen_ssa/src/mir/intrinsic.rs

-16
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
211211
args[1].val.unaligned_volatile_store(bx, dst);
212212
return;
213213
}
214-
| sym::unchecked_div
215-
| sym::unchecked_rem
216214
| sym::unchecked_shl
217215
| sym::unchecked_shr
218216
| sym::unchecked_add
@@ -229,20 +227,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
229227
bx.exactudiv(args[0].immediate(), args[1].immediate())
230228
}
231229
}
232-
sym::unchecked_div => {
233-
if signed {
234-
bx.sdiv(args[0].immediate(), args[1].immediate())
235-
} else {
236-
bx.udiv(args[0].immediate(), args[1].immediate())
237-
}
238-
}
239-
sym::unchecked_rem => {
240-
if signed {
241-
bx.srem(args[0].immediate(), args[1].immediate())
242-
} else {
243-
bx.urem(args[0].immediate(), args[1].immediate())
244-
}
245-
}
246230
sym::unchecked_shl => bx.shl(args[0].immediate(), args[1].immediate()),
247231
sym::unchecked_shr => {
248232
if signed {

compiler/rustc_const_eval/src/interpret/intrinsics.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
238238
| sym::unchecked_shr
239239
| sym::unchecked_add
240240
| sym::unchecked_sub
241-
| sym::unchecked_mul
242-
| sym::unchecked_div
243-
| sym::unchecked_rem => {
241+
| sym::unchecked_mul => {
244242
let l = self.read_immediate(&args[0])?;
245243
let r = self.read_immediate(&args[1])?;
246244
let bin_op = match intrinsic_name {
@@ -249,8 +247,6 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
249247
sym::unchecked_add => BinOp::Add,
250248
sym::unchecked_sub => BinOp::Sub,
251249
sym::unchecked_mul => BinOp::Mul,
252-
sym::unchecked_div => BinOp::Div,
253-
sym::unchecked_rem => BinOp::Rem,
254250
_ => bug!(),
255251
};
256252
let (val, overflowed, _ty) = self.overflowing_binary_op(bin_op, &l, &r)?;

compiler/rustc_hir/src/hir.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -1675,6 +1675,14 @@ pub struct AnonConst {
16751675
pub body: BodyId,
16761676
}
16771677

1678+
/// An inline constant expression `const { something }`.
1679+
#[derive(Copy, Clone, Debug, HashStable_Generic)]
1680+
pub struct ConstBlock {
1681+
pub hir_id: HirId,
1682+
pub def_id: LocalDefId,
1683+
pub body: BodyId,
1684+
}
1685+
16781686
/// An expression.
16791687
#[derive(Debug, Clone, Copy, HashStable_Generic)]
16801688
pub struct Expr<'hir> {
@@ -1922,7 +1930,7 @@ pub fn is_range_literal(expr: &Expr<'_>) -> bool {
19221930
#[derive(Debug, Clone, Copy, HashStable_Generic)]
19231931
pub enum ExprKind<'hir> {
19241932
/// Allow anonymous constants from an inline `const` block
1925-
ConstBlock(AnonConst),
1933+
ConstBlock(ConstBlock),
19261934
/// An array (e.g., `[a, b, c, d]`).
19271935
Array(&'hir [Expr<'hir>]),
19281936
/// A function call.
@@ -3641,6 +3649,7 @@ pub enum Node<'hir> {
36413649
Variant(&'hir Variant<'hir>),
36423650
Field(&'hir FieldDef<'hir>),
36433651
AnonConst(&'hir AnonConst),
3652+
ConstBlock(&'hir ConstBlock),
36443653
Expr(&'hir Expr<'hir>),
36453654
ExprField(&'hir ExprField<'hir>),
36463655
Stmt(&'hir Stmt<'hir>),
@@ -3695,6 +3704,7 @@ impl<'hir> Node<'hir> {
36953704
Node::TypeBinding(b) => Some(b.ident),
36963705
Node::Param(..)
36973706
| Node::AnonConst(..)
3707+
| Node::ConstBlock(..)
36983708
| Node::Expr(..)
36993709
| Node::Stmt(..)
37003710
| Node::Block(..)
@@ -3758,7 +3768,7 @@ impl<'hir> Node<'hir> {
37583768
})
37593769
| Node::Expr(Expr {
37603770
kind:
3761-
ExprKind::ConstBlock(AnonConst { body, .. })
3771+
ExprKind::ConstBlock(ConstBlock { body, .. })
37623772
| ExprKind::Closure(Closure { body, .. })
37633773
| ExprKind::Repeat(_, ArrayLen::Body(AnonConst { body, .. })),
37643774
..
@@ -3878,6 +3888,13 @@ impl<'hir> Node<'hir> {
38783888
this
38793889
}
38803890

3891+
/// Expect a [`Node::ConstBlock`] or panic.
3892+
#[track_caller]
3893+
pub fn expect_inline_const(self) -> &'hir ConstBlock {
3894+
let Node::ConstBlock(this) = self else { self.expect_failed("an inline constant") };
3895+
this
3896+
}
3897+
38813898
/// Expect a [`Node::Expr`] or panic.
38823899
#[track_caller]
38833900
pub fn expect_expr(self) -> &'hir Expr<'hir> {

compiler/rustc_hir/src/intravisit.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,9 @@ pub trait Visitor<'v>: Sized {
335335
fn visit_anon_const(&mut self, c: &'v AnonConst) {
336336
walk_anon_const(self, c)
337337
}
338+
fn visit_inline_const(&mut self, c: &'v ConstBlock) {
339+
walk_inline_const(self, c)
340+
}
338341
fn visit_expr(&mut self, ex: &'v Expr<'v>) {
339342
walk_expr(self, ex)
340343
}
@@ -679,13 +682,18 @@ pub fn walk_anon_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v AnonCo
679682
visitor.visit_nested_body(constant.body);
680683
}
681684

685+
pub fn walk_inline_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v ConstBlock) {
686+
visitor.visit_id(constant.hir_id);
687+
visitor.visit_nested_body(constant.body);
688+
}
689+
682690
pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>) {
683691
visitor.visit_id(expression.hir_id);
684692
match expression.kind {
685693
ExprKind::Array(subexpressions) => {
686694
walk_list!(visitor, visit_expr, subexpressions);
687695
}
688-
ExprKind::ConstBlock(ref anon_const) => visitor.visit_anon_const(anon_const),
696+
ExprKind::ConstBlock(ref const_block) => visitor.visit_inline_const(const_block),
689697
ExprKind::Repeat(ref element, ref count) => {
690698
visitor.visit_expr(element);
691699
visitor.visit_array_length(count)

compiler/rustc_hir_analysis/src/check/region.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ fn resolve_expr<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, expr: &'tcx h
392392
// Manually recurse over closures and inline consts, because they are the only
393393
// case of nested bodies that share the parent environment.
394394
hir::ExprKind::Closure(&hir::Closure { body, .. })
395-
| hir::ExprKind::ConstBlock(hir::AnonConst { body, .. }) => {
395+
| hir::ExprKind::ConstBlock(hir::ConstBlock { body, .. }) => {
396396
let body = visitor.tcx.hir().body(body);
397397
visitor.visit_body(body);
398398
}

compiler/rustc_hir_analysis/src/collect/generics_of.rs

+11-16
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
5050
// We do not allow generic parameters in anon consts if we are inside
5151
// of a const parameter type, e.g. `struct Foo<const N: usize, const M: [u8; N]>` is not allowed.
5252
None
53-
} else if tcx.lazy_normalization() {
53+
} else if tcx.features().generic_const_exprs {
5454
let parent_node = tcx.hir().get_parent(hir_id);
5555
if let Node::Variant(Variant { disr_expr: Some(constant), .. }) = parent_node
5656
&& constant.hir_id == hir_id
@@ -123,9 +123,6 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
123123
{
124124
Some(parent_def_id.to_def_id())
125125
}
126-
Node::Expr(&Expr { kind: ExprKind::ConstBlock(_), .. }) => {
127-
Some(tcx.typeck_root_def_id(def_id.to_def_id()))
128-
}
129126
// Exclude `GlobalAsm` here which cannot have generics.
130127
Node::Expr(&Expr { kind: ExprKind::InlineAsm(asm), .. })
131128
if asm.operands.iter().any(|(op, _op_sp)| match op {
@@ -142,7 +139,8 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
142139
}
143140
}
144141
}
145-
Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => {
142+
Node::ConstBlock(_)
143+
| Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => {
146144
Some(tcx.typeck_root_def_id(def_id.to_def_id()))
147145
}
148146
Node::Item(item) => match item.kind {
@@ -339,17 +337,14 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
339337
}
340338

341339
// provide junk type parameter defs for const blocks.
342-
if let Node::AnonConst(_) = node {
343-
let parent_node = tcx.hir().get_parent(hir_id);
344-
if let Node::Expr(&Expr { kind: ExprKind::ConstBlock(_), .. }) = parent_node {
345-
params.push(ty::GenericParamDef {
346-
index: next_index(),
347-
name: Symbol::intern("<const_ty>"),
348-
def_id: def_id.to_def_id(),
349-
pure_wrt_drop: false,
350-
kind: ty::GenericParamDefKind::Type { has_default: false, synthetic: false },
351-
});
352-
}
340+
if let Node::ConstBlock(_) = node {
341+
params.push(ty::GenericParamDef {
342+
index: next_index(),
343+
name: Symbol::intern("<const_ty>"),
344+
def_id: def_id.to_def_id(),
345+
pure_wrt_drop: false,
346+
kind: ty::GenericParamDefKind::Type { has_default: false, synthetic: false },
347+
});
353348
}
354349

355350
let param_def_id_to_index = params.iter().map(|param| (param.def_id, param.index)).collect();

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ pub(super) fn explicit_predicates_of<'tcx>(
463463
}
464464
}
465465
} else {
466-
if matches!(def_kind, DefKind::AnonConst) && tcx.lazy_normalization() {
466+
if matches!(def_kind, DefKind::AnonConst) && tcx.features().generic_const_exprs {
467467
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
468468
let parent_def_id = tcx.hir().get_parent_item(hir_id);
469469

compiler/rustc_hir_analysis/src/collect/type_of.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,6 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> {
3434
Node::Ty(&Ty { kind: TyKind::Typeof(ref e), .. }) if e.hir_id == hir_id => {
3535
return tcx.typeck(def_id).node_type(e.hir_id)
3636
}
37-
Node::Expr(&Expr { kind: ExprKind::ConstBlock(ref anon_const), .. })
38-
if anon_const.hir_id == hir_id =>
39-
{
40-
let substs = InternalSubsts::identity_for_item(tcx, def_id.to_def_id());
41-
return substs.as_inline_const().ty()
42-
}
4337
Node::Expr(&Expr { kind: ExprKind::InlineAsm(asm), .. })
4438
| Node::Item(&Item { kind: ItemKind::GlobalAsm(asm), .. })
4539
if asm.operands.iter().any(|(op, _op_sp)| match op {
@@ -487,6 +481,11 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<Ty
487481

488482
Node::AnonConst(_) => anon_const_type_of(tcx, def_id),
489483

484+
Node::ConstBlock(_) => {
485+
let substs = InternalSubsts::identity_for_item(tcx, def_id.to_def_id());
486+
substs.as_inline_const().ty()
487+
}
488+
490489
Node::GenericParam(param) => match &param.kind {
491490
GenericParamKind::Type { default: Some(ty), .. }
492491
| GenericParamKind::Const { ty, .. } => icx.to_ty(ty),

compiler/rustc_hir_analysis/src/outlives/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ pub fn provide(providers: &mut Providers) {
2020
fn inferred_outlives_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[(ty::Clause<'_>, Span)] {
2121
let id = tcx.hir().local_def_id_to_hir_id(item_def_id);
2222

23-
if matches!(tcx.def_kind(item_def_id), hir::def::DefKind::AnonConst) && tcx.lazy_normalization()
23+
if matches!(tcx.def_kind(item_def_id), hir::def::DefKind::AnonConst)
24+
&& tcx.features().generic_const_exprs
2425
{
2526
if tcx.hir().opt_const_param_default_param_def_id(id).is_some() {
2627
// In `generics_of` we set the generics' parent to be our parent's parent which means that

compiler/rustc_hir_pretty/src/lib.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ impl<'a> State<'a> {
8484
Node::ImplItem(a) => self.print_impl_item(a),
8585
Node::Variant(a) => self.print_variant(a),
8686
Node::AnonConst(a) => self.print_anon_const(a),
87+
Node::ConstBlock(a) => self.print_inline_const(a),
8788
Node::Expr(a) => self.print_expr(a),
8889
Node::ExprField(a) => self.print_expr_field(&a),
8990
Node::Stmt(a) => self.print_stmt(a),
@@ -1095,10 +1096,10 @@ impl<'a> State<'a> {
10951096
self.end()
10961097
}
10971098

1098-
fn print_expr_anon_const(&mut self, anon_const: &hir::AnonConst) {
1099+
fn print_inline_const(&mut self, constant: &hir::ConstBlock) {
10991100
self.ibox(INDENT_UNIT);
11001101
self.word_space("const");
1101-
self.print_anon_const(anon_const);
1102+
self.ann.nested(self, Nested::Body(constant.body));
11021103
self.end()
11031104
}
11041105

@@ -1370,7 +1371,7 @@ impl<'a> State<'a> {
13701371
self.print_expr_vec(exprs);
13711372
}
13721373
hir::ExprKind::ConstBlock(ref anon_const) => {
1373-
self.print_expr_anon_const(anon_const);
1374+
self.print_inline_const(anon_const);
13741375
}
13751376
hir::ExprKind::Repeat(element, ref count) => {
13761377
self.print_expr_repeat(element, count);

compiler/rustc_hir_typeck/src/expr.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
348348
}
349349
ExprKind::DropTemps(e) => self.check_expr_with_expectation(e, expected),
350350
ExprKind::Array(args) => self.check_expr_array(args, expected, expr),
351-
ExprKind::ConstBlock(ref anon_const) => {
352-
self.check_expr_const_block(anon_const, expected, expr)
353-
}
351+
ExprKind::ConstBlock(ref block) => self.check_expr_const_block(block, expected, expr),
354352
ExprKind::Repeat(element, ref count) => {
355353
self.check_expr_repeat(element, count, expected, expr)
356354
}
@@ -1368,20 +1366,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13681366

13691367
fn check_expr_const_block(
13701368
&self,
1371-
anon_const: &'tcx hir::AnonConst,
1369+
block: &'tcx hir::ConstBlock,
13721370
expected: Expectation<'tcx>,
13731371
_expr: &'tcx hir::Expr<'tcx>,
13741372
) -> Ty<'tcx> {
1375-
let body = self.tcx.hir().body(anon_const.body);
1373+
let body = self.tcx.hir().body(block.body);
13761374

13771375
// Create a new function context.
1378-
let def_id = anon_const.def_id;
1376+
let def_id = block.def_id;
13791377
let fcx = FnCtxt::new(self, self.param_env.with_const(), def_id);
13801378
crate::GatherLocalsVisitor::new(&fcx).visit_body(body);
13811379

13821380
let ty = fcx.check_expr_with_expectation(&body.value, expected);
13831381
fcx.require_type_is_sized(ty, body.value.span, traits::ConstSized);
1384-
fcx.write_ty(anon_const.hir_id, ty);
1382+
fcx.write_ty(block.hir_id, ty);
13851383
ty
13861384
}
13871385

compiler/rustc_hir_typeck/src/generator_interior/drop_ranges/cfg_build.rs

+1
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ impl<'a, 'tcx> DropRangeVisitor<'a, 'tcx> {
270270
| hir::Node::Variant(..)
271271
| hir::Node::Field(..)
272272
| hir::Node::AnonConst(..)
273+
| hir::Node::ConstBlock(..)
273274
| hir::Node::Stmt(..)
274275
| hir::Node::PathSegment(..)
275276
| hir::Node::Ty(..)

compiler/rustc_infer/src/infer/combine.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ impl<'tcx> InferCtxt<'tcx> {
227227
return self.unify_const_variable(vid, a, relation.param_env());
228228
}
229229
(ty::ConstKind::Unevaluated(..), _) | (_, ty::ConstKind::Unevaluated(..))
230-
if self.tcx.lazy_normalization() =>
230+
if self.tcx.features().generic_const_exprs || self.tcx.trait_solver_next() =>
231231
{
232232
relation.register_const_equate_obligation(a, b);
233233
return Ok(b);

0 commit comments

Comments
 (0)