Skip to content

Commit 7f3c6d7

Browse files
varkoryodaldevoid
andcommitted
Take Const into account in HIR
Co-Authored-By: Gabriel Smith <[email protected]>
1 parent ddf8811 commit 7f3c6d7

File tree

10 files changed

+166
-113
lines changed

10 files changed

+166
-113
lines changed

src/librustc/hir/intravisit.rs

Lines changed: 2 additions & 0 deletions
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/map/mod.rs

Lines changed: 1 addition & 0 deletions
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/ich/impls_hir.rs

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

Lines changed: 1 addition & 1 deletion
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/resolve_lifetime.rs

Lines changed: 83 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -527,23 +527,20 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
527527
} else {
528528
0
529529
};
530-
let mut type_count = 0;
531-
let lifetimes = generics
532-
.params
533-
.iter()
534-
.filter_map(|param| match param.kind {
535-
GenericParamKind::Lifetime { .. } => {
536-
Some(Region::early(&self.tcx.hir(), &mut index, param))
537-
}
538-
GenericParamKind::Type { .. } => {
539-
type_count += 1;
540-
None
541-
}
542-
})
543-
.collect();
530+
let mut non_lifetime_count = 0;
531+
let lifetimes = generics.params.iter().filter_map(|param| match param.kind {
532+
GenericParamKind::Lifetime { .. } => {
533+
Some(Region::early(&self.tcx.hir(), &mut index, param))
534+
}
535+
GenericParamKind::Type { .. } |
536+
GenericParamKind::Const { .. } => {
537+
non_lifetime_count += 1;
538+
None
539+
}
540+
}).collect();
544541
let scope = Scope::Binder {
545542
lifetimes,
546-
next_early_index: index + type_count,
543+
next_early_index: index + non_lifetime_count,
547544
abstract_type_parent: true,
548545
track_lifetime_uses,
549546
s: ROOT_SCOPE,
@@ -708,7 +705,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
708705

709706
let mut elision = None;
710707
let mut lifetimes = FxHashMap::default();
711-
let mut type_count = 0;
708+
let mut non_lifetime_count = 0;
712709
for param in &generics.params {
713710
match param.kind {
714711
GenericParamKind::Lifetime { .. } => {
@@ -725,12 +722,13 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
725722
lifetimes.insert(name, reg);
726723
}
727724
}
728-
GenericParamKind::Type { .. } => {
729-
type_count += 1;
725+
GenericParamKind::Type { .. } |
726+
GenericParamKind::Const { .. } => {
727+
non_lifetime_count += 1;
730728
}
731729
}
732730
}
733-
let next_early_index = index + type_count;
731+
let next_early_index = index + non_lifetime_count;
734732

735733
if let Some(elision_region) = elision {
736734
let scope = Scope::Elision {
@@ -788,23 +786,20 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
788786
let generics = &trait_item.generics;
789787
let mut index = self.next_early_index();
790788
debug!("visit_ty: index = {}", index);
791-
let mut type_count = 0;
792-
let lifetimes = generics
793-
.params
794-
.iter()
795-
.filter_map(|param| match param.kind {
796-
GenericParamKind::Lifetime { .. } => {
797-
Some(Region::early(&self.tcx.hir(), &mut index, param))
798-
}
799-
GenericParamKind::Type { .. } => {
800-
type_count += 1;
801-
None
802-
}
803-
})
804-
.collect();
789+
let mut non_lifetime_count = 0;
790+
let lifetimes = generics.params.iter().filter_map(|param| match param.kind {
791+
GenericParamKind::Lifetime { .. } => {
792+
Some(Region::early(&self.tcx.hir(), &mut index, param))
793+
}
794+
GenericParamKind::Type { .. } |
795+
GenericParamKind::Const { .. } => {
796+
non_lifetime_count += 1;
797+
None
798+
}
799+
}).collect();
805800
let scope = Scope::Binder {
806801
lifetimes,
807-
next_early_index: index + type_count,
802+
next_early_index: index + non_lifetime_count,
808803
s: self.scope,
809804
track_lifetime_uses: true,
810805
abstract_type_parent: true,
@@ -842,24 +837,21 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
842837
Type(ref ty) => {
843838
let generics = &impl_item.generics;
844839
let mut index = self.next_early_index();
845-
let mut next_early_index = index;
840+
let mut non_lifetime_count = 0;
846841
debug!("visit_ty: index = {}", index);
847-
let lifetimes = generics
848-
.params
849-
.iter()
850-
.filter_map(|param| match param.kind {
851-
GenericParamKind::Lifetime { .. } => {
852-
Some(Region::early(&self.tcx.hir(), &mut index, param))
853-
}
854-
GenericParamKind::Type { .. } => {
855-
next_early_index += 1;
856-
None
857-
}
858-
})
859-
.collect();
842+
let lifetimes = generics.params.iter().filter_map(|param| match param.kind {
843+
GenericParamKind::Lifetime { .. } => {
844+
Some(Region::early(&self.tcx.hir(), &mut index, param))
845+
}
846+
GenericParamKind::Const { .. } |
847+
GenericParamKind::Type { .. } => {
848+
non_lifetime_count += 1;
849+
None
850+
}
851+
}).collect();
860852
let scope = Scope::Binder {
861853
lifetimes,
862-
next_early_index,
854+
next_early_index: index + non_lifetime_count,
863855
s: self.scope,
864856
track_lifetime_uses: true,
865857
abstract_type_parent: true,
@@ -874,19 +866,19 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
874866
let mut index = self.next_early_index();
875867
let mut next_early_index = index;
876868
debug!("visit_ty: index = {}", index);
877-
let lifetimes = generics
878-
.params
879-
.iter()
880-
.filter_map(|param| match param.kind {
881-
GenericParamKind::Lifetime { .. } => {
882-
Some(Region::early(&self.tcx.hir(), &mut index, param))
883-
}
884-
GenericParamKind::Type { .. } => {
885-
next_early_index += 1;
886-
None
887-
}
888-
})
889-
.collect();
869+
let lifetimes = generics.params.iter().filter_map(|param| match param.kind {
870+
GenericParamKind::Lifetime { .. } => {
871+
Some(Region::early(&self.tcx.hir(), &mut index, param))
872+
}
873+
GenericParamKind::Type { .. } => {
874+
next_early_index += 1;
875+
None
876+
}
877+
GenericParamKind::Const { .. } => {
878+
next_early_index += 1;
879+
None
880+
}
881+
}).collect();
890882

891883
let scope = Scope::Binder {
892884
lifetimes,
@@ -950,6 +942,10 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
950942
self.visit_ty(&ty);
951943
}
952944
}
945+
GenericParamKind::Const { ref ty, .. } => {
946+
walk_list!(self, visit_param_bound, &param.bounds);
947+
self.visit_ty(&ty);
948+
}
953949
}
954950
}
955951
for predicate in &generics.where_clause.predicates {
@@ -1395,6 +1391,10 @@ fn object_lifetime_defaults_for_item(
13951391
Set1::Many => Set1::Many,
13961392
})
13971393
}
1394+
GenericParamKind::Const { .. } => {
1395+
// Generic consts don't impose any constraints.
1396+
None
1397+
}
13981398
})
13991399
.collect()
14001400
}
@@ -1703,25 +1703,22 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
17031703
}
17041704
}
17051705

1706-
let mut type_count = 0;
1707-
let lifetimes = generics
1708-
.params
1709-
.iter()
1710-
.filter_map(|param| match param.kind {
1711-
GenericParamKind::Lifetime { .. } => {
1712-
if self.map.late_bound.contains(&param.id) {
1713-
Some(Region::late(&self.tcx.hir(), param))
1714-
} else {
1715-
Some(Region::early(&self.tcx.hir(), &mut index, param))
1716-
}
1717-
}
1718-
GenericParamKind::Type { .. } => {
1719-
type_count += 1;
1720-
None
1706+
let mut non_lifetime_count = 0;
1707+
let lifetimes = generics.params.iter().filter_map(|param| match param.kind {
1708+
GenericParamKind::Lifetime { .. } => {
1709+
if self.map.late_bound.contains(&param.id) {
1710+
Some(Region::late(&self.tcx.hir(), param))
1711+
} else {
1712+
Some(Region::early(&self.tcx.hir(), &mut index, param))
17211713
}
1722-
})
1723-
.collect();
1724-
let next_early_index = index + type_count;
1714+
}
1715+
GenericParamKind::Type { .. } |
1716+
GenericParamKind::Const { .. } => {
1717+
non_lifetime_count += 1;
1718+
None
1719+
}
1720+
}).collect();
1721+
let next_early_index = index + non_lifetime_count;
17251722

17261723
let scope = Scope::Binder {
17271724
lifetimes,
@@ -2011,6 +2008,9 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
20112008
}
20122009
i += 1;
20132010
}
2011+
GenericArg::Const(ct) => {
2012+
self.visit_anon_const(&ct.value);
2013+
}
20142014
}
20152015
}
20162016

@@ -2768,8 +2768,9 @@ fn insert_late_bound_lifetimes(
27682768
match param.kind {
27692769
hir::GenericParamKind::Lifetime { .. } => { /* fall through */ }
27702770

2771-
// Types are not late-bound.
2772-
hir::GenericParamKind::Type { .. } => continue,
2771+
// Neither types nor consts are late-bound.
2772+
hir::GenericParamKind::Type { .. }
2773+
| hir::GenericParamKind::Const { .. } => continue,
27732774
}
27742775

27752776
let lt_name = hir::LifetimeName::Param(param.name.modern());

src/librustc_lint/builtin.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -907,11 +907,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidNoMangleItems {
907907
for param in &generics.params {
908908
match param.kind {
909909
GenericParamKind::Lifetime { .. } => {}
910-
GenericParamKind::Type { .. } => {
911-
let mut err = cx.struct_span_lint(NO_MANGLE_GENERIC_ITEMS,
912-
it.span,
913-
"functions generic over \
914-
types must be mangled");
910+
GenericParamKind::Type { .. } |
911+
GenericParamKind::Const { .. } => {
912+
let mut err = cx.struct_span_lint(
913+
NO_MANGLE_GENERIC_ITEMS,
914+
it.span,
915+
"functions generic over types or consts must be mangled",
916+
);
915917
err.span_suggestion_short(
916918
no_mangle_attr.span,
917919
"remove this attribute",
@@ -1791,14 +1793,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ExplicitOutlivesRequirements {
17911793

17921794
for param in &generics.params {
17931795
let param_name = match param.kind {
1794-
hir::GenericParamKind::Lifetime { .. } => { continue; },
1796+
hir::GenericParamKind::Lifetime { .. } => continue,
17951797
hir::GenericParamKind::Type { .. } => {
17961798
match param.name {
1797-
hir::ParamName::Fresh(_) => { continue; },
1798-
hir::ParamName::Error => { continue; },
1799-
hir::ParamName::Plain(name) => name.to_string()
1799+
hir::ParamName::Fresh(_) => continue,
1800+
hir::ParamName::Error => continue,
1801+
hir::ParamName::Plain(name) => name.to_string(),
18001802
}
18011803
}
1804+
hir::GenericParamKind::Const { .. } => continue,
18021805
};
18031806
let bound_spans = self.collect_outlives_bound_spans(
18041807
cx, def_id, &param_name, &param.bounds, infer_static

src/librustc_metadata/encoder.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,29 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
13311331
}
13321332
}
13331333

1334+
fn encode_info_for_const_param(&mut self, def_id: DefId) -> Entry<'tcx> {
1335+
debug!("IsolatedEncoder::encode_info_for_const_param({:?})", def_id);
1336+
let tcx = self.tcx;
1337+
Entry {
1338+
kind: EntryKind::Type,
1339+
visibility: self.lazy(&ty::Visibility::Public),
1340+
span: self.lazy(&tcx.def_span(def_id)),
1341+
attributes: LazySeq::empty(),
1342+
children: LazySeq::empty(),
1343+
stability: None,
1344+
deprecation: None,
1345+
1346+
ty: Some(self.encode_item_type(def_id)),
1347+
inherent_impls: LazySeq::empty(),
1348+
variances: LazySeq::empty(),
1349+
generics: None,
1350+
predicates: None,
1351+
predicates_defined_on: None,
1352+
1353+
mir: None,
1354+
}
1355+
}
1356+
13341357
fn encode_info_for_closure(&mut self, def_id: DefId) -> Entry<'tcx> {
13351358
debug!("IsolatedEncoder::encode_info_for_closure({:?})", def_id);
13361359
let tcx = self.tcx;
@@ -1684,6 +1707,11 @@ impl<'a, 'b, 'tcx> IndexBuilder<'a, 'b, 'tcx> {
16841707
let encode_info = IsolatedEncoder::encode_info_for_ty_param;
16851708
self.record(def_id, encode_info, (def_id, has_default));
16861709
}
1710+
hir::GenericParamKind::Const { .. } => {
1711+
let def_id = self.tcx.hir().local_def_id(param.id);
1712+
let encode_info = IsolatedEncoder::encode_info_for_const_param;
1713+
self.record(def_id, encode_info, def_id);
1714+
}
16871715
}
16881716
}
16891717
}

0 commit comments

Comments
 (0)