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

Lines changed: 7 additions & 3 deletions
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

Lines changed: 8 additions & 0 deletions
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

Lines changed: 1 addition & 4 deletions
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

Lines changed: 0 additions & 16 deletions
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

Lines changed: 1 addition & 5 deletions
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

Lines changed: 19 additions & 2 deletions
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

Lines changed: 9 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 11 additions & 16 deletions
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

Lines changed: 1 addition & 1 deletion
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

0 commit comments

Comments
 (0)