Skip to content

Commit c95f08b

Browse files
committed
Auto merge of #133728 - jhpratt:rollup-k1i60pg, r=jhpratt
Rollup of 4 pull requests Successful merges: - #133589 (Remove `hir::ArrayLen`) - #133672 (Remove a bunch of unnecessary const stability noise) - #133678 (Stabilize `ptr::fn_addr_eq`) - #133727 (Update mailmap) r? `@ghost` `@rustbot` modify labels: rollup
2 parents caa8172 + 9cac33a commit c95f08b

File tree

40 files changed

+108
-232
lines changed

40 files changed

+108
-232
lines changed

.mailmap

+1
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ Jack Huey <[email protected]> <[email protected]>
254254
255255
Jacob Greenfield <[email protected]>
256256
257+
257258
Jake Vossen <[email protected]>
258259
259260
Jakob Lautrup Nysom <[email protected]>

compiler/rustc_ast_lowering/src/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
116116
}
117117
ExprKind::Repeat(expr, count) => {
118118
let expr = self.lower_expr(expr);
119-
let count = self.lower_array_length(count);
119+
let count = self.lower_array_length_to_const_arg(count);
120120
hir::ExprKind::Repeat(expr, count)
121121
}
122122
ExprKind::Tup(elts) => hir::ExprKind::Tup(self.lower_exprs(elts)),

compiler/rustc_ast_lowering/src/index.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
235235
}
236236

237237
fn visit_anon_const(&mut self, constant: &'hir AnonConst) {
238-
// FIXME: use real span?
239-
self.insert(DUMMY_SP, constant.hir_id, Node::AnonConst(constant));
238+
self.insert(constant.span, constant.hir_id, Node::AnonConst(constant));
240239

241240
self.with_parent(constant.hir_id, |this| {
242241
intravisit::walk_anon_const(this, constant);
@@ -252,8 +251,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
252251
}
253252

254253
fn visit_const_arg(&mut self, const_arg: &'hir ConstArg<'hir>) {
255-
// FIXME: use real span?
256-
self.insert(DUMMY_SP, const_arg.hir_id, Node::ConstArg(const_arg));
254+
self.insert(const_arg.span(), const_arg.hir_id, Node::ConstArg(const_arg));
257255

258256
self.with_parent(const_arg.hir_id, |this| {
259257
intravisit::walk_const_arg(this, const_arg);
@@ -387,13 +385,6 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
387385
});
388386
}
389387

390-
fn visit_array_length(&mut self, len: &'hir ArrayLen<'hir>) {
391-
match len {
392-
ArrayLen::Infer(inf) => self.insert(inf.span, inf.hir_id, Node::ArrayLenInfer(inf)),
393-
ArrayLen::Body(..) => intravisit::walk_array_len(self, len),
394-
}
395-
}
396-
397388
fn visit_pattern_type_pattern(&mut self, p: &'hir hir::Pat<'hir>) {
398389
self.visit_pat(p)
399390
}

compiler/rustc_ast_lowering/src/lib.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -1257,9 +1257,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12571257
}),
12581258
))
12591259
}
1260-
TyKind::Array(ty, length) => {
1261-
hir::TyKind::Array(self.lower_ty(ty, itctx), self.lower_array_length(length))
1262-
}
1260+
TyKind::Array(ty, length) => hir::TyKind::Array(
1261+
self.lower_ty(ty, itctx),
1262+
self.lower_array_length_to_const_arg(length),
1263+
),
12631264
TyKind::Typeof(expr) => hir::TyKind::Typeof(self.lower_anon_const_to_anon_const(expr)),
12641265
TyKind::TraitObject(bounds, kind) => {
12651266
let mut lifetime_bound = None;
@@ -2007,14 +2008,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20072008
self.expr_block(block)
20082009
}
20092010

2010-
fn lower_array_length(&mut self, c: &AnonConst) -> hir::ArrayLen<'hir> {
2011+
fn lower_array_length_to_const_arg(&mut self, c: &AnonConst) -> &'hir hir::ConstArg<'hir> {
20112012
match c.value.kind {
20122013
ExprKind::Underscore => {
20132014
if self.tcx.features().generic_arg_infer() {
2014-
hir::ArrayLen::Infer(hir::InferArg {
2015-
hir_id: self.lower_node_id(c.id),
2016-
span: self.lower_span(c.value.span),
2017-
})
2015+
let ct_kind = hir::ConstArgKind::Infer(self.lower_span(c.value.span));
2016+
self.arena.alloc(hir::ConstArg { hir_id: self.next_id(), kind: ct_kind })
20182017
} else {
20192018
feature_err(
20202019
&self.tcx.sess,
@@ -2023,10 +2022,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20232022
fluent_generated::ast_lowering_underscore_array_length_unstable,
20242023
)
20252024
.stash(c.value.span, StashKey::UnderscoreForArrayLengths);
2026-
hir::ArrayLen::Body(self.lower_anon_const_to_const_arg(c))
2025+
self.lower_anon_const_to_const_arg(c)
20272026
}
20282027
}
2029-
_ => hir::ArrayLen::Body(self.lower_anon_const_to_const_arg(c)),
2028+
_ => self.lower_anon_const_to_const_arg(c),
20302029
}
20312030
}
20322031

compiler/rustc_hir/src/hir.rs

+18-29
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ impl<'hir> ConstArg<'hir> {
275275
match self.kind {
276276
ConstArgKind::Path(path) => path.span(),
277277
ConstArgKind::Anon(anon) => anon.span,
278+
ConstArgKind::Infer(span) => span,
278279
}
279280
}
280281
}
@@ -289,6 +290,11 @@ pub enum ConstArgKind<'hir> {
289290
/// However, in the future, we'll be using it for all of those.
290291
Path(QPath<'hir>),
291292
Anon(&'hir AnonConst),
293+
/// **Note:** Not all inferred consts are represented as
294+
/// `ConstArgKind::Infer`. In cases where it is ambiguous whether
295+
/// a generic arg is a type or a const, inference variables are
296+
/// represented as `GenericArg::Infer` instead.
297+
Infer(Span),
292298
}
293299

294300
#[derive(Clone, Copy, Debug, HashStable_Generic)]
@@ -308,6 +314,10 @@ pub enum GenericArg<'hir> {
308314
Lifetime(&'hir Lifetime),
309315
Type(&'hir Ty<'hir>),
310316
Const(&'hir ConstArg<'hir>),
317+
/// **Note:** Inference variables are only represented as
318+
/// `GenericArg::Infer` in cases where it is ambiguous whether
319+
/// a generic arg is a type or a const. Otherwise, inference variables
320+
/// are represented as `TyKind::Infer` or `ConstArgKind::Infer`.
311321
Infer(InferArg),
312322
}
313323

@@ -1645,29 +1655,6 @@ impl fmt::Display for ConstContext {
16451655
/// A literal.
16461656
pub type Lit = Spanned<LitKind>;
16471657

1648-
#[derive(Copy, Clone, Debug, HashStable_Generic)]
1649-
pub enum ArrayLen<'hir> {
1650-
Infer(InferArg),
1651-
Body(&'hir ConstArg<'hir>),
1652-
}
1653-
1654-
impl ArrayLen<'_> {
1655-
pub fn span(self) -> Span {
1656-
match self {
1657-
ArrayLen::Infer(arg) => arg.span,
1658-
ArrayLen::Body(body) => body.span(),
1659-
}
1660-
}
1661-
1662-
pub fn hir_id(self) -> HirId {
1663-
match self {
1664-
ArrayLen::Infer(InferArg { hir_id, .. }) | ArrayLen::Body(&ConstArg { hir_id, .. }) => {
1665-
hir_id
1666-
}
1667-
}
1668-
}
1669-
}
1670-
16711658
/// A constant (expression) that's not an item or associated item,
16721659
/// but needs its own `DefId` for type-checking, const-eval, etc.
16731660
/// These are usually found nested inside types (e.g., array lengths)
@@ -2115,7 +2102,7 @@ pub enum ExprKind<'hir> {
21152102
///
21162103
/// E.g., `[1; 5]`. The first expression is the element
21172104
/// to be repeated; the second is the number of times to repeat it.
2118-
Repeat(&'hir Expr<'hir>, ArrayLen<'hir>),
2105+
Repeat(&'hir Expr<'hir>, &'hir ConstArg<'hir>),
21192106

21202107
/// A suspension point for coroutines (i.e., `yield <expr>`).
21212108
Yield(&'hir Expr<'hir>, YieldSource),
@@ -2625,7 +2612,7 @@ impl<'hir> Ty<'hir> {
26252612
TyKind::Infer => true,
26262613
TyKind::Slice(ty) => ty.is_suggestable_infer_ty(),
26272614
TyKind::Array(ty, length) => {
2628-
ty.is_suggestable_infer_ty() || matches!(length, ArrayLen::Infer(..))
2615+
ty.is_suggestable_infer_ty() || matches!(length.kind, ConstArgKind::Infer(..))
26292616
}
26302617
TyKind::Tup(tys) => tys.iter().any(Self::is_suggestable_infer_ty),
26312618
TyKind::Ptr(mut_ty) | TyKind::Ref(_, mut_ty) => mut_ty.ty.is_suggestable_infer_ty(),
@@ -2834,7 +2821,7 @@ pub enum TyKind<'hir> {
28342821
/// A variable length slice (i.e., `[T]`).
28352822
Slice(&'hir Ty<'hir>),
28362823
/// A fixed length array (i.e., `[T; n]`).
2837-
Array(&'hir Ty<'hir>, ArrayLen<'hir>),
2824+
Array(&'hir Ty<'hir>, &'hir ConstArg<'hir>),
28382825
/// A raw pointer (i.e., `*const T` or `*mut T`).
28392826
Ptr(MutTy<'hir>),
28402827
/// A reference (i.e., `&'a T` or `&'a mut T`).
@@ -2861,6 +2848,11 @@ pub enum TyKind<'hir> {
28612848
Typeof(&'hir AnonConst),
28622849
/// `TyKind::Infer` means the type should be inferred instead of it having been
28632850
/// specified. This can appear anywhere in a type.
2851+
///
2852+
/// **Note:** Not all inferred types are represented as
2853+
/// `TyKind::Infer`. In cases where it is ambiguous whether
2854+
/// a generic arg is a type or a const, inference variables are
2855+
/// represented as `GenericArg::Infer` instead.
28642856
Infer,
28652857
/// Placeholder for a type that has failed to be defined.
28662858
Err(rustc_span::ErrorGuaranteed),
@@ -3801,8 +3793,6 @@ pub enum Node<'hir> {
38013793
Crate(&'hir Mod<'hir>),
38023794
Infer(&'hir InferArg),
38033795
WherePredicate(&'hir WherePredicate<'hir>),
3804-
// FIXME: Merge into `Node::Infer`.
3805-
ArrayLenInfer(&'hir InferArg),
38063796
PreciseCapturingNonLifetimeArg(&'hir PreciseCapturingNonLifetimeArg),
38073797
// Created by query feeding
38083798
Synthetic,
@@ -3856,7 +3846,6 @@ impl<'hir> Node<'hir> {
38563846
| Node::OpaqueTy(..)
38573847
| Node::Infer(..)
38583848
| Node::WherePredicate(..)
3859-
| Node::ArrayLenInfer(..)
38603849
| Node::Synthetic
38613850
| Node::Err(..) => None,
38623851
}

compiler/rustc_hir/src/intravisit.rs

+3-13
Original file line numberDiff line numberDiff line change
@@ -343,9 +343,6 @@ pub trait Visitor<'v>: Sized {
343343
fn visit_pat_field(&mut self, f: &'v PatField<'v>) -> Self::Result {
344344
walk_pat_field(self, f)
345345
}
346-
fn visit_array_length(&mut self, len: &'v ArrayLen<'v>) -> Self::Result {
347-
walk_array_len(self, len)
348-
}
349346
fn visit_anon_const(&mut self, c: &'v AnonConst) -> Self::Result {
350347
walk_anon_const(self, c)
351348
}
@@ -710,14 +707,6 @@ pub fn walk_pat_field<'v, V: Visitor<'v>>(visitor: &mut V, field: &'v PatField<'
710707
visitor.visit_pat(field.pat)
711708
}
712709

713-
pub fn walk_array_len<'v, V: Visitor<'v>>(visitor: &mut V, len: &'v ArrayLen<'v>) -> V::Result {
714-
match len {
715-
// FIXME: Use `visit_infer` here.
716-
ArrayLen::Infer(InferArg { hir_id, span: _ }) => visitor.visit_id(*hir_id),
717-
ArrayLen::Body(c) => visitor.visit_const_arg(c),
718-
}
719-
}
720-
721710
pub fn walk_anon_const<'v, V: Visitor<'v>>(visitor: &mut V, constant: &'v AnonConst) -> V::Result {
722711
try_visit!(visitor.visit_id(constant.hir_id));
723712
visitor.visit_nested_body(constant.body)
@@ -739,6 +728,7 @@ pub fn walk_const_arg<'v, V: Visitor<'v>>(
739728
match &const_arg.kind {
740729
ConstArgKind::Path(qpath) => visitor.visit_qpath(qpath, const_arg.hir_id, qpath.span()),
741730
ConstArgKind::Anon(anon) => visitor.visit_anon_const(*anon),
731+
ConstArgKind::Infer(..) => V::Result::output(),
742732
}
743733
}
744734

@@ -753,7 +743,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>)
753743
}
754744
ExprKind::Repeat(ref element, ref count) => {
755745
try_visit!(visitor.visit_expr(element));
756-
try_visit!(visitor.visit_array_length(count));
746+
try_visit!(visitor.visit_const_arg(count));
757747
}
758748
ExprKind::Struct(ref qpath, fields, ref optional_base) => {
759749
try_visit!(visitor.visit_qpath(qpath, expression.hir_id, expression.span));
@@ -901,7 +891,7 @@ pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty<'v>) -> V::Resul
901891
}
902892
TyKind::Array(ref ty, ref length) => {
903893
try_visit!(visitor.visit_ty(ty));
904-
try_visit!(visitor.visit_array_length(length));
894+
try_visit!(visitor.visit_const_arg(length));
905895
}
906896
TyKind::TraitObject(bounds, ref lifetime, _syntax) => {
907897
for bound in bounds {

compiler/rustc_hir_analysis/src/collect.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,11 @@ impl<'v> Visitor<'v> for HirPlaceholderCollector {
151151
_ => {}
152152
}
153153
}
154-
fn visit_array_length(&mut self, length: &'v hir::ArrayLen<'v>) {
155-
if let hir::ArrayLen::Infer(inf) = length {
156-
self.0.push(inf.span);
154+
fn visit_const_arg(&mut self, const_arg: &'v hir::ConstArg<'v>) {
155+
if let hir::ConstArgKind::Infer(span) = const_arg.kind {
156+
self.0.push(span);
157157
}
158-
intravisit::walk_array_len(self, length)
158+
intravisit::walk_const_arg(self, const_arg)
159159
}
160160
}
161161

compiler/rustc_hir_analysis/src/collect/generics_of.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
181181
// expressions' count (i.e. `N` in `[x; N]`), and explicit
182182
// `enum` discriminants (i.e. `D` in `enum Foo { Bar = D }`),
183183
// as they shouldn't be able to cause query cycle errors.
184-
Node::Expr(Expr { kind: ExprKind::Repeat(_, ArrayLen::Body(ct)), .. })
184+
Node::Expr(Expr { kind: ExprKind::Repeat(_, ct), .. })
185185
if ct.anon_const_hir_id() == Some(hir_id) =>
186186
{
187187
Some(parent_did)

compiler/rustc_hir_analysis/src/collect/type_of.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ fn const_arg_anon_type_of<'tcx>(tcx: TyCtxt<'tcx>, arg_hir_id: HirId, span: Span
145145
// Easy case: arrays repeat expressions.
146146
Node::Ty(&hir::Ty { kind: TyKind::Array(_, ref constant), .. })
147147
| Node::Expr(&Expr { kind: ExprKind::Repeat(_, ref constant), .. })
148-
if constant.hir_id() == arg_hir_id =>
148+
if constant.hir_id == arg_hir_id =>
149149
{
150150
return tcx.types.usize;
151151
}
@@ -579,8 +579,6 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
579579
x => bug!("unexpected non-type Node::GenericParam: {:?}", x),
580580
},
581581

582-
Node::ArrayLenInfer(_) => tcx.types.usize,
583-
584582
x => {
585583
bug!("unexpected sort of node in type_of(): {:?}", x);
586584
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ fn generic_arg_mismatch_err(
104104
GenericArg::Type(hir::Ty { kind: hir::TyKind::Array(_, len), .. }),
105105
GenericParamDefKind::Const { .. },
106106
) if tcx.type_of(param.def_id).skip_binder() == tcx.types.usize => {
107-
let snippet = sess.source_map().span_to_snippet(tcx.hir().span(len.hir_id()));
107+
let snippet = sess.source_map().span_to_snippet(tcx.hir().span(len.hir_id));
108108
if let Ok(snippet) = snippet {
109109
err.span_suggestion(
110110
arg.span(),

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -2090,6 +2090,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
20902090
format!("Const::lower_const_arg: invalid qpath {qpath:?}"),
20912091
),
20922092
hir::ConstArgKind::Anon(anon) => Const::from_anon_const(tcx, anon.def_id),
2093+
hir::ConstArgKind::Infer(span) => self.ct_infer(None, span),
20932094
}
20942095
}
20952096

@@ -2310,13 +2311,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23102311
tcx.at(span).type_of(def_id).instantiate(tcx, args)
23112312
}
23122313
hir::TyKind::Array(ty, length) => {
2313-
let length = match length {
2314-
hir::ArrayLen::Infer(inf) => self.ct_infer(None, inf.span),
2315-
hir::ArrayLen::Body(constant) => {
2316-
self.lower_const_arg(constant, FeedConstTy::No)
2317-
}
2318-
};
2319-
2314+
let length = self.lower_const_arg(length, FeedConstTy::No);
23202315
Ty::new_array_with_const_len(tcx, self.lower_ty(ty), length)
23212316
}
23222317
hir::TyKind::Typeof(e) => tcx.type_of(e.def_id).instantiate_identity(),

compiler/rustc_hir_pretty/src/lib.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ impl<'a> State<'a> {
118118
Node::LetStmt(a) => self.print_local_decl(a),
119119
Node::Crate(..) => panic!("cannot print Crate"),
120120
Node::WherePredicate(pred) => self.print_where_predicate(pred),
121-
Node::ArrayLenInfer(_) => self.word("_"),
122121
Node::Synthetic => unreachable!(),
123122
Node::Err(_) => self.word("/*ERROR*/"),
124123
}
@@ -315,7 +314,7 @@ impl<'a> State<'a> {
315314
self.word("[");
316315
self.print_type(ty);
317316
self.word("; ");
318-
self.print_array_length(length);
317+
self.print_const_arg(length);
319318
self.word("]");
320319
}
321320
hir::TyKind::Typeof(ref e) => {
@@ -986,13 +985,6 @@ impl<'a> State<'a> {
986985
self.print_else(elseopt)
987986
}
988987

989-
fn print_array_length(&mut self, len: &hir::ArrayLen<'_>) {
990-
match len {
991-
hir::ArrayLen::Infer(..) => self.word("_"),
992-
hir::ArrayLen::Body(ct) => self.print_const_arg(ct),
993-
}
994-
}
995-
996988
fn print_anon_const(&mut self, constant: &hir::AnonConst) {
997989
self.ann.nested(self, Nested::Body(constant.body))
998990
}
@@ -1001,6 +993,7 @@ impl<'a> State<'a> {
1001993
match &const_arg.kind {
1002994
ConstArgKind::Path(qpath) => self.print_qpath(qpath, true),
1003995
ConstArgKind::Anon(anon) => self.print_anon_const(anon),
996+
ConstArgKind::Infer(..) => self.word("_"),
1004997
}
1005998
}
1006999

@@ -1073,12 +1066,12 @@ impl<'a> State<'a> {
10731066
self.end()
10741067
}
10751068

1076-
fn print_expr_repeat(&mut self, element: &hir::Expr<'_>, count: &hir::ArrayLen<'_>) {
1069+
fn print_expr_repeat(&mut self, element: &hir::Expr<'_>, count: &hir::ConstArg<'_>) {
10771070
self.ibox(INDENT_UNIT);
10781071
self.word("[");
10791072
self.print_expr(element);
10801073
self.word_space(";");
1081-
self.print_array_length(count);
1074+
self.print_const_arg(count);
10821075
self.word("]");
10831076
self.end()
10841077
}

0 commit comments

Comments
 (0)