Skip to content

Commit d64c2ac

Browse files
Improve code
1 parent 6b830ec commit d64c2ac

File tree

5 files changed

+47
-67
lines changed

5 files changed

+47
-67
lines changed

src/librustdoc/clean/auto_trait.rs

Lines changed: 43 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use rustc::hir;
1212
use rustc::traits::{self, auto_trait as auto};
13-
use rustc::ty::{ToPredicate, TypeFoldable};
13+
use rustc::ty::{self, ToPredicate, TypeFoldable};
1414
use rustc::ty::subst::Subst;
1515
use rustc::infer::InferOk;
1616
use std::fmt::Debug;
@@ -80,6 +80,33 @@ impl<'a, 'tcx, 'rcx> AutoTraitFinder<'a, 'tcx, 'rcx> {
8080
self.get_auto_trait_impls(did, &def_ctor, Some(name))
8181
}
8282

83+
fn get_real_ty<F>(&self, def_id: DefId, def_ctor: &F, real_name: &Option<Ident>,
84+
generics: &ty::Generics) -> hir::Ty
85+
where F: Fn(DefId) -> Def {
86+
let path = get_path_for_type(self.cx.tcx, def_id, def_ctor);
87+
let mut segments = path.segments.into_vec();
88+
let last = segments.pop().unwrap();
89+
90+
segments.push(hir::PathSegment::new(
91+
real_name.unwrap_or(last.ident),
92+
self.generics_to_path_params(generics.clone()),
93+
false,
94+
));
95+
96+
let new_path = hir::Path {
97+
span: path.span,
98+
def: path.def,
99+
segments: HirVec::from_vec(segments),
100+
};
101+
102+
hir::Ty {
103+
id: ast::DUMMY_NODE_ID,
104+
node: hir::TyKind::Path(hir::QPath::Resolved(None, P(new_path))),
105+
span: DUMMY_SP,
106+
hir_id: hir::DUMMY_HIR_ID,
107+
}
108+
}
109+
83110
pub fn get_auto_trait_impls<F>(
84111
&self,
85112
def_id: DefId,
@@ -140,7 +167,8 @@ impl<'a, 'tcx, 'rcx> AutoTraitFinder<'a, 'tcx, 'rcx> {
140167
// Require the type the impl is implemented on to match
141168
// our type, and ignore the impl if there was a mismatch.
142169
let cause = traits::ObligationCause::dummy();
143-
let eq_result = infcx.at(&cause, param_env).eq(trait_ref.self_ty(), ty2);
170+
let eq_result = infcx.at(&cause, param_env)
171+
.eq(trait_ref.self_ty(), ty2);
144172
if let Ok(InferOk { value: (), obligations }) = eq_result {
145173
// FIXME(eddyb) ignoring `obligations` might cause false positives.
146174
drop(obligations);
@@ -156,36 +184,18 @@ impl<'a, 'tcx, 'rcx> AutoTraitFinder<'a, 'tcx, 'rcx> {
156184
self.cx.generated_synthetics.borrow_mut()
157185
.insert((def_id, trait_def_id));
158186
let trait_ = hir::TraitRef {
159-
path: get_path_for_type(infcx.tcx, trait_def_id, hir::def::Def::Trait),
187+
path: get_path_for_type(infcx.tcx,
188+
trait_def_id,
189+
hir::def::Def::Trait),
160190
ref_id: ast::DUMMY_NODE_ID,
161191
};
162-
let provided_trait_methods = infcx.tcx.provided_trait_methods(impl_def_id)
163-
.into_iter()
164-
.map(|meth| meth.ident.to_string())
165-
.collect();
166-
167-
let path = get_path_for_type(self.cx.tcx, def_id, def_ctor);
168-
let mut segments = path.segments.into_vec();
169-
let last = segments.pop().unwrap();
170-
171-
segments.push(hir::PathSegment::new(
172-
real_name.unwrap_or(last.ident),
173-
self.generics_to_path_params(generics.clone()),
174-
false,
175-
));
192+
let provided_trait_methods =
193+
infcx.tcx.provided_trait_methods(impl_def_id)
194+
.into_iter()
195+
.map(|meth| meth.ident.to_string())
196+
.collect();
176197

177-
let new_path = hir::Path {
178-
span: path.span,
179-
def: path.def,
180-
segments: HirVec::from_vec(segments),
181-
};
182-
183-
let ty = hir::Ty {
184-
id: ast::DUMMY_NODE_ID,
185-
node: hir::Ty_::TyPath(hir::QPath::Resolved(None, P(new_path))),
186-
span: DUMMY_SP,
187-
hir_id: hir::DUMMY_HIR_ID,
188-
};
198+
let ty = self.get_real_ty(def_id, def_ctor, &real_name, generics);
189199

190200
traits.push(Item {
191201
source: Span::empty(),
@@ -202,7 +212,9 @@ impl<'a, 'tcx, 'rcx> AutoTraitFinder<'a, 'tcx, 'rcx> {
202212
provided_trait_methods,
203213
trait_: Some(trait_.clean(self.cx)),
204214
for_: ty.clean(self.cx),
205-
items: infcx.tcx.associated_items(impl_def_id).collect::<Vec<_>>().clean(self.cx),
215+
items: infcx.tcx.associated_items(impl_def_id)
216+
.collect::<Vec<_>>()
217+
.clean(self.cx),
206218
polarity: None,
207219
synthetic: true,
208220
}),
@@ -312,31 +324,8 @@ impl<'a, 'tcx, 'rcx> AutoTraitFinder<'a, 'tcx, 'rcx> {
312324
}
313325
_ => unreachable!(),
314326
};
315-
316-
let path = get_path_for_type(self.cx.tcx, def_id, def_ctor);
317-
let mut segments = path.segments.into_vec();
318-
let last = segments.pop().unwrap();
319-
320327
let real_name = name.map(|name| Ident::from_str(&name));
321-
322-
segments.push(hir::PathSegment::new(
323-
real_name.unwrap_or(last.ident),
324-
self.generics_to_path_params(generics.clone()),
325-
false,
326-
));
327-
328-
let new_path = hir::Path {
329-
span: path.span,
330-
def: path.def,
331-
segments: HirVec::from_vec(segments),
332-
};
333-
334-
let ty = hir::Ty {
335-
id: ast::DUMMY_NODE_ID,
336-
node: hir::TyKind::Path(hir::QPath::Resolved(None, P(new_path))),
337-
span: DUMMY_SP,
338-
hir_id: hir::DUMMY_HIR_ID,
339-
};
328+
let ty = self.get_real_ty(def_id, def_ctor, &real_name, &generics);
340329

341330
return Some(Item {
342331
source: Span::empty(),

src/librustdoc/html/render.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3628,16 +3628,7 @@ fn render_assoc_items(w: &mut fmt::Formatter,
36283628

36293629
let (synthetic, concrete) = traits
36303630
.iter()
3631-
.partition::<Vec<&&Impl>, _>(|t| t.inner_impl().synthetic);
3632-
3633-
// ugly hacks to remove duplicates.
3634-
let synthetic = synthetic.into_iter()
3635-
.filter(|t| {
3636-
!concrete.iter()
3637-
.any(|tt| {
3638-
tt.inner_impl().trait_.def_id() == t.inner_impl().trait_.def_id()
3639-
})
3640-
}).collect::<Vec<_>>();
3631+
.partition::<Vec<_>, _>(|t| t.inner_impl().synthetic);
36413632

36423633
struct RendererStruct<'a, 'b, 'c>(&'a Context, Vec<&'b &'b Impl>, &'c clean::Item);
36433634

src/test/rustdoc/generic-impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
use std::fmt;
1414

15-
// @!has foo/struct.Bar.html 'impl<T> ToString for Bar'
15+
// @!has foo/struct.Bar.html '//h3[@id="impl-ToString"]//code' 'impl<T> ToString for Bar'
1616
pub struct Bar;
1717

1818
// @has foo/struct.Foo.html '//h3[@id="impl-ToString"]//code' 'impl<T> ToString for Foo'

src/test/rustdoc/synthetic_auto/basic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// @has - '//code' 'impl<T> Send for Foo<T> where T: Send'
1313
// @has - '//code' 'impl<T> Sync for Foo<T> where T: Sync'
1414
// @count - '//*[@id="implementations-list"]/*[@class="impl"]' 0
15-
// @count - '//*[@id="synthetic-implementations-list"]/*[@class="impl"]' 11
15+
// @count - '//*[@id="synthetic-implementations-list"]/*[@class="impl"]' 9
1616
pub struct Foo<T> {
1717
field: T,
1818
}

src/test/rustdoc/synthetic_auto/manual.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// 'impl<T> Send for Foo<T>'
1717
//
1818
// @count - '//*[@id="implementations-list"]/*[@class="impl"]' 1
19-
// @count - '//*[@id="synthetic-implementations-list"]/*[@class="impl"]' 10
19+
// @count - '//*[@id="synthetic-implementations-list"]/*[@class="impl"]' 8
2020
pub struct Foo<T> {
2121
field: T,
2222
}

0 commit comments

Comments
 (0)