Skip to content

Commit 32471f7

Browse files
committed
Auto merge of #58503 - varkor:const-generics-hir, r=petrochenkov
Add const generics to the HIR Split out from #53645. cc @yodaldevoid r? @eddyb
2 parents c61b92b + 727e204 commit 32471f7

34 files changed

+390
-191
lines changed

src/librustc/hir/def.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ pub enum Def {
5252
AssociatedExistential(DefId),
5353
PrimTy(hir::PrimTy),
5454
TyParam(DefId),
55-
ConstParam(DefId),
5655
SelfTy(Option<DefId> /* trait */, Option<DefId> /* impl */),
5756
ToolMod, // e.g., `rustfmt` in `#[rustfmt::skip]`
5857

5958
// Value namespace
6059
Fn(DefId),
6160
Const(DefId),
61+
ConstParam(DefId),
6262
Static(DefId, bool /* is_mutbl */),
6363
StructCtor(DefId, CtorKind), // `DefId` refers to `NodeId` of the struct's constructor
6464
VariantCtor(DefId, CtorKind), // `DefId` refers to the enum variant

src/librustc/hir/intravisit.rs

+2
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ pub trait Visitor<'v> : Sized {
334334
match generic_arg {
335335
GenericArg::Lifetime(lt) => self.visit_lifetime(lt),
336336
GenericArg::Type(ty) => self.visit_ty(ty),
337+
GenericArg::Const(ct) => self.visit_anon_const(&ct.value),
337338
}
338339
}
339340
fn visit_lifetime(&mut self, lifetime: &'v Lifetime) {
@@ -752,6 +753,7 @@ pub fn walk_generic_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v Generi
752753
match param.kind {
753754
GenericParamKind::Lifetime { .. } => {}
754755
GenericParamKind::Type { ref default, .. } => walk_list!(visitor, visit_ty, default),
756+
GenericParamKind::Const { ref ty } => visitor.visit_ty(ty),
755757
}
756758
walk_list!(visitor, visit_param_bound, &param.bounds);
757759
}

src/librustc/hir/lowering.rs

+9-16
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use crate::hir::HirVec;
3636
use crate::hir::map::{DefKey, DefPathData, Definitions};
3737
use crate::hir::def_id::{DefId, DefIndex, DefIndexAddressSpace, CRATE_DEF_INDEX};
3838
use crate::hir::def::{Def, PathResolution, PerNS};
39-
use crate::hir::GenericArg;
39+
use crate::hir::{GenericArg, ConstArg};
4040
use crate::lint::builtin::{self, PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES,
4141
ELIDED_LIFETIMES_IN_PATHS};
4242
use crate::middle::cstore::CrateStore;
@@ -1172,13 +1172,10 @@ impl<'a> LoweringContext<'a> {
11721172
ast::GenericArg::Lifetime(lt) => GenericArg::Lifetime(self.lower_lifetime(&lt)),
11731173
ast::GenericArg::Type(ty) => GenericArg::Type(self.lower_ty_direct(&ty, itctx)),
11741174
ast::GenericArg::Const(ct) => {
1175-
// FIXME(const_generics): const generics are not yet defined in the HIR.
1176-
self.sess.struct_span_err(
1177-
ct.value.span,
1178-
"const generics in any position are currently unsupported",
1179-
).emit();
1180-
self.sess.abort_if_errors();
1181-
bug!();
1175+
GenericArg::Const(ConstArg {
1176+
value: self.lower_anon_const(&ct),
1177+
span: ct.value.span,
1178+
})
11821179
}
11831180
}
11841181
}
@@ -2520,14 +2517,10 @@ impl<'a> LoweringContext<'a> {
25202517

25212518
(hir::ParamName::Plain(ident), kind)
25222519
}
2523-
GenericParamKind::Const { .. } => {
2524-
// FIXME(const_generics): const generics are not yet defined in the HIR.
2525-
self.sess.struct_span_err(
2526-
param.ident.span,
2527-
"const generics in any position are currently unsupported",
2528-
).emit();
2529-
self.sess.abort_if_errors();
2530-
bug!();
2520+
GenericParamKind::Const { ref ty } => {
2521+
(hir::ParamName::Plain(param.ident), hir::GenericParamKind::Const {
2522+
ty: self.lower_ty(&ty, ImplTraitContext::disallowed()),
2523+
})
25312524
}
25322525
};
25332526

src/librustc/hir/map/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ impl<'hir> Map<'hir> {
398398
Some(match param.kind {
399399
GenericParamKind::Lifetime { .. } => Def::Local(param.id),
400400
GenericParamKind::Type { .. } => Def::TyParam(self.local_def_id(param.id)),
401+
GenericParamKind::Const { .. } => Def::ConstParam(self.local_def_id(param.id)),
401402
})
402403
}
403404
}

src/librustc/hir/mod.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -389,24 +389,33 @@ impl PathSegment {
389389
}
390390
}
391391

392+
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
393+
pub struct ConstArg {
394+
pub value: AnonConst,
395+
pub span: Span,
396+
}
397+
392398
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
393399
pub enum GenericArg {
394400
Lifetime(Lifetime),
395401
Type(Ty),
402+
Const(ConstArg),
396403
}
397404

398405
impl GenericArg {
399406
pub fn span(&self) -> Span {
400407
match self {
401408
GenericArg::Lifetime(l) => l.span,
402409
GenericArg::Type(t) => t.span,
410+
GenericArg::Const(c) => c.span,
403411
}
404412
}
405413

406414
pub fn id(&self) -> NodeId {
407415
match self {
408416
GenericArg::Lifetime(l) => l.id,
409417
GenericArg::Type(t) => t.id,
418+
GenericArg::Const(c) => c.value.id,
410419
}
411420
}
412421
}
@@ -448,6 +457,7 @@ impl GenericArgs {
448457
}
449458
break;
450459
}
460+
GenericArg::Const(_) => {}
451461
}
452462
}
453463
}
@@ -464,6 +474,7 @@ impl GenericArgs {
464474
match arg {
465475
GenericArg::Lifetime(_) => own_counts.lifetimes += 1,
466476
GenericArg::Type(_) => own_counts.types += 1,
477+
GenericArg::Const(_) => own_counts.consts += 1,
467478
};
468479
}
469480

@@ -528,6 +539,9 @@ pub enum GenericParamKind {
528539
Type {
529540
default: Option<P<Ty>>,
530541
synthetic: Option<SyntheticTyParamKind>,
542+
},
543+
Const {
544+
ty: P<Ty>,
531545
}
532546
}
533547

@@ -548,6 +562,7 @@ pub struct GenericParam {
548562
pub struct GenericParamCount {
549563
pub lifetimes: usize,
550564
pub types: usize,
565+
pub consts: usize,
551566
}
552567

553568
/// Represents lifetimes and type parameters attached to a declaration
@@ -582,6 +597,7 @@ impl Generics {
582597
match param.kind {
583598
GenericParamKind::Lifetime { .. } => own_counts.lifetimes += 1,
584599
GenericParamKind::Type { .. } => own_counts.types += 1,
600+
GenericParamKind::Const { .. } => own_counts.consts += 1,
585601
};
586602
}
587603

@@ -1302,7 +1318,7 @@ impl BodyOwnerKind {
13021318
/// These are usually found nested inside types (e.g., array lengths)
13031319
/// or expressions (e.g., repeat counts), and also used to define
13041320
/// explicit discriminant values for enum variants.
1305-
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Debug)]
1321+
#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Debug)]
13061322
pub struct AnonConst {
13071323
pub id: NodeId,
13081324
pub hir_id: HirId,

src/librustc/hir/pat_util.rs

-13
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,6 @@ impl hir::Pat {
6464
}
6565
}
6666

67-
pub fn is_const(&self) -> bool {
68-
match self.node {
69-
PatKind::Path(hir::QPath::TypeRelative(..)) => true,
70-
PatKind::Path(hir::QPath::Resolved(_, ref path)) => {
71-
match path.def {
72-
Def::Const(..) | Def::AssociatedConst(..) => true,
73-
_ => false
74-
}
75-
}
76-
_ => false
77-
}
78-
}
79-
8067
/// Call `f` on every "binding" in a pattern, e.g., on `a` in
8168
/// `match foo() { Some(a) => (), None => () }`
8269
pub fn each_binding<F>(&self, mut f: F)

src/librustc/hir/print.rs

+21-18
Original file line numberDiff line numberDiff line change
@@ -1711,31 +1711,25 @@ impl<'a> State<'a> {
17111711
}
17121712
};
17131713

1714-
let mut types = vec![];
1715-
let mut elide_lifetimes = true;
1716-
for arg in &generic_args.args {
1717-
match arg {
1718-
GenericArg::Lifetime(lt) => {
1719-
if !lt.is_elided() {
1720-
elide_lifetimes = false;
1721-
}
1722-
}
1723-
GenericArg::Type(ty) => {
1724-
types.push(ty);
1725-
}
1714+
let mut nonelided_generic_args: bool = false;
1715+
let elide_lifetimes = generic_args.args.iter().all(|arg| match arg {
1716+
GenericArg::Lifetime(lt) => lt.is_elided(),
1717+
_ => {
1718+
nonelided_generic_args = true;
1719+
true
17261720
}
1727-
}
1728-
if !elide_lifetimes {
1721+
});
1722+
1723+
if nonelided_generic_args {
17291724
start_or_comma(self)?;
17301725
self.commasep(Inconsistent, &generic_args.args, |s, generic_arg| {
17311726
match generic_arg {
1732-
GenericArg::Lifetime(lt) => s.print_lifetime(lt),
1727+
GenericArg::Lifetime(lt) if !elide_lifetimes => s.print_lifetime(lt),
1728+
GenericArg::Lifetime(_) => Ok(()),
17331729
GenericArg::Type(ty) => s.print_type(ty),
1730+
GenericArg::Const(ct) => s.print_anon_const(&ct.value),
17341731
}
17351732
})?;
1736-
} else if !types.is_empty() {
1737-
start_or_comma(self)?;
1738-
self.commasep(Inconsistent, &types, |s, ty| s.print_type(&ty))?;
17391733
}
17401734

17411735
// FIXME(eddyb) This would leak into error messages, e.g.:
@@ -2106,7 +2100,12 @@ impl<'a> State<'a> {
21062100
}
21072101

21082102
pub fn print_generic_param(&mut self, param: &GenericParam) -> io::Result<()> {
2103+
if let GenericParamKind::Const { .. } = param.kind {
2104+
self.word_space("const")?;
2105+
}
2106+
21092107
self.print_ident(param.name.ident())?;
2108+
21102109
match param.kind {
21112110
GenericParamKind::Lifetime { .. } => {
21122111
let mut sep = ":";
@@ -2133,6 +2132,10 @@ impl<'a> State<'a> {
21332132
_ => Ok(()),
21342133
}
21352134
}
2135+
GenericParamKind::Const { ref ty } => {
2136+
self.word_space(":")?;
2137+
self.print_type(ty)
2138+
}
21362139
}
21372140
}
21382141

src/librustc/ich/impls_hir.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,15 @@ impl_stable_hash_for!(struct hir::PathSegment {
179179
args
180180
});
181181

182+
impl_stable_hash_for!(struct hir::ConstArg {
183+
value,
184+
span,
185+
});
186+
182187
impl_stable_hash_for!(enum hir::GenericArg {
183188
Lifetime(lt),
184-
Type(ty)
189+
Type(ty),
190+
Const(ct),
185191
});
186192

187193
impl_stable_hash_for!(struct hir::GenericArgs {
@@ -231,6 +237,9 @@ impl<'a> HashStable<StableHashingContext<'a>> for hir::GenericParamKind {
231237
default.hash_stable(hcx, hasher);
232238
synthetic.hash_stable(hcx, hasher);
233239
}
240+
hir::GenericParamKind::Const { ref ty } => {
241+
ty.hash_stable(hcx, hasher);
242+
}
234243
}
235244
}
236245
}

src/librustc/middle/mem_categorization.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
704704
hir_id, expr_ty, def);
705705

706706
match def {
707-
Def::StructCtor(..) | Def::VariantCtor(..) | Def::Const(..) |
707+
Def::StructCtor(..) | Def::VariantCtor(..) | Def::Const(..) | Def::ConstParam(..) |
708708
Def::AssociatedConst(..) | Def::Fn(..) | Def::Method(..) | Def::SelfCtor(..) => {
709709
Ok(self.cat_rvalue_node(hir_id, span, expr_ty))
710710
}

src/librustc/middle/reachable.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,11 @@ impl<'a, 'tcx> ReachableContext<'a, 'tcx> {
315315
Node::Ty(_) |
316316
Node::MacroDef(_) => {}
317317
_ => {
318-
bug!("found unexpected thingy in worklist: {}",
319-
self.tcx.hir().node_to_string(search_item))
318+
bug!(
319+
"found unexpected node kind in worklist: {} ({:?})",
320+
self.tcx.hir().node_to_string(search_item),
321+
node,
322+
);
320323
}
321324
}
322325
}

0 commit comments

Comments
 (0)