Skip to content

Commit 2c6ff24

Browse files
committed
Refactor ast::GenericParam as a struct
1 parent fba1fe2 commit 2c6ff24

File tree

16 files changed

+338
-355
lines changed

16 files changed

+338
-355
lines changed

src/librustc/hir/lowering.rs

Lines changed: 53 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -759,20 +759,20 @@ impl<'a> LoweringContext<'a> {
759759
hir_name
760760
}
761761

762-
// Evaluates `f` with the lifetimes in `lt_defs` in-scope.
762+
// Evaluates `f` with the lifetimes in `params` in-scope.
763763
// This is used to track which lifetimes have already been defined, and
764764
// which are new in-band lifetimes that need to have a definition created
765765
// for them.
766766
fn with_in_scope_lifetime_defs<'l, T, F>(
767767
&mut self,
768-
lt_defs: impl Iterator<Item = &'l LifetimeDef>,
768+
params: impl Iterator<Item = &'l GenericParamAST>,
769769
f: F,
770770
) -> T
771771
where
772772
F: FnOnce(&mut LoweringContext) -> T,
773773
{
774774
let old_len = self.in_scope_lifetimes.len();
775-
let lt_def_names = lt_defs.map(|lt_def| lt_def.lifetime.ident.name);
775+
let lt_def_names = params.map(|param| param.ident.name);
776776
self.in_scope_lifetimes.extend(lt_def_names);
777777

778778
let res = f(self);
@@ -781,8 +781,8 @@ impl<'a> LoweringContext<'a> {
781781
res
782782
}
783783

784-
// Same as the method above, but accepts `hir::LifetimeDef`s
785-
// instead of `ast::LifetimeDef`s.
784+
// Same as the method above, but accepts `hir::GenericParam`s
785+
// instead of `ast::GenericParam`s.
786786
// This should only be used with generics that have already had their
787787
// in-band lifetimes added. In practice, this means that this function is
788788
// only used when lowering a child item of a trait or impl.
@@ -817,8 +817,8 @@ impl<'a> LoweringContext<'a> {
817817
F: FnOnce(&mut LoweringContext) -> T,
818818
{
819819
let (in_band_defs, (mut lowered_generics, res)) = self.with_in_scope_lifetime_defs(
820-
generics.params.iter().filter_map(|p| match p {
821-
GenericParamAST::Lifetime(ld) => Some(ld),
820+
generics.params.iter().filter_map(|param| match param.kind {
821+
GenericParamKindAST::Lifetime { .. } => Some(param),
822822
_ => None,
823823
}),
824824
|this| {
@@ -1076,8 +1076,8 @@ impl<'a> LoweringContext<'a> {
10761076
hir::TyRptr(lifetime, self.lower_mt(mt, itctx))
10771077
}
10781078
TyKind::BareFn(ref f) => self.with_in_scope_lifetime_defs(
1079-
f.generic_params.iter().filter_map(|p| match p {
1080-
GenericParamAST::Lifetime(ld) => Some(ld),
1079+
f.generic_params.iter().filter_map(|param| match param.kind {
1080+
GenericParamKindAST::Lifetime { .. } => Some(param),
10811081
_ => None,
10821082
}),
10831083
|this| {
@@ -1940,21 +1940,19 @@ impl<'a> LoweringContext<'a> {
19401940
add_bounds: &NodeMap<Vec<TyParamBound>>,
19411941
itctx: ImplTraitContext)
19421942
-> hir::GenericParam {
1943-
match param {
1944-
GenericParamAST::Lifetime(ref lifetime_def) => {
1943+
match param.kind {
1944+
GenericParamKindAST::Lifetime { ref bounds, ref lifetime, .. } => {
19451945
let was_collecting_in_band = self.is_collecting_in_band_lifetimes;
19461946
self.is_collecting_in_band_lifetimes = false;
19471947

1948-
let lifetime = self.lower_lifetime(&lifetime_def.lifetime);
1948+
let lifetime = self.lower_lifetime(lifetime);
19491949
let param = hir::GenericParam {
19501950
id: lifetime.id,
19511951
span: lifetime.span,
1952-
pure_wrt_drop: attr::contains_name(&lifetime_def.attrs, "may_dangle"),
1952+
pure_wrt_drop: attr::contains_name(&param.attrs, "may_dangle"),
19531953
kind: hir::GenericParamKind::Lifetime {
19541954
name: lifetime.name,
1955-
bounds: lifetime_def.bounds
1956-
.iter()
1957-
.map(|lt| self.lower_lifetime(lt)).collect(),
1955+
bounds: bounds.iter().map(|lt| self.lower_lifetime(lt)).collect(),
19581956
in_band: false,
19591957
lifetime_deprecated: lifetime,
19601958
}
@@ -1964,8 +1962,8 @@ impl<'a> LoweringContext<'a> {
19641962

19651963
param
19661964
}
1967-
GenericParamAST::Type(ref ty_param) => {
1968-
let mut name = self.lower_ident(ty_param.ident);
1965+
GenericParamKindAST::Type { ref bounds, ref default } => {
1966+
let mut name = self.lower_ident(param.ident);
19691967

19701968
// Don't expose `Self` (recovered "keyword used as ident" parse error).
19711969
// `rustc::ty` expects `Self` to be only used for a trait's `Self`.
@@ -1974,8 +1972,8 @@ impl<'a> LoweringContext<'a> {
19741972
name = Symbol::gensym("Self");
19751973
}
19761974

1977-
let mut bounds = self.lower_bounds(&ty_param.bounds, itctx);
1978-
let add_bounds = add_bounds.get(&ty_param.id).map_or(&[][..], |x| &x);
1975+
let mut bounds = self.lower_bounds(bounds, itctx);
1976+
let add_bounds = add_bounds.get(&param.id).map_or(&[][..], |x| &x);
19791977
if !add_bounds.is_empty() {
19801978
bounds = bounds
19811979
.into_iter()
@@ -1984,22 +1982,20 @@ impl<'a> LoweringContext<'a> {
19841982
}
19851983

19861984
hir::GenericParam {
1987-
id: self.lower_node_id(ty_param.id).node_id,
1988-
span: ty_param.ident.span,
1989-
pure_wrt_drop: attr::contains_name(&ty_param.attrs, "may_dangle"),
1985+
id: self.lower_node_id(param.id).node_id,
1986+
span: param.ident.span,
1987+
pure_wrt_drop: attr::contains_name(&param.attrs, "may_dangle"),
19901988
kind: hir::GenericParamKind::Type {
19911989
name,
19921990
bounds,
1993-
default: ty_param.default.as_ref()
1994-
.map(|x| {
1995-
self.lower_ty(x, ImplTraitContext::Disallowed)
1996-
}),
1997-
synthetic: ty_param.attrs
1998-
.iter()
1999-
.filter(|attr| attr.check_name("rustc_synthetic"))
2000-
.map(|_| hir::SyntheticTyParamKind::ImplTrait)
2001-
.nth(0),
2002-
attrs: self.lower_attrs(&ty_param.attrs),
1991+
default: default.as_ref().map(|x| {
1992+
self.lower_ty(x, ImplTraitContext::Disallowed)
1993+
}),
1994+
synthetic: param.attrs.iter()
1995+
.filter(|attr| attr.check_name("rustc_synthetic"))
1996+
.map(|_| hir::SyntheticTyParamKind::ImplTrait)
1997+
.nth(0),
1998+
attrs: self.lower_attrs(&param.attrs),
20031999
}
20042000
}
20052001
}
@@ -2015,13 +2011,18 @@ impl<'a> LoweringContext<'a> {
20152011
params.iter().map(|param| self.lower_generic_param(param, add_bounds, itctx)).collect()
20162012
}
20172013

2018-
fn lower_generics(&mut self, g: &Generics, itctx: ImplTraitContext) -> hir::Generics {
2014+
fn lower_generics(
2015+
&mut self,
2016+
generics: &Generics,
2017+
itctx: ImplTraitContext)
2018+
-> hir::Generics
2019+
{
20192020
// Collect `?Trait` bounds in where clause and move them to parameter definitions.
20202021
// FIXME: This could probably be done with less rightward drift. Also looks like two control
20212022
// paths where report_error is called are also the only paths that advance to after
20222023
// the match statement, so the error reporting could probably just be moved there.
20232024
let mut add_bounds = NodeMap();
2024-
for pred in &g.where_clause.predicates {
2025+
for pred in &generics.where_clause.predicates {
20252026
if let WherePredicate::BoundPredicate(ref bound_pred) = *pred {
20262027
'next_bound: for bound in &bound_pred.bounds {
20272028
if let TraitTyParamBound(_, TraitBoundModifier::Maybe) = *bound {
@@ -2045,15 +2046,17 @@ impl<'a> LoweringContext<'a> {
20452046
if let Some(node_id) =
20462047
self.resolver.definitions().as_local_node_id(def_id)
20472048
{
2048-
for param in &g.params {
2049-
if let GenericParamAST::Type(ref ty_param) = *param {
2050-
if node_id == ty_param.id {
2051-
add_bounds
2052-
.entry(ty_param.id)
2053-
.or_insert(Vec::new())
2054-
.push(bound.clone());
2055-
continue 'next_bound;
2049+
for param in &generics.params {
2050+
match param.kind {
2051+
GenericParamKindAST::Type { .. } => {
2052+
if node_id == param.id {
2053+
add_bounds.entry(param.id)
2054+
.or_insert(Vec::new())
2055+
.push(bound.clone());
2056+
continue 'next_bound;
2057+
}
20562058
}
2059+
_ => {}
20572060
}
20582061
}
20592062
}
@@ -2068,9 +2071,9 @@ impl<'a> LoweringContext<'a> {
20682071
}
20692072

20702073
hir::Generics {
2071-
params: self.lower_generic_params(&g.params, &add_bounds, itctx),
2072-
where_clause: self.lower_where_clause(&g.where_clause),
2073-
span: g.span,
2074+
params: self.lower_generic_params(&generics.params, &add_bounds, itctx),
2075+
where_clause: self.lower_where_clause(&generics.where_clause),
2076+
span: generics.span,
20742077
}
20752078
}
20762079

@@ -2093,8 +2096,8 @@ impl<'a> LoweringContext<'a> {
20932096
span,
20942097
}) => {
20952098
self.with_in_scope_lifetime_defs(
2096-
bound_generic_params.iter().filter_map(|p| match p {
2097-
GenericParamAST::Lifetime(ld) => Some(ld),
2099+
bound_generic_params.iter().filter_map(|param| match param.kind {
2100+
GenericParamKindAST::Lifetime { .. } => Some(param),
20982101
_ => None,
20992102
}),
21002103
|this| {
@@ -2412,8 +2415,8 @@ impl<'a> LoweringContext<'a> {
24122415
);
24132416

24142417
let new_impl_items = self.with_in_scope_lifetime_defs(
2415-
ast_generics.params.iter().filter_map(|p| match p {
2416-
GenericParamAST::Lifetime(ld) => Some(ld),
2418+
ast_generics.params.iter().filter_map(|param| match param.kind {
2419+
GenericParamKindAST::Lifetime { .. } => Some(param),
24172420
_ => None,
24182421
}),
24192422
|this| {

src/librustc/hir/map/def_collector.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -171,24 +171,17 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
171171
}
172172

173173
fn visit_generic_param(&mut self, param: &'a GenericParamAST) {
174-
match *param {
175-
GenericParamAST::Lifetime(ref lifetime_def) => {
176-
self.create_def(
177-
lifetime_def.lifetime.id,
178-
DefPathData::LifetimeDef(lifetime_def.lifetime.ident.name.as_interned_str()),
179-
REGULAR_SPACE,
180-
lifetime_def.lifetime.ident.span
181-
);
182-
}
183-
GenericParamAST::Type(ref ty_param) => {
184-
self.create_def(
185-
ty_param.id,
186-
DefPathData::TypeParam(ty_param.ident.name.as_interned_str()),
187-
REGULAR_SPACE,
188-
ty_param.ident.span
189-
);
190-
}
191-
}
174+
let name = param.ident.name.as_interned_str();
175+
let def_path_data = match param.kind {
176+
GenericParamKindAST::Lifetime { .. } => DefPathData::LifetimeDef(name),
177+
GenericParamKindAST::Type { .. } => DefPathData::TypeParam(name),
178+
};
179+
self.create_def(
180+
param.id,
181+
def_path_data,
182+
REGULAR_SPACE,
183+
param.ident.span
184+
);
192185

193186
visit::walk_generic_param(self, param);
194187
}

src/librustc_passes/ast_validation.rs

Lines changed: 40 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -139,29 +139,23 @@ impl<'a> AstValidator<'a> {
139139
}
140140

141141
fn check_late_bound_lifetime_defs(&self, params: &Vec<GenericParamAST>) {
142-
// Check: Only lifetime parameters
143-
let non_lifetime_param_spans : Vec<_> = params.iter()
144-
.filter_map(|param| match *param {
145-
GenericParamAST::Lifetime(_) => None,
146-
GenericParamAST::Type(ref t) => Some(t.ident.span),
147-
}).collect();
148-
if !non_lifetime_param_spans.is_empty() {
149-
self.err_handler().span_err(non_lifetime_param_spans,
150-
"only lifetime parameters can be used in this context");
151-
}
152-
153-
// Check: No bounds on lifetime parameters
154-
for param in params.iter() {
155-
match *param {
156-
GenericParamAST::Lifetime(ref l) => {
157-
if !l.bounds.is_empty() {
158-
let spans: Vec<_> = l.bounds.iter().map(|b| b.ident.span).collect();
142+
// Check only lifetime parameters are present and that the lifetime
143+
// parameters that are present have no bounds.
144+
let non_lifetime_param_spans: Vec<_> = params.iter()
145+
.filter_map(|param| match param.kind {
146+
GenericParamKindAST::Lifetime { ref bounds, .. } => {
147+
if !bounds.is_empty() {
148+
let spans: Vec<_> = bounds.iter().map(|b| b.ident.span).collect();
159149
self.err_handler().span_err(spans,
160150
"lifetime bounds cannot be used in this context");
161151
}
152+
None
162153
}
163-
GenericParamAST::Type(_) => {}
164-
}
154+
_ => Some(param.ident.span),
155+
}).collect();
156+
if !non_lifetime_param_spans.is_empty() {
157+
self.err_handler().span_err(non_lifetime_param_spans,
158+
"only lifetime parameters can be used in this context");
165159
}
166160
}
167161
}
@@ -335,22 +329,21 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
335329
}
336330
ItemKind::TraitAlias(Generics { ref params, .. }, ..) => {
337331
for param in params {
338-
if let GenericParamAST::Type(TyParam {
339-
ident,
340-
ref bounds,
341-
ref default,
342-
..
343-
}) = *param
344-
{
345-
if !bounds.is_empty() {
346-
self.err_handler().span_err(ident.span,
347-
"type parameters on the left side of a \
348-
trait alias cannot be bounded");
349-
}
350-
if !default.is_none() {
351-
self.err_handler().span_err(ident.span,
352-
"type parameters on the left side of a \
353-
trait alias cannot have defaults");
332+
match param.kind {
333+
GenericParamKindAST::Lifetime { .. } => {}
334+
GenericParamKindAST::Type { ref bounds, ref default, .. } => {
335+
if !bounds.is_empty() {
336+
self.err_handler().span_err(param.ident.span,
337+
"type parameters on the left side \
338+
of a trait alias cannot be \
339+
bounded");
340+
}
341+
if !default.is_none() {
342+
self.err_handler().span_err(param.ident.span,
343+
"type parameters on the left side \
344+
of a trait alias cannot have \
345+
defaults");
346+
}
354347
}
355348
}
356349
}
@@ -413,24 +406,23 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
413406
let mut seen_non_lifetime_param = false;
414407
let mut seen_default = None;
415408
for param in &g.params {
416-
match (param, seen_non_lifetime_param) {
417-
(&GenericParamAST::Lifetime(ref ld), true) => {
409+
match (&param.kind, seen_non_lifetime_param) {
410+
(GenericParamKindAST::Lifetime { .. }, true) => {
418411
self.err_handler()
419-
.span_err(ld.lifetime.ident.span, "lifetime parameters must be leading");
412+
.span_err(param.ident.span, "lifetime parameters must be leading");
420413
},
421-
(&GenericParamAST::Lifetime(_), false) => {}
422-
_ => {
414+
(GenericParamKindAST::Lifetime { .. }, false) => {}
415+
(GenericParamKindAST::Type { ref default, .. }, _) => {
423416
seen_non_lifetime_param = true;
417+
if default.is_some() {
418+
seen_default = Some(param.ident.span);
419+
} else if let Some(span) = seen_default {
420+
self.err_handler()
421+
.span_err(span, "type parameters with a default must be trailing");
422+
break;
423+
}
424424
}
425425
}
426-
427-
if let GenericParamAST::Type(ref ty_param @ TyParam { default: Some(_), .. }) = *param {
428-
seen_default = Some(ty_param.ident.span);
429-
} else if let Some(span) = seen_default {
430-
self.err_handler()
431-
.span_err(span, "type parameters with a default must be trailing");
432-
break
433-
}
434426
}
435427
for predicate in &g.where_clause.predicates {
436428
if let WherePredicate::EqPredicate(ref predicate) = *predicate {

0 commit comments

Comments
 (0)