Skip to content

Commit 2f02cf8

Browse files
authored
Rollup merge of #103523 - GuillaumeGomez:inline-doc-comment-impl-block, r=notriddle
Fix unwanted merge of inline doc comments for impl blocks Fixes #102909. We need this merge mechanism for inlined items but it's completely unwanted for impl blocks (at least the doc comments are, not the other attributes) since we want to keep what `cfg()` is put on the `pub use` or other attributes. r? ``@notriddle``
2 parents 270e0c5 + a4279a1 commit 2f02cf8

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

src/librustdoc/clean/inline.rs

+35-8
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,39 @@ pub(crate) fn try_inline(
5555
let mut ret = Vec::new();
5656

5757
debug!("attrs={:?}", attrs);
58-
let attrs_clone = attrs;
58+
59+
let attrs_without_docs = attrs.map(|attrs| {
60+
attrs.into_iter().filter(|a| a.doc_str().is_none()).cloned().collect::<Vec<_>>()
61+
});
62+
// We need this ugly code because:
63+
//
64+
// ```
65+
// attrs_without_docs.map(|a| a.as_slice())
66+
// ```
67+
//
68+
// will fail because it returns a temporary slice and:
69+
//
70+
// ```
71+
// attrs_without_docs.map(|s| {
72+
// vec = s.as_slice();
73+
// vec
74+
// })
75+
// ```
76+
//
77+
// will fail because we're moving an uninitialized variable into a closure.
78+
let vec;
79+
let attrs_without_docs = match attrs_without_docs {
80+
Some(s) => {
81+
vec = s;
82+
Some(vec.as_slice())
83+
}
84+
None => None,
85+
};
5986

6087
let kind = match res {
6188
Res::Def(DefKind::Trait, did) => {
6289
record_extern_fqn(cx, did, ItemType::Trait);
63-
build_impls(cx, Some(parent_module), did, attrs, &mut ret);
90+
build_impls(cx, Some(parent_module), did, attrs_without_docs, &mut ret);
6491
clean::TraitItem(Box::new(build_external_trait(cx, did)))
6592
}
6693
Res::Def(DefKind::Fn, did) => {
@@ -69,27 +96,27 @@ pub(crate) fn try_inline(
6996
}
7097
Res::Def(DefKind::Struct, did) => {
7198
record_extern_fqn(cx, did, ItemType::Struct);
72-
build_impls(cx, Some(parent_module), did, attrs, &mut ret);
99+
build_impls(cx, Some(parent_module), did, attrs_without_docs, &mut ret);
73100
clean::StructItem(build_struct(cx, did))
74101
}
75102
Res::Def(DefKind::Union, did) => {
76103
record_extern_fqn(cx, did, ItemType::Union);
77-
build_impls(cx, Some(parent_module), did, attrs, &mut ret);
104+
build_impls(cx, Some(parent_module), did, attrs_without_docs, &mut ret);
78105
clean::UnionItem(build_union(cx, did))
79106
}
80107
Res::Def(DefKind::TyAlias, did) => {
81108
record_extern_fqn(cx, did, ItemType::Typedef);
82-
build_impls(cx, Some(parent_module), did, attrs, &mut ret);
109+
build_impls(cx, Some(parent_module), did, attrs_without_docs, &mut ret);
83110
clean::TypedefItem(build_type_alias(cx, did))
84111
}
85112
Res::Def(DefKind::Enum, did) => {
86113
record_extern_fqn(cx, did, ItemType::Enum);
87-
build_impls(cx, Some(parent_module), did, attrs, &mut ret);
114+
build_impls(cx, Some(parent_module), did, attrs_without_docs, &mut ret);
88115
clean::EnumItem(build_enum(cx, did))
89116
}
90117
Res::Def(DefKind::ForeignTy, did) => {
91118
record_extern_fqn(cx, did, ItemType::ForeignType);
92-
build_impls(cx, Some(parent_module), did, attrs, &mut ret);
119+
build_impls(cx, Some(parent_module), did, attrs_without_docs, &mut ret);
93120
clean::ForeignTypeItem
94121
}
95122
// Never inline enum variants but leave them shown as re-exports.
@@ -123,7 +150,7 @@ pub(crate) fn try_inline(
123150
_ => return None,
124151
};
125152

126-
let (attrs, cfg) = merge_attrs(cx, Some(parent_module), load_attrs(cx, did), attrs_clone);
153+
let (attrs, cfg) = merge_attrs(cx, Some(parent_module), load_attrs(cx, did), attrs);
127154
cx.inlined.insert(did.into());
128155
let mut item = clean::Item::from_def_id_and_attrs_and_parts(
129156
did,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pub struct Foo;
2+
3+
impl Foo {
4+
pub fn foo() {}
5+
}

src/test/rustdoc/reexport-doc.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// aux-build:reexport-doc-aux.rs
2+
3+
extern crate reexport_doc_aux as dep;
4+
5+
// @has 'reexport_doc/struct.Foo.html'
6+
// @count - '//p' 'These are the docs for Foo.' 1
7+
/// These are the docs for Foo.
8+
pub use dep::Foo;

0 commit comments

Comments
 (0)