Skip to content

Commit 049d29b

Browse files
committedSep 24, 2020
Unify primitive errors with other intra-link errors
Now that `PrimTy::name()` exists, there's no need to carry around the name of the primitive that failed to resolve. This removes the variants special-casing primitives in favor of `NotResolved`. - Remove `NoPrimitiveImpl` and `NoPrimitiveAssocItem` - Remove hacky `has_primitive` check in `resolution_failure()` - Fixup a couple tests that I forgot to `--bless` before
1 parent 472e52e commit 049d29b

File tree

4 files changed

+47
-43
lines changed

4 files changed

+47
-43
lines changed
 

‎src/librustdoc/passes/collect_intra_doc_links.rs

+14-34
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,6 @@ impl<'a> From<ResolutionFailure<'a>> for ErrorKind<'a> {
6060

6161
#[derive(Debug)]
6262
enum ResolutionFailure<'a> {
63-
/// this is a primitive type without an impls (no associated methods)
64-
/// when will this actually happen?
65-
/// the `Res` is the primitive it resolved to
66-
NoPrimitiveImpl(Res, String),
67-
/// `[u8::not_found]`
68-
/// the `Res` is the primitive it resolved to
69-
NoPrimitiveAssocItem { res: Res, prim_name: &'a str, assoc_item: Symbol },
7063
/// This resolved, but with the wrong namespace.
7164
/// `Namespace` is the expected namespace (as opposed to the actual).
7265
WrongNamespace(Res, Namespace),
@@ -326,8 +319,12 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
326319
})?;
327320

328321
if let Some((path, prim)) = is_primitive(&path_root, TypeNS) {
329-
let impls = primitive_impl(cx, &path)
330-
.ok_or_else(|| ResolutionFailure::NoPrimitiveImpl(prim, path_root.into()))?;
322+
let impls =
323+
primitive_impl(cx, &path).ok_or_else(|| ResolutionFailure::NotResolved {
324+
module_id,
325+
partial_res: Some(prim),
326+
unresolved: item_str.into(),
327+
})?;
331328
for &impl_ in impls {
332329
let link = cx
333330
.tcx
@@ -354,10 +351,10 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
354351
item_name,
355352
ns.descr()
356353
);
357-
return Err(ResolutionFailure::NoPrimitiveAssocItem {
358-
res: prim,
359-
prim_name: path,
360-
assoc_item: item_name,
354+
return Err(ResolutionFailure::NotResolved {
355+
module_id,
356+
partial_res: Some(prim),
357+
unresolved: item_str.into(),
361358
}
362359
.into());
363360
}
@@ -1009,7 +1006,7 @@ impl LinkCollector<'_, '_> {
10091006
suggest_disambiguator(resolved, diag, path_str, dox, sp, &link_range);
10101007
});
10111008
};
1012-
if let Res::PrimTy(ty) = res {
1009+
if let Res::PrimTy(..) = res {
10131010
match disambiguator {
10141011
Some(Disambiguator::Primitive | Disambiguator::Namespace(_)) | None => {
10151012
item.attrs.links.push(ItemLink {
@@ -1489,10 +1486,6 @@ fn resolution_failure(
14891486
link_range: Option<Range<usize>>,
14901487
kinds: SmallVec<[ResolutionFailure<'_>; 3]>,
14911488
) {
1492-
let has_primitive = kinds.iter().any(|err|
1493-
matches!(err, ResolutionFailure::NoPrimitiveAssocItem{..} | ResolutionFailure::NoPrimitiveImpl(_, _))
1494-
);
1495-
14961489
report_diagnostic(
14971490
collector.cx,
14981491
&format!("unresolved link to `{}`", path_str),
@@ -1533,7 +1526,7 @@ fn resolution_failure(
15331526

15341527
let module_id = *module_id;
15351528
// FIXME(jynelson): this might conflict with my `Self` fix in #76467
1536-
// FIXME: use itertools `collect_tuple` instead
1529+
// FIXME: maybe use itertools `collect_tuple` instead?
15371530
fn split(path: &str) -> Option<(&str, &str)> {
15381531
let mut splitter = path.rsplitn(2, "::");
15391532
splitter.next().and_then(|right| splitter.next().map(|left| (left, right)))
@@ -1600,10 +1593,7 @@ fn resolution_failure(
16001593
diagnostic_name = collector.cx.tcx.item_name(def_id).as_str();
16011594
(Some(kind), &*diagnostic_name)
16021595
}
1603-
Res::PrimTy(_) => {
1604-
assert!(has_primitive);
1605-
continue;
1606-
}
1596+
Res::PrimTy(ty) => (None, ty.name_str()),
16071597
_ => unreachable!("only ADTs and primitives are in scope at module level"),
16081598
};
16091599
let path_description = if let Some(kind) = kind {
@@ -1640,7 +1630,7 @@ fn resolution_failure(
16401630
Impl | GlobalAsm => unreachable!("not a path"),
16411631
}
16421632
} else {
1643-
res.descr()
1633+
"associated item"
16441634
};
16451635
let note = format!(
16461636
"the {} `{}` has no {} named `{}`",
@@ -1683,16 +1673,6 @@ fn resolution_failure(
16831673
diag.level = rustc_errors::Level::Bug;
16841674
"all intra doc links should have a parent item".to_owned()
16851675
}
1686-
ResolutionFailure::NoPrimitiveImpl(res, _) => format!(
1687-
"this link partially resolves to {}, which does not have any associated items",
1688-
item(res),
1689-
),
1690-
ResolutionFailure::NoPrimitiveAssocItem { prim_name, assoc_item, .. } => {
1691-
format!(
1692-
"the builtin type `{}` does not have an associated item named `{}`",
1693-
prim_name, assoc_item
1694-
)
1695-
}
16961676
};
16971677
if let Some(span) = sp {
16981678
diag.span_label(span, &note);

‎src/test/rustdoc-ui/intra-link-errors.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,16 @@
5454

5555
/// [u8::not_found]
5656
//~^ ERROR unresolved link
57-
//~| NOTE the builtin type `u8` does not have an associated item named `not_found`
57+
//~| NOTE the builtin type `u8` has no associated item named `not_found`
58+
59+
/// [std::primitive::u8::not_found]
60+
//~^ ERROR unresolved link
61+
//~| NOTE the builtin type `u8` has no associated item named `not_found`
62+
63+
/// [type@Vec::into_iter]
64+
//~^ ERROR unresolved link
65+
//~| HELP to link to the associated function, add parentheses
66+
//~| NOTE this link resolves to the associated function `into_iter`
5867

5968
/// [S!]
6069
//~^ ERROR unresolved link

‎src/test/rustdoc-ui/intra-link-errors.stderr

+22-7
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,34 @@ error: unresolved link to `u8::not_found`
8080
--> $DIR/intra-link-errors.rs:55:6
8181
|
8282
LL | /// [u8::not_found]
83-
| ^^^^^^^^^^^^^ the builtin type `u8` does not have an associated item named `not_found`
83+
| ^^^^^^^^^^^^^ the builtin type `u8` has no associated item named `not_found`
8484

85-
error: unresolved link to `S`
85+
error: unresolved link to `std::primitive::u8::not_found`
8686
--> $DIR/intra-link-errors.rs:59:6
8787
|
88+
LL | /// [std::primitive::u8::not_found]
89+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the builtin type `u8` has no associated item named `not_found`
90+
91+
error: unresolved link to `Vec::into_iter`
92+
--> $DIR/intra-link-errors.rs:63:6
93+
|
94+
LL | /// [type@Vec::into_iter]
95+
| ^^^^^^^^^^^^^^^^^^^
96+
| |
97+
| this link resolves to the associated function `into_iter`, which is not in the type namespace
98+
| help: to link to the associated function, add parentheses: `Vec::into_iter()`
99+
100+
error: unresolved link to `S`
101+
--> $DIR/intra-link-errors.rs:68:6
102+
|
88103
LL | /// [S!]
89104
| ^^
90105
| |
91106
| this link resolves to the struct `S`, which is not in the macro namespace
92107
| help: to link to the struct, prefix with `struct@`: `struct@S`
93108

94109
error: unresolved link to `T::g`
95-
--> $DIR/intra-link-errors.rs:77:6
110+
--> $DIR/intra-link-errors.rs:86:6
96111
|
97112
LL | /// [type@T::g]
98113
| ^^^^^^^^^
@@ -101,13 +116,13 @@ LL | /// [type@T::g]
101116
| help: to link to the associated function, add parentheses: `T::g()`
102117

103118
error: unresolved link to `T::h`
104-
--> $DIR/intra-link-errors.rs:82:6
119+
--> $DIR/intra-link-errors.rs:91:6
105120
|
106121
LL | /// [T::h!]
107122
| ^^^^^ the trait `T` has no macro named `h`
108123

109124
error: unresolved link to `S::h`
110-
--> $DIR/intra-link-errors.rs:69:6
125+
--> $DIR/intra-link-errors.rs:78:6
111126
|
112127
LL | /// [type@S::h]
113128
| ^^^^^^^^^
@@ -116,13 +131,13 @@ LL | /// [type@S::h]
116131
| help: to link to the associated function, add parentheses: `S::h()`
117132

118133
error: unresolved link to `m`
119-
--> $DIR/intra-link-errors.rs:89:6
134+
--> $DIR/intra-link-errors.rs:98:6
120135
|
121136
LL | /// [m()]
122137
| ^^^
123138
| |
124139
| this link resolves to the macro `m`, which is not in the value namespace
125140
| help: to link to the macro, add an exclamation mark: `m!`
126141

127-
error: aborting due to 18 previous errors
142+
error: aborting due to 20 previous errors
128143

‎src/test/rustdoc/intra-link-associated-items.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
/// [`std::collections::BTreeMap::into_iter`]
55
/// [`String::from`] is ambiguous as to which `From` impl
6-
/// [type@Vec::into_iter] uses a disambiguator
6+
/// [Vec::into_iter()] uses a disambiguator
77
// @has 'intra_link_associated_items/fn.foo.html' '//a[@href="https://doc.rust-lang.org/nightly/alloc/collections/btree/map/struct.BTreeMap.html#method.into_iter"]' 'std::collections::BTreeMap::into_iter'
88
// @has 'intra_link_associated_items/fn.foo.html' '//a[@href="https://doc.rust-lang.org/nightly/alloc/string/struct.String.html#method.from"]' 'String::from'
99
// @has 'intra_link_associated_items/fn.foo.html' '//a[@href="https://doc.rust-lang.org/nightly/alloc/vec/struct.Vec.html#method.into_iter"]' 'Vec::into_iter'

0 commit comments

Comments
 (0)
Please sign in to comment.