Skip to content

Commit 82dba3d

Browse files
committed
Refactor hir::GenericParam as a struct
1 parent d643946 commit 82dba3d

File tree

23 files changed

+704
-566
lines changed

23 files changed

+704
-566
lines changed

src/librustc/hir/intravisit.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -743,26 +743,25 @@ pub fn walk_ty_param_bound<'v, V: Visitor<'v>>(visitor: &mut V, bound: &'v TyPar
743743
}
744744

745745
pub fn walk_generic_param<'v, V: Visitor<'v>>(visitor: &mut V, param: &'v GenericParam) {
746-
match *param {
747-
GenericParam::Lifetime(ref ld) => {
748-
visitor.visit_id(ld.lifetime.id);
749-
match ld.lifetime.name {
746+
visitor.visit_id(param.id);
747+
match param.kind {
748+
GenericParamKind::Lifetime { ref bounds, ref lifetime_deprecated, .. } => {
749+
match lifetime_deprecated.name {
750750
LifetimeName::Name(name) => {
751-
visitor.visit_name(ld.lifetime.span, name);
751+
visitor.visit_name(param.span, name);
752752
}
753753
LifetimeName::Fresh(_) |
754754
LifetimeName::Static |
755755
LifetimeName::Implicit |
756756
LifetimeName::Underscore => {}
757757
}
758-
walk_list!(visitor, visit_lifetime, &ld.bounds);
759-
}
760-
GenericParam::Type(ref ty_param) => {
761-
visitor.visit_id(ty_param.id);
762-
visitor.visit_name(ty_param.span, ty_param.name);
763-
walk_list!(visitor, visit_ty_param_bound, &ty_param.bounds);
764-
walk_list!(visitor, visit_ty, &ty_param.default);
765-
walk_list!(visitor, visit_attribute, ty_param.attrs.iter());
758+
walk_list!(visitor, visit_lifetime, bounds);
759+
}
760+
GenericParamKind::Type { name, ref bounds, ref default, ref attrs, .. } => {
761+
visitor.visit_name(param.span, name);
762+
walk_list!(visitor, visit_ty_param_bound, bounds);
763+
walk_list!(visitor, visit_ty, default);
764+
walk_list!(visitor, visit_attribute, attrs.iter());
766765
}
767766
}
768767
}

src/librustc/hir/lowering.rs

Lines changed: 110 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub struct LoweringContext<'a> {
116116
// When traversing a signature such as `fn foo(x: impl Trait)`,
117117
// we record `impl Trait` as a new type parameter, then later
118118
// add it on to `foo`s generics.
119-
in_band_ty_params: Vec<hir::TyParam>,
119+
in_band_ty_params: Vec<hir::GenericParam>,
120120

121121
// Used to create lifetime definitions from in-band lifetime usages.
122122
// e.g. `fn foo(x: &'x u8) -> &'x u8` to `fn foo<'x>(x: &'x u8) -> &'x u8`
@@ -695,22 +695,23 @@ impl<'a> LoweringContext<'a> {
695695
span,
696696
);
697697

698-
hir::GenericParam::Lifetime(hir::LifetimeDef {
699-
lifetime: hir::Lifetime {
700-
id: def_node_id,
701-
span,
702-
name: hir_name,
703-
},
704-
bounds: Vec::new().into(),
698+
hir::GenericParam {
699+
id: def_node_id,
700+
span,
705701
pure_wrt_drop: false,
706-
in_band: true,
707-
})
702+
kind: hir::GenericParamKind::Lifetime {
703+
name: hir_name,
704+
bounds: vec![].into(),
705+
in_band: true,
706+
lifetime_deprecated: hir::Lifetime {
707+
id: def_node_id,
708+
span,
709+
name: hir_name,
710+
}
711+
}
712+
}
708713
})
709-
.chain(
710-
in_band_ty_params
711-
.into_iter()
712-
.map(|tp| hir::GenericParam::Type(tp)),
713-
)
714+
.chain(in_band_ty_params.into_iter())
714715
.collect();
715716

716717
(params, res)
@@ -778,12 +779,12 @@ impl<'a> LoweringContext<'a> {
778779
// This should only be used with generics that have already had their
779780
// in-band lifetimes added. In practice, this means that this function is
780781
// only used when lowering a child item of a trait or impl.
781-
fn with_parent_impl_lifetime_defs<T, F>(&mut self, lt_defs: &[hir::LifetimeDef], f: F) -> T
782+
fn with_parent_impl_lifetime_defs<T, F>(&mut self, params: &[hir::GenericParam], f: F) -> T
782783
where
783784
F: FnOnce(&mut LoweringContext) -> T,
784785
{
785786
let old_len = self.in_scope_lifetimes.len();
786-
let lt_def_names = lt_defs.iter().map(|lt_def| lt_def.lifetime.name.name());
787+
let lt_def_names = params.iter().map(|param| param.name());
787788
self.in_scope_lifetimes.extend(lt_def_names);
788789

789790
let res = f(self);
@@ -1252,15 +1253,17 @@ impl<'a> LoweringContext<'a> {
12521253
let hir_bounds = self.lower_bounds(bounds, itctx);
12531254
// Set the name to `impl Bound1 + Bound2`
12541255
let name = Symbol::intern(&pprust::ty_to_string(t));
1255-
self.in_band_ty_params.push(hir::TyParam {
1256-
name,
1256+
self.in_band_ty_params.push(hir::GenericParam {
12571257
id: def_node_id,
1258-
bounds: hir_bounds,
1259-
default: None,
12601258
span,
12611259
pure_wrt_drop: false,
1262-
synthetic: Some(hir::SyntheticTyParamKind::ImplTrait),
1263-
attrs: P::new(),
1260+
kind: hir::GenericParamKind::Type {
1261+
name,
1262+
bounds: hir_bounds,
1263+
default: None,
1264+
synthetic: Some(hir::SyntheticTyParamKind::ImplTrait),
1265+
attrs: P::new(),
1266+
}
12641267
});
12651268

12661269
hir::TyPath(hir::QPath::Resolved(
@@ -1367,10 +1370,10 @@ impl<'a> LoweringContext<'a> {
13671370

13681371
fn visit_generic_param(&mut self, param: &'v hir::GenericParam) {
13691372
// Record the introduction of 'a in `for<'a> ...`
1370-
if let hir::GenericParam::Lifetime(ref lt_def) = *param {
1373+
if let hir::GenericParamKind::Lifetime { name, .. } = param.kind {
13711374
// Introduce lifetimes one at a time so that we can handle
13721375
// cases like `fn foo<'d>() -> impl for<'a, 'b: 'a, 'c: 'b + 'd>`
1373-
self.currently_bound_lifetimes.push(lt_def.lifetime.name);
1376+
self.currently_bound_lifetimes.push(name);
13741377
}
13751378

13761379
hir::intravisit::walk_generic_param(self, param);
@@ -1416,18 +1419,22 @@ impl<'a> LoweringContext<'a> {
14161419
Mark::root(),
14171420
lifetime.span,
14181421
);
1419-
let def_lifetime = hir::Lifetime {
1422+
1423+
self.output_lifetime_params.push(hir::GenericParam {
14201424
id: def_node_id,
14211425
span: lifetime.span,
1422-
name,
1423-
};
1424-
self.output_lifetime_params
1425-
.push(hir::GenericParam::Lifetime(hir::LifetimeDef {
1426-
lifetime: def_lifetime,
1427-
bounds: Vec::new().into(),
1428-
pure_wrt_drop: false,
1426+
pure_wrt_drop: false,
1427+
kind: hir::GenericParamKind::Lifetime {
1428+
name,
1429+
bounds: vec![].into(),
14291430
in_band: false,
1430-
}));
1431+
lifetime_deprecated: hir::Lifetime {
1432+
id: def_node_id,
1433+
span: lifetime.span,
1434+
name,
1435+
}
1436+
}
1437+
});
14311438
}
14321439
}
14331440
}
@@ -1887,47 +1894,6 @@ impl<'a> LoweringContext<'a> {
18871894
}
18881895
}
18891896

1890-
fn lower_ty_param(
1891-
&mut self,
1892-
tp: &TyParam,
1893-
add_bounds: &[TyParamBound],
1894-
itctx: ImplTraitContext,
1895-
) -> hir::TyParam {
1896-
let mut name = self.lower_ident(tp.ident);
1897-
1898-
// Don't expose `Self` (recovered "keyword used as ident" parse error).
1899-
// `rustc::ty` expects `Self` to be only used for a trait's `Self`.
1900-
// Instead, use gensym("Self") to create a distinct name that looks the same.
1901-
if name == keywords::SelfType.name() {
1902-
name = Symbol::gensym("Self");
1903-
}
1904-
1905-
let mut bounds = self.lower_bounds(&tp.bounds, itctx);
1906-
if !add_bounds.is_empty() {
1907-
bounds = bounds
1908-
.into_iter()
1909-
.chain(self.lower_bounds(add_bounds, itctx).into_iter())
1910-
.collect();
1911-
}
1912-
1913-
hir::TyParam {
1914-
id: self.lower_node_id(tp.id).node_id,
1915-
name,
1916-
bounds,
1917-
default: tp.default
1918-
.as_ref()
1919-
.map(|x| self.lower_ty(x, ImplTraitContext::Disallowed)),
1920-
span: tp.ident.span,
1921-
pure_wrt_drop: attr::contains_name(&tp.attrs, "may_dangle"),
1922-
synthetic: tp.attrs
1923-
.iter()
1924-
.filter(|attr| attr.check_name("rustc_synthetic"))
1925-
.map(|_| hir::SyntheticTyParamKind::ImplTrait)
1926-
.nth(0),
1927-
attrs: self.lower_attrs(&tp.attrs),
1928-
}
1929-
}
1930-
19311897
fn lower_lifetime(&mut self, l: &Lifetime) -> hir::Lifetime {
19321898
let span = l.ident.span;
19331899
match self.lower_ident(l.ident) {
@@ -1962,20 +1928,75 @@ impl<'a> LoweringContext<'a> {
19621928
}
19631929
}
19641930

1965-
fn lower_lifetime_def(&mut self, l: &LifetimeDef) -> hir::LifetimeDef {
1966-
let was_collecting_in_band = self.is_collecting_in_band_lifetimes;
1967-
self.is_collecting_in_band_lifetimes = false;
1931+
fn lower_generic_param(&mut self,
1932+
param: &GenericParamAST,
1933+
add_bounds: &NodeMap<Vec<TyParamBound>>,
1934+
itctx: ImplTraitContext)
1935+
-> hir::GenericParam {
1936+
match param {
1937+
GenericParamAST::Lifetime(ref lifetime_def) => {
1938+
let was_collecting_in_band = self.is_collecting_in_band_lifetimes;
1939+
self.is_collecting_in_band_lifetimes = false;
1940+
1941+
let lifetime = self.lower_lifetime(&lifetime_def.lifetime);
1942+
let param = hir::GenericParam {
1943+
id: lifetime.id,
1944+
span: lifetime.span,
1945+
pure_wrt_drop: attr::contains_name(&lifetime_def.attrs, "may_dangle"),
1946+
kind: hir::GenericParamKind::Lifetime {
1947+
name: lifetime.name,
1948+
bounds: lifetime_def.bounds
1949+
.iter()
1950+
.map(|lt| self.lower_lifetime(lt)).collect(),
1951+
in_band: false,
1952+
lifetime_deprecated: lifetime,
1953+
}
1954+
};
19681955

1969-
let def = hir::LifetimeDef {
1970-
lifetime: self.lower_lifetime(&l.lifetime),
1971-
bounds: l.bounds.iter().map(|l| self.lower_lifetime(l)).collect(),
1972-
pure_wrt_drop: attr::contains_name(&l.attrs, "may_dangle"),
1973-
in_band: false,
1974-
};
1956+
self.is_collecting_in_band_lifetimes = was_collecting_in_band;
19751957

1976-
self.is_collecting_in_band_lifetimes = was_collecting_in_band;
1958+
param
1959+
}
1960+
GenericParamAST::Type(ref ty_param) => {
1961+
let mut name = self.lower_ident(ty_param.ident);
1962+
1963+
// Don't expose `Self` (recovered "keyword used as ident" parse error).
1964+
// `rustc::ty` expects `Self` to be only used for a trait's `Self`.
1965+
// Instead, use gensym("Self") to create a distinct name that looks the same.
1966+
if name == keywords::SelfType.name() {
1967+
name = Symbol::gensym("Self");
1968+
}
1969+
1970+
let mut bounds = self.lower_bounds(&ty_param.bounds, itctx);
1971+
let add_bounds = add_bounds.get(&ty_param.id).map_or(&[][..], |x| &x);
1972+
if !add_bounds.is_empty() {
1973+
bounds = bounds
1974+
.into_iter()
1975+
.chain(self.lower_bounds(add_bounds, itctx).into_iter())
1976+
.collect();
1977+
}
19771978

1978-
def
1979+
hir::GenericParam {
1980+
id: self.lower_node_id(ty_param.id).node_id,
1981+
span: ty_param.ident.span,
1982+
pure_wrt_drop: attr::contains_name(&ty_param.attrs, "may_dangle"),
1983+
kind: hir::GenericParamKind::Type {
1984+
name,
1985+
bounds,
1986+
default: ty_param.default.as_ref()
1987+
.map(|x| {
1988+
self.lower_ty(x, ImplTraitContext::Disallowed)
1989+
}),
1990+
synthetic: ty_param.attrs
1991+
.iter()
1992+
.filter(|attr| attr.check_name("rustc_synthetic"))
1993+
.map(|_| hir::SyntheticTyParamKind::ImplTrait)
1994+
.nth(0),
1995+
attrs: self.lower_attrs(&ty_param.attrs),
1996+
}
1997+
}
1998+
}
1999+
}
19792000
}
19802001

19812002
fn lower_generic_params(
@@ -1984,19 +2005,7 @@ impl<'a> LoweringContext<'a> {
19842005
add_bounds: &NodeMap<Vec<TyParamBound>>,
19852006
itctx: ImplTraitContext,
19862007
) -> hir::HirVec<hir::GenericParam> {
1987-
params
1988-
.iter()
1989-
.map(|param| match *param {
1990-
GenericParamAST::Lifetime(ref lifetime_def) => {
1991-
hir::GenericParam::Lifetime(self.lower_lifetime_def(lifetime_def))
1992-
}
1993-
GenericParamAST::Type(ref ty_param) => hir::GenericParam::Type(self.lower_ty_param(
1994-
ty_param,
1995-
add_bounds.get(&ty_param.id).map_or(&[][..], |x| &x),
1996-
itctx,
1997-
)),
1998-
})
1999-
.collect()
2008+
params.iter().map(|param| self.lower_generic_param(param, add_bounds, itctx)).collect()
20002009
}
20012010

20022011
fn lower_generics(&mut self, g: &Generics, itctx: ImplTraitContext) -> hir::Generics {
@@ -2175,8 +2184,8 @@ impl<'a> LoweringContext<'a> {
21752184
let trait_ref = self.with_parent_impl_lifetime_defs(
21762185
&bound_generic_params
21772186
.iter()
2178-
.filter_map(|p| match *p {
2179-
hir::GenericParam::Lifetime(ref ld) => Some(ld.clone()),
2187+
.filter_map(|param| match param.kind {
2188+
hir::GenericParamKind::Lifetime { .. } => Some(param.clone()),
21802189
_ => None,
21812190
})
21822191
.collect::<Vec<_>>(),

src/librustc/hir/map/collector.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
212212
NodeBlock(n) => EntryBlock(parent, dep_node_index, n),
213213
NodeStructCtor(n) => EntryStructCtor(parent, dep_node_index, n),
214214
NodeLifetime(n) => EntryLifetime(parent, dep_node_index, n),
215-
NodeTyParam(n) => EntryTyParam(parent, dep_node_index, n),
215+
NodeGenericParam(n) => EntryGenericParam(parent, dep_node_index, n),
216216
NodeVisibility(n) => EntryVisibility(parent, dep_node_index, n),
217217
NodeLocal(n) => EntryLocal(parent, dep_node_index, n),
218218
NodeMacroDef(n) => EntryMacroDef(dep_node_index, n),
@@ -347,12 +347,12 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
347347
}
348348

349349
fn visit_generic_param(&mut self, param: &'hir GenericParam) {
350-
match *param {
351-
GenericParam::Lifetime(ref ld) => {
352-
self.insert(ld.lifetime.id, NodeLifetime(&ld.lifetime));
350+
match param.kind {
351+
GenericParamKind::Lifetime { ref lifetime_deprecated, .. } => {
352+
self.insert(param.id, NodeLifetime(lifetime_deprecated));
353353
}
354-
GenericParam::Type(ref ty_param) => {
355-
self.insert(ty_param.id, NodeTyParam(ty_param));
354+
GenericParamKind::Type { .. } => {
355+
self.insert(param.id, NodeGenericParam(param));
356356
}
357357
}
358358
intravisit::walk_generic_param(self, param);

0 commit comments

Comments
 (0)