Add new tests and fix old ones
This commit is contained in:
parent
3e96ac1178
commit
6b830ec23e
@ -105,16 +105,19 @@ pub fn get_auto_trait_impls<F>(
|
||||
let generics = self.cx.tcx.generics_of(def_id);
|
||||
|
||||
let ty = self.cx.tcx.type_of(def_id);
|
||||
let mut traits = FxHashMap();
|
||||
let mut traits = Vec::new();
|
||||
if self.cx.crate_name != Some("core".to_string()) {
|
||||
if let ty::TyAdt(_adt, _) = ty.sty {
|
||||
let real_name = name.clone().map(|name| Ident::from_str(&name));
|
||||
let param_env = self.cx.tcx.param_env(def_id);
|
||||
for &trait_def_id in self.cx.all_traits.iter() {
|
||||
if traits.get(&trait_def_id).is_some() ||
|
||||
!self.cx.access_levels.borrow().is_doc_reachable(trait_def_id) {
|
||||
if !self.cx.access_levels.borrow().is_doc_reachable(trait_def_id) ||
|
||||
self.cx.generated_synthetics
|
||||
.borrow_mut()
|
||||
.get(&(def_id, trait_def_id))
|
||||
.is_some() {
|
||||
continue
|
||||
}
|
||||
let t_name = self.cx.tcx.item_name(trait_def_id).to_string();
|
||||
self.cx.tcx.for_each_relevant_impl(trait_def_id, ty, |impl_def_id| {
|
||||
self.cx.tcx.infer_ctxt().enter(|infcx| {
|
||||
let generics = infcx.tcx.generics_of(impl_def_id);
|
||||
@ -124,7 +127,7 @@ pub fn get_auto_trait_impls<F>(
|
||||
::rustc::ty::TypeVariants::TyParam(_) => true,
|
||||
_ => false,
|
||||
} {
|
||||
return;
|
||||
return
|
||||
}
|
||||
|
||||
let substs = infcx.fresh_substs_for_item(DUMMY_SP, def_id);
|
||||
@ -147,38 +150,63 @@ pub fn get_auto_trait_impls<F>(
|
||||
param_env,
|
||||
trait_ref.to_predicate(),
|
||||
));
|
||||
if may_apply {
|
||||
if traits.get(&trait_def_id).is_none() {
|
||||
let trait_ = hir::TraitRef {
|
||||
path: get_path_for_type(infcx.tcx, trait_def_id, hir::def::Def::Trait),
|
||||
ref_id: ast::DUMMY_NODE_ID,
|
||||
};
|
||||
let provided_trait_methods = infcx.tcx.provided_trait_methods(impl_def_id)
|
||||
.into_iter()
|
||||
.map(|meth| meth.ident.to_string())
|
||||
.collect();
|
||||
traits.insert(trait_def_id, Item {
|
||||
source: Span::empty(),
|
||||
name: None,
|
||||
attrs: Default::default(),
|
||||
visibility: None,
|
||||
def_id: self.next_def_id(impl_def_id.krate),
|
||||
stability: None,
|
||||
deprecation: None,
|
||||
inner: ImplItem(Impl {
|
||||
unsafety: hir::Unsafety::Normal,
|
||||
generics: (generics,
|
||||
&tcx.predicates_of(impl_def_id)).clean(self.cx),
|
||||
provided_trait_methods,
|
||||
trait_: Some(trait_.clean(self.cx)),
|
||||
for_: ty.clean(self.cx),
|
||||
items: infcx.tcx.associated_items(impl_def_id).collect::<Vec<_>>().clean(self.cx),
|
||||
polarity: None,
|
||||
synthetic: true,
|
||||
}),
|
||||
});
|
||||
}
|
||||
if !may_apply {
|
||||
return
|
||||
}
|
||||
self.cx.generated_synthetics.borrow_mut()
|
||||
.insert((def_id, trait_def_id));
|
||||
let trait_ = hir::TraitRef {
|
||||
path: get_path_for_type(infcx.tcx, trait_def_id, hir::def::Def::Trait),
|
||||
ref_id: ast::DUMMY_NODE_ID,
|
||||
};
|
||||
let provided_trait_methods = infcx.tcx.provided_trait_methods(impl_def_id)
|
||||
.into_iter()
|
||||
.map(|meth| meth.ident.to_string())
|
||||
.collect();
|
||||
|
||||
let path = get_path_for_type(self.cx.tcx, def_id, def_ctor);
|
||||
let mut segments = path.segments.into_vec();
|
||||
let last = segments.pop().unwrap();
|
||||
|
||||
segments.push(hir::PathSegment::new(
|
||||
real_name.unwrap_or(last.ident),
|
||||
self.generics_to_path_params(generics.clone()),
|
||||
false,
|
||||
));
|
||||
|
||||
let new_path = hir::Path {
|
||||
span: path.span,
|
||||
def: path.def,
|
||||
segments: HirVec::from_vec(segments),
|
||||
};
|
||||
|
||||
let ty = hir::Ty {
|
||||
id: ast::DUMMY_NODE_ID,
|
||||
node: hir::Ty_::TyPath(hir::QPath::Resolved(None, P(new_path))),
|
||||
span: DUMMY_SP,
|
||||
hir_id: hir::DUMMY_HIR_ID,
|
||||
};
|
||||
|
||||
traits.push(Item {
|
||||
source: Span::empty(),
|
||||
name: None,
|
||||
attrs: Default::default(),
|
||||
visibility: None,
|
||||
def_id: self.next_def_id(impl_def_id.krate),
|
||||
stability: None,
|
||||
deprecation: None,
|
||||
inner: ImplItem(Impl {
|
||||
unsafety: hir::Unsafety::Normal,
|
||||
generics: (generics,
|
||||
&tcx.predicates_of(impl_def_id)).clean(self.cx),
|
||||
provided_trait_methods,
|
||||
trait_: Some(trait_.clean(self.cx)),
|
||||
for_: ty.clean(self.cx),
|
||||
items: infcx.tcx.associated_items(impl_def_id).collect::<Vec<_>>().clean(self.cx),
|
||||
polarity: None,
|
||||
synthetic: true,
|
||||
}),
|
||||
});
|
||||
debug!("{:?} => {}", trait_ref, may_apply);
|
||||
}
|
||||
});
|
||||
@ -209,7 +237,7 @@ pub fn get_auto_trait_impls<F>(
|
||||
def_ctor,
|
||||
tcx.require_lang_item(lang_items::SyncTraitLangItem),
|
||||
).into_iter())
|
||||
.chain(traits.into_iter().map(|(_, v)| v))
|
||||
.chain(traits.into_iter())
|
||||
.collect();
|
||||
|
||||
debug!(
|
||||
|
25
src/test/rustdoc/generic-impl.rs
Normal file
25
src/test/rustdoc/generic-impl.rs
Normal file
@ -0,0 +1,25 @@
|
||||
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#![crate_name = "foo"]
|
||||
|
||||
use std::fmt;
|
||||
|
||||
// @!has foo/struct.Bar.html 'impl<T> ToString for Bar'
|
||||
pub struct Bar;
|
||||
|
||||
// @has foo/struct.Foo.html '//h3[@id="impl-ToString"]//code' 'impl<T> ToString for Foo'
|
||||
pub struct Foo;
|
||||
|
||||
impl fmt::Display for Foo {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "Foo")
|
||||
}
|
||||
}
|
@ -56,7 +56,6 @@ fn a_method(&self) -> usize {
|
||||
// @!has - '//*[@class="docblock"]' 'Docs associated with the trait a_method definition.'
|
||||
// @!has - '//*[@class="docblock"]' 'Docs associated with the trait c_method definition.'
|
||||
// @has - '//*[@class="docblock"]' 'Docs associated with the trait b_method definition.'
|
||||
// @!has - '//*[@class="docblock"]' 'Read more'
|
||||
pub struct S2(usize);
|
||||
|
||||
/// Docs associated with the S2 trait implementation.
|
||||
|
@ -31,11 +31,11 @@ fn foo() {}
|
||||
// @has - '//*[@class="sidebar-title"][@href="#fields"]' 'Fields'
|
||||
// @has - '//*[@class="sidebar-links"]/a[@href="#structfield.f"]' 'f'
|
||||
// @has - '//*[@class="sidebar-links"]/a[@href="#structfield.u"]' 'u'
|
||||
// @!has - '//*[@class="sidebar-links"]/a' 'w'
|
||||
// @!has - '//*[@class="sidebar-links"]/a' 'waza'
|
||||
pub struct Bar {
|
||||
pub f: u32,
|
||||
pub u: u32,
|
||||
w: u32,
|
||||
waza: u32,
|
||||
}
|
||||
|
||||
// @has foo/enum.En.html
|
||||
@ -51,9 +51,9 @@ pub enum En {
|
||||
// @has - '//*[@class="sidebar-title"][@href="#fields"]' 'Fields'
|
||||
// @has - '//*[@class="sidebar-links"]/a[@href="#structfield.f1"]' 'f1'
|
||||
// @has - '//*[@class="sidebar-links"]/a[@href="#structfield.f2"]' 'f2'
|
||||
// @!has - '//*[@class="sidebar-links"]/a' 'w'
|
||||
// @!has - '//*[@class="sidebar-links"]/a' 'waza'
|
||||
pub union MyUnion {
|
||||
pub f1: u32,
|
||||
pub f2: f32,
|
||||
w: u32,
|
||||
waza: u32,
|
||||
}
|
||||
|
@ -12,7 +12,7 @@
|
||||
// @has - '//code' 'impl<T> Send for Foo<T> where T: Send'
|
||||
// @has - '//code' 'impl<T> Sync for Foo<T> where T: Sync'
|
||||
// @count - '//*[@id="implementations-list"]/*[@class="impl"]' 0
|
||||
// @count - '//*[@id="synthetic-implementations-list"]/*[@class="impl"]' 2
|
||||
// @count - '//*[@id="synthetic-implementations-list"]/*[@class="impl"]' 11
|
||||
pub struct Foo<T> {
|
||||
field: T,
|
||||
}
|
||||
|
@ -16,7 +16,7 @@
|
||||
// 'impl<T> Send for Foo<T>'
|
||||
//
|
||||
// @count - '//*[@id="implementations-list"]/*[@class="impl"]' 1
|
||||
// @count - '//*[@id="synthetic-implementations-list"]/*[@class="impl"]' 1
|
||||
// @count - '//*[@id="synthetic-implementations-list"]/*[@class="impl"]' 10
|
||||
pub struct Foo<T> {
|
||||
field: T,
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user