Skip to content

Commit f457b3d

Browse files
committed
Refactor generic parameters in rustdoc/clean
1 parent 80b381e commit f457b3d

File tree

21 files changed

+296
-367
lines changed

21 files changed

+296
-367
lines changed

src/librustc/hir/intravisit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -654,8 +654,8 @@ pub fn walk_path_segment<'v, V: Visitor<'v>>(visitor: &mut V,
654654
}
655655

656656
pub fn walk_generic_args<'v, V: Visitor<'v>>(visitor: &mut V,
657-
_path_span: Span,
658-
generic_args: &'v GenericArgs) {
657+
_path_span: Span,
658+
generic_args: &'v GenericArgs) {
659659
walk_list!(visitor, visit_generic_arg, &generic_args.args);
660660
walk_list!(visitor, visit_assoc_type_binding, &generic_args.bindings);
661661
}

src/librustc/hir/lowering.rs

Lines changed: 37 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -382,17 +382,9 @@ impl<'a> LoweringContext<'a> {
382382
let item_lifetimes = match self.lctx.items.get(&item.id).unwrap().node {
383383
hir::Item_::ItemImpl(_, _, _, ref generics, ..)
384384
| hir::Item_::ItemTrait(_, _, ref generics, ..) => {
385-
generics.params
386-
.iter()
387-
.filter_map(|param| match param.kind {
388-
hir::GenericParamKind::Lifetime { .. } => {
389-
Some(param.clone())
390-
}
391-
_ => None,
392-
})
393-
.collect::<Vec<_>>()
385+
generics.params.clone()
394386
}
395-
_ => Vec::new(),
387+
_ => HirVec::new(),
396388
};
397389

398390
self.lctx.with_parent_impl_lifetime_defs(&item_lifetimes, |this| {
@@ -766,16 +758,15 @@ impl<'a> LoweringContext<'a> {
766758
// This is used to track which lifetimes have already been defined, and
767759
// which are new in-band lifetimes that need to have a definition created
768760
// for them.
769-
fn with_in_scope_lifetime_defs<'l, T, F>(
770-
&mut self,
771-
params: impl Iterator<Item = &'l GenericParamAST>,
772-
f: F,
773-
) -> T
761+
fn with_in_scope_lifetime_defs<T, F>(&mut self, params: &Vec<GenericParamAST>, f: F) -> T
774762
where
775763
F: FnOnce(&mut LoweringContext) -> T,
776764
{
777765
let old_len = self.in_scope_lifetimes.len();
778-
let lt_def_names = params.map(|param| param.ident.name);
766+
let lt_def_names = params.iter().filter_map(|param| match param.kind {
767+
GenericParamKindAST::Lifetime { .. } => Some(param.ident.name),
768+
_ => None,
769+
});
779770
self.in_scope_lifetimes.extend(lt_def_names);
780771

781772
let res = f(self);
@@ -789,12 +780,17 @@ impl<'a> LoweringContext<'a> {
789780
// This should only be used with generics that have already had their
790781
// in-band lifetimes added. In practice, this means that this function is
791782
// only used when lowering a child item of a trait or impl.
792-
fn with_parent_impl_lifetime_defs<T, F>(&mut self, params: &[hir::GenericParam], f: F) -> T
793-
where
783+
fn with_parent_impl_lifetime_defs<T, F>(&mut self,
784+
params: &HirVec<hir::GenericParam>,
785+
f: F
786+
) -> T where
794787
F: FnOnce(&mut LoweringContext) -> T,
795788
{
796789
let old_len = self.in_scope_lifetimes.len();
797-
let lt_def_names = params.iter().map(|param| param.name());
790+
let lt_def_names = params.iter().filter_map(|param| match param.kind {
791+
hir::GenericParamKind::Lifetime { .. } => Some(param.name()),
792+
_ => None,
793+
});
798794
self.in_scope_lifetimes.extend(lt_def_names);
799795

800796
let res = f(self);
@@ -820,10 +816,7 @@ impl<'a> LoweringContext<'a> {
820816
F: FnOnce(&mut LoweringContext) -> T,
821817
{
822818
let (in_band_defs, (mut lowered_generics, res)) = self.with_in_scope_lifetime_defs(
823-
generics.params.iter().filter_map(|param| match param.kind {
824-
GenericParamKindAST::Lifetime { .. } => Some(param),
825-
_ => None,
826-
}),
819+
&generics.params,
827820
|this| {
828821
let itctx = ImplTraitContext::Universal(parent_id);
829822
this.collect_in_band_defs(parent_id, anonymous_lifetime_mode, |this| {
@@ -1051,16 +1044,12 @@ impl<'a> LoweringContext<'a> {
10511044
}
10521045

10531046
fn lower_generic_arg(&mut self,
1054-
p: &ast::GenericArgAST,
1047+
arg: &ast::GenericArgAST,
10551048
itctx: ImplTraitContext)
10561049
-> hir::GenericArg {
1057-
match p {
1058-
ast::GenericArgAST::Lifetime(lt) => {
1059-
GenericArg::Lifetime(self.lower_lifetime(&lt))
1060-
}
1061-
ast::GenericArgAST::Type(ty) => {
1062-
GenericArg::Type(self.lower_ty(&ty, itctx))
1063-
}
1050+
match arg {
1051+
ast::GenericArgAST::Lifetime(lt) => GenericArg::Lifetime(self.lower_lifetime(&lt)),
1052+
ast::GenericArgAST::Type(ty) => GenericArg::Type(self.lower_ty(&ty, itctx)),
10641053
}
10651054
}
10661055

@@ -1079,10 +1068,7 @@ impl<'a> LoweringContext<'a> {
10791068
hir::TyRptr(lifetime, self.lower_mt(mt, itctx))
10801069
}
10811070
TyKind::BareFn(ref f) => self.with_in_scope_lifetime_defs(
1082-
f.generic_params.iter().filter_map(|param| match param.kind {
1083-
GenericParamKindAST::Lifetime { .. } => Some(param),
1084-
_ => None,
1085-
}),
1071+
&f.generic_params,
10861072
|this| {
10871073
this.with_anonymous_lifetime_mode(
10881074
AnonymousLifetimeMode::PassThrough,
@@ -1946,6 +1932,15 @@ impl<'a> LoweringContext<'a> {
19461932
}
19471933
}
19481934

1935+
fn lower_generic_params(
1936+
&mut self,
1937+
params: &Vec<GenericParamAST>,
1938+
add_bounds: &NodeMap<Vec<TyParamBound>>,
1939+
itctx: ImplTraitContext,
1940+
) -> hir::HirVec<hir::GenericParam> {
1941+
params.iter().map(|param| self.lower_generic_param(param, add_bounds, itctx)).collect()
1942+
}
1943+
19491944
fn lower_generic_param(&mut self,
19501945
param: &GenericParamAST,
19511946
add_bounds: &NodeMap<Vec<TyParamBound>>,
@@ -1986,10 +1981,9 @@ impl<'a> LoweringContext<'a> {
19861981
let mut bounds = self.lower_bounds(bounds, itctx);
19871982
let add_bounds = add_bounds.get(&param.id).map_or(&[][..], |x| &x);
19881983
if !add_bounds.is_empty() {
1989-
bounds = bounds
1990-
.into_iter()
1991-
.chain(self.lower_bounds(add_bounds, itctx).into_iter())
1992-
.collect();
1984+
bounds = bounds.into_iter()
1985+
.chain(self.lower_bounds(add_bounds, itctx).into_iter())
1986+
.collect();
19931987
}
19941988

19951989
hir::GenericParam {
@@ -2005,23 +1999,14 @@ impl<'a> LoweringContext<'a> {
20051999
synthetic: param.attrs.iter()
20062000
.filter(|attr| attr.check_name("rustc_synthetic"))
20072001
.map(|_| hir::SyntheticTyParamKind::ImplTrait)
2008-
.nth(0),
2002+
.next(),
20092003
attrs: self.lower_attrs(&param.attrs),
20102004
}
20112005
}
20122006
}
20132007
}
20142008
}
20152009

2016-
fn lower_generic_params(
2017-
&mut self,
2018-
params: &Vec<GenericParamAST>,
2019-
add_bounds: &NodeMap<Vec<TyParamBound>>,
2020-
itctx: ImplTraitContext,
2021-
) -> hir::HirVec<hir::GenericParam> {
2022-
params.iter().map(|param| self.lower_generic_param(param, add_bounds, itctx)).collect()
2023-
}
2024-
20252010
fn lower_generics(
20262011
&mut self,
20272012
generics: &Generics,
@@ -2107,10 +2092,7 @@ impl<'a> LoweringContext<'a> {
21072092
span,
21082093
}) => {
21092094
self.with_in_scope_lifetime_defs(
2110-
bound_generic_params.iter().filter_map(|param| match param.kind {
2111-
GenericParamKindAST::Lifetime { .. } => Some(param),
2112-
_ => None,
2113-
}),
2095+
&bound_generic_params,
21142096
|this| {
21152097
hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate {
21162098
bound_generic_params: this.lower_generic_params(
@@ -2203,13 +2185,7 @@ impl<'a> LoweringContext<'a> {
22032185
let bound_generic_params =
22042186
self.lower_generic_params(&p.bound_generic_params, &NodeMap(), itctx);
22052187
let trait_ref = self.with_parent_impl_lifetime_defs(
2206-
&bound_generic_params
2207-
.iter()
2208-
.filter_map(|param| match param.kind {
2209-
hir::GenericParamKind::Lifetime { .. } => Some(param.clone()),
2210-
_ => None,
2211-
})
2212-
.collect::<Vec<_>>(),
2188+
&bound_generic_params,
22132189
|this| this.lower_trait_ref(&p.trait_ref, itctx),
22142190
);
22152191

@@ -2426,10 +2402,7 @@ impl<'a> LoweringContext<'a> {
24262402
);
24272403

24282404
let new_impl_items = self.with_in_scope_lifetime_defs(
2429-
ast_generics.params.iter().filter_map(|param| match param.kind {
2430-
GenericParamKindAST::Lifetime { .. } => Some(param),
2431-
_ => None,
2432-
}),
2405+
&ast_generics.params,
24332406
|this| {
24342407
impl_items
24352408
.iter()

src/librustc/hir/map/def_collector.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
176176
GenericParamKindAST::Lifetime { .. } => DefPathData::LifetimeParam(name),
177177
GenericParamKindAST::Type { .. } => DefPathData::TypeParam(name),
178178
};
179-
self.create_def(
180-
param.id,
181-
def_path_data,
182-
REGULAR_SPACE,
183-
param.ident.span
184-
);
179+
self.create_def(param.id, def_path_data, REGULAR_SPACE, param.ident.span);
185180

186181
visit::walk_generic_param(self, param);
187182
}

src/librustc/hir/map/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -974,11 +974,9 @@ impl<'hir> Map<'hir> {
974974
Some(NodeField(ref f)) => Some(&f.attrs[..]),
975975
Some(NodeExpr(ref e)) => Some(&*e.attrs),
976976
Some(NodeStmt(ref s)) => Some(s.node.attrs()),
977-
Some(NodeGenericParam(param)) => {
978-
match param.kind {
979-
GenericParamKind::Type { ref attrs, .. } => Some(&attrs[..]),
980-
_ => bug!("unexpected non-type NodeGenericParam")
981-
}
977+
Some(NodeGenericParam(param)) => match param.kind {
978+
GenericParamKind::Type { ref attrs, .. } => Some(&attrs[..]),
979+
_ => bug!("unexpected non-type NodeGenericParam")
982980
}
983981
// unit/tuple structs take the attributes straight from
984982
// the struct definition.

src/librustc/hir/print.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,13 +1270,10 @@ impl<'a> State<'a> {
12701270
self.print_name(segment.name)?;
12711271

12721272
segment.with_generic_args(|generic_args| {
1273-
if !generic_args.args.is_empty() ||
1274-
!generic_args.bindings.is_empty()
1275-
{
1276-
self.print_generic_args(&generic_args, segment.infer_types, true)
1277-
} else {
1278-
Ok(())
1273+
if !generic_args.args.is_empty() || !generic_args.bindings.is_empty() {
1274+
return self.print_generic_args(&generic_args, segment.infer_types, true);
12791275
}
1276+
Ok(())
12801277
})?;
12811278
self.print_call_post(base_args)
12821279
}
@@ -1642,8 +1639,7 @@ impl<'a> State<'a> {
16421639
segment.name != keywords::DollarCrate.name() {
16431640
self.print_name(segment.name)?;
16441641
segment.with_generic_args(|generic_args| {
1645-
self.print_generic_args(generic_args,
1646-
segment.infer_types,
1642+
self.print_generic_args(generic_args, segment.infer_types,
16471643
colons_before_params)
16481644
})?;
16491645
}

src/librustc/infer/error_reporting/mod.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,29 +1036,27 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
10361036
// Get the `hir::TyParam` to verify whether it already has any bounds.
10371037
// We do this to avoid suggesting code that ends up as `T: 'a'b`,
10381038
// instead we suggest `T: 'a + 'b` in that case.
1039-
let has_lifetimes =
1040-
if let hir_map::NodeGenericParam(ref param) = hir.get(id) {
1039+
let mut has_bounds = false;
1040+
if let hir_map::NodeGenericParam(ref param) = hir.get(id) {
10411041
match param.kind {
10421042
GenericParamKind::Type { ref bounds, .. } => {
1043-
!bounds.is_empty()
1043+
has_bounds = !bounds.is_empty();
10441044
}
10451045
_ => bug!("unexpected non-type NodeGenericParam"),
10461046
}
1047-
} else {
1048-
false
1049-
};
1047+
}
10501048
let sp = hir.span(id);
10511049
// `sp` only covers `T`, change it so that it covers
10521050
// `T:` when appropriate
1053-
let sp = if has_lifetimes {
1051+
let sp = if has_bounds {
10541052
sp.to(self.tcx
10551053
.sess
10561054
.codemap()
10571055
.next_point(self.tcx.sess.codemap().next_point(sp)))
10581056
} else {
10591057
sp
10601058
};
1061-
(sp, has_lifetimes)
1059+
(sp, has_bounds)
10621060
})
10631061
} else {
10641062
None

0 commit comments

Comments
 (0)