Skip to content

Commit 40e5b35

Browse files
BoxyUwUcamelid
authored andcommitted
add ConstArgKind::Error and format
1 parent 4738807 commit 40e5b35

File tree

9 files changed

+72
-55
lines changed

9 files changed

+72
-55
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
201201
|this| {
202202
let ty = this
203203
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
204-
let body = this.lower_anon_const_to_const_arg(
205-
body.as_deref().unwrap(),
206-
!this.tcx.features().min_generic_const_args(),
207-
);
204+
let body = this.lower_item_body_to_const_arg(body.as_deref());
208205
(ty, body)
209206
},
210207
);
@@ -815,12 +812,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
815812
|this| {
816813
let ty = this
817814
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
818-
let body = body.as_deref().map(|body| {
819-
this.lower_anon_const_to_const_arg(
820-
body,
821-
!this.tcx.features().min_generic_const_args(),
822-
)
823-
});
815+
let body = body
816+
.as_deref()
817+
.map(|body| this.lower_item_body_to_const_arg(Some(body)));
824818
hir::TraitItemKind::Const(ty, body)
825819
},
826820
);
@@ -1034,10 +1028,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10341028
let ty = this
10351029
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
10361030
this.lower_define_opaque(hir_id, &define_opaque);
1037-
let body = this.lower_anon_const_to_const_arg(
1038-
body.as_deref().unwrap(),
1039-
!this.tcx.features().min_generic_const_args(),
1040-
);
1031+
let body = this.lower_item_body_to_const_arg(body.as_deref());
10411032
hir::ImplItemKind::Const(ty, body)
10421033
},
10431034
),

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10841084
AssocItemConstraintKind::Equality { term } => {
10851085
let term = match term {
10861086
Term::Ty(ty) => self.lower_ty(ty, itctx).into(),
1087-
Term::Const(c) => self.lower_anon_const_to_const_arg(c, false).into(),
1087+
Term::Const(c) => self.lower_anon_const_to_const_arg(c).into(),
10881088
};
10891089
hir::AssocItemConstraintKind::Equality { term }
10901090
}
@@ -1210,9 +1210,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12101210
}
12111211
GenericArg::Type(self.lower_ty(ty, itctx).try_as_ambig_ty().unwrap())
12121212
}
1213-
ast::GenericArg::Const(ct) => GenericArg::Const(
1214-
self.lower_anon_const_to_const_arg(ct, false).try_as_ambig_ct().unwrap(),
1215-
),
1213+
ast::GenericArg::Const(ct) => {
1214+
GenericArg::Const(self.lower_anon_const_to_const_arg(ct).try_as_ambig_ct().unwrap())
1215+
}
12161216
}
12171217
}
12181218

@@ -2024,7 +2024,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20242024
false
20252025
}
20262026
})
2027-
.map(|def| self.lower_anon_const_to_const_arg(def, false));
2027+
.map(|def| self.lower_anon_const_to_const_arg(def));
20282028

20292029
(
20302030
hir::ParamName::Plain(self.lower_ident(param.ident)),
@@ -2236,7 +2236,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
22362236
let ct_kind = hir::ConstArgKind::Infer(self.lower_span(c.value.span), ());
22372237
self.arena.alloc(hir::ConstArg { hir_id: self.lower_node_id(c.id), kind: ct_kind })
22382238
}
2239-
_ => self.lower_anon_const_to_const_arg(c, false),
2239+
_ => self.lower_anon_const_to_const_arg(c),
22402240
}
22412241
}
22422242

@@ -2305,13 +2305,31 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23052305

23062306
/// See [`hir::ConstArg`] for when to use this function vs
23072307
/// [`Self::lower_anon_const_to_anon_const`].
2308-
fn lower_anon_const_to_const_arg(
2308+
fn lower_item_body_to_const_arg(
23092309
&mut self,
2310-
anon: &AnonConst,
2311-
always_lower_to_anon_const: bool,
2310+
anon: Option<&AnonConst>,
23122311
) -> &'hir hir::ConstArg<'hir> {
2313-
self.arena
2314-
.alloc(self.lower_anon_const_to_const_arg_direct(anon, always_lower_to_anon_const))
2312+
let Some(anon) = anon else {
2313+
let const_arg = ConstArg {
2314+
hir_id: self.next_id(),
2315+
kind: hir::ConstArgKind::Error(
2316+
DUMMY_SP,
2317+
self.dcx().span_delayed_bug(DUMMY_SP, "no block"),
2318+
),
2319+
};
2320+
return self.arena.alloc(const_arg);
2321+
};
2322+
2323+
self.arena.alloc(self.lower_anon_const_to_const_arg_direct(
2324+
anon,
2325+
!self.tcx.features().min_generic_const_args(),
2326+
))
2327+
}
2328+
2329+
/// See [`hir::ConstArg`] for when to use this function vs
2330+
/// [`Self::lower_anon_const_to_anon_const`].
2331+
fn lower_anon_const_to_const_arg(&mut self, anon: &AnonConst) -> &'hir hir::ConstArg<'hir> {
2332+
self.arena.alloc(self.lower_anon_const_to_const_arg_direct(anon, false))
23152333
}
23162334

23172335
#[instrument(level = "debug", skip(self))]

compiler/rustc_ast_lowering/src/pat.rs

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -438,36 +438,35 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
438438
fn lower_ty_pat_mut(&mut self, pattern: &TyPat, base_type: Span) -> hir::TyPat<'hir> {
439439
// loop here to avoid recursion
440440
let pat_hir_id = self.lower_node_id(pattern.id);
441-
let node =
442-
match &pattern.kind {
443-
TyPatKind::Range(e1, e2, Spanned { node: end, span }) => hir::TyPatKind::Range(
444-
e1.as_deref()
445-
.map(|e| self.lower_anon_const_to_const_arg(e, false))
446-
.unwrap_or_else(|| {
447-
self.lower_ty_pat_range_end(
448-
hir::LangItem::RangeMin,
449-
span.shrink_to_lo(),
450-
base_type,
451-
)
452-
}),
453-
e2.as_deref()
454-
.map(|e| match end {
455-
RangeEnd::Included(..) => self.lower_anon_const_to_const_arg(e, false),
456-
RangeEnd::Excluded => self.lower_excluded_range_end(e),
457-
})
458-
.unwrap_or_else(|| {
459-
self.lower_ty_pat_range_end(
460-
hir::LangItem::RangeMax,
461-
span.shrink_to_hi(),
462-
base_type,
463-
)
464-
}),
465-
),
466-
TyPatKind::Or(variants) => hir::TyPatKind::Or(self.arena.alloc_from_iter(
441+
let node = match &pattern.kind {
442+
TyPatKind::Range(e1, e2, Spanned { node: end, span }) => hir::TyPatKind::Range(
443+
e1.as_deref().map(|e| self.lower_anon_const_to_const_arg(e)).unwrap_or_else(|| {
444+
self.lower_ty_pat_range_end(
445+
hir::LangItem::RangeMin,
446+
span.shrink_to_lo(),
447+
base_type,
448+
)
449+
}),
450+
e2.as_deref()
451+
.map(|e| match end {
452+
RangeEnd::Included(..) => self.lower_anon_const_to_const_arg(e),
453+
RangeEnd::Excluded => self.lower_excluded_range_end(e),
454+
})
455+
.unwrap_or_else(|| {
456+
self.lower_ty_pat_range_end(
457+
hir::LangItem::RangeMax,
458+
span.shrink_to_hi(),
459+
base_type,
460+
)
461+
}),
462+
),
463+
TyPatKind::Or(variants) => {
464+
hir::TyPatKind::Or(self.arena.alloc_from_iter(
467465
variants.iter().map(|pat| self.lower_ty_pat_mut(pat, base_type)),
468-
)),
469-
TyPatKind::Err(guar) => hir::TyPatKind::Err(*guar),
470-
};
466+
))
467+
}
468+
TyPatKind::Err(guar) => hir::TyPatKind::Err(*guar),
469+
};
471470

472471
hir::TyPat { hir_id: pat_hir_id, kind: node, span: self.lower_span(pattern.span) }
473472
}

compiler/rustc_hir/src/hir.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ impl<'hir, Unambig> ConstArg<'hir, Unambig> {
473473
match self.kind {
474474
ConstArgKind::Path(path) => path.span(),
475475
ConstArgKind::Anon(anon) => anon.span,
476+
ConstArgKind::Error(span, _) => span,
476477
ConstArgKind::Infer(span, _) => span,
477478
}
478479
}
@@ -489,6 +490,8 @@ pub enum ConstArgKind<'hir, Unambig = ()> {
489490
/// However, in the future, we'll be using it for all of those.
490491
Path(QPath<'hir>),
491492
Anon(&'hir AnonConst),
493+
/// Error const
494+
Error(Span, ErrorGuaranteed),
492495
/// This variant is not always used to represent inference consts, sometimes
493496
/// [`GenericArg::Infer`] is used instead.
494497
Infer(Span, Unambig),

compiler/rustc_hir/src/intravisit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,7 @@ pub fn walk_const_arg<'v, V: Visitor<'v>>(
10581058
match kind {
10591059
ConstArgKind::Path(qpath) => visitor.visit_qpath(qpath, *hir_id, qpath.span()),
10601060
ConstArgKind::Anon(anon) => visitor.visit_anon_const(*anon),
1061+
ConstArgKind::Error(_, _) => V::Result::output(), // errors and spans are not impotant
10611062
}
10621063
}
10631064

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2206,6 +2206,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
22062206
}
22072207
hir::ConstArgKind::Anon(anon) => self.lower_anon_const(anon),
22082208
hir::ConstArgKind::Infer(span, ()) => self.ct_infer(None, span),
2209+
hir::ConstArgKind::Error(_, e) => ty::Const::new_error(tcx, e),
22092210
}
22102211
}
22112212

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,7 @@ impl<'a> State<'a> {
11291129
match &const_arg.kind {
11301130
ConstArgKind::Path(qpath) => self.print_qpath(qpath, true),
11311131
ConstArgKind::Anon(anon) => self.print_anon_const(anon),
1132+
ConstArgKind::Error(_, _) => self.word("/*ERROR*/"),
11321133
ConstArgKind::Infer(..) => self.word("_"),
11331134
}
11341135
}

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1429,7 +1429,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
14291429
&& match tcx.hir_node_by_def_id(local_id) {
14301430
hir::Node::ConstArg(hir::ConstArg { kind, .. }) => match kind {
14311431
// Skip encoding defs for these as they should not have had a `DefId` created
1432-
hir::ConstArgKind::Path(..) | hir::ConstArgKind::Infer(..) => true,
1432+
hir::ConstArgKind::Error(..)
1433+
| hir::ConstArgKind::Path(..)
1434+
| hir::ConstArgKind::Infer(..) => true,
14331435
hir::ConstArgKind::Anon(..) => false,
14341436
},
14351437
_ => false,

compiler/rustc_ty_utils/src/instance.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use rustc_errors::ErrorGuaranteed;
2+
use rustc_hir::def::DefKind;
23
use rustc_hir::LangItem;
34
use rustc_hir::def_id::DefId;
45
use rustc_infer::infer::TyCtxtInferExt;

0 commit comments

Comments
 (0)