-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rustdoc: Fix invalid suggestions on ambiguous intra doc links v2 #109104
rustdoc: Fix invalid suggestions on ambiguous intra doc links v2 #109104
Conversation
this seems better than before. you should probably get someone from t-compiler to review the change to |
Let's ask to @oli-obk then if they think it looks ok. |
131633d
to
3cafb6d
Compare
) | ||
fn is_derive_trait_collision<T>(ns: &PerNS<Result<Vec<(Res, T)>, ResolutionFailure<'_>>>) -> bool { | ||
if let (&Ok(ref type_ns), &Ok(ref macro_ns)) = (&ns.type_ns, &ns.macro_ns) { | ||
type_ns.iter().any(|(res, _)| matches!(res, Res::Def(DefKind::Trait, _))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be any
or should it be all
? What happens if there's a three-way conflict between a macro_rules macro, a derive macro, and a trait?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's not an issue since we only want to check if there is a derive macro and a trait. In one case, we run this check if we have two elements which then allow to only return the types, and in the other case, we remove the macros from the candidates being returned.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went ahead and tried to write a test for this one.
It doesn't seem to matter, since if I try to bring a macro_rules macro and a derive macro into scope at once, Rust says no. The difference between any
and all
doesn't seem to matter, because you're usually not going to get more than one match in the same namespace anyway.
error[E0255]: the name `Dev` is defined multiple times
--> $DIR/issue-108653-associated-items-9.rs:9:3
|
LL | use proc_macro_derive::Dev;
| ---------------------- previous import of the macro `Dev` here
...
LL | macro_rules! Dev {
| ^^^^^^^^^^^^^^^^ `Dev` redefined here
|
= note: `Dev` must be defined only once in the macro namespace of this module
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems the function wasn't needed so I removed it completely.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it was needed:
error: `PartialEq` is both a trait and a derive macro
--> library/core/src/macros/mod.rs:65:71
|
65 | /// Asserts that two expressions are not equal to each other (using [`PartialEq`]).
| ^^^^^^^^^ ambiguous link
|
help: to link to the trait, prefix with `trait@`
|
65 | /// Asserts that two expressions are not equal to each other (using [`trait@PartialEq`]).
| ++++++
help: to link to the derive macro, prefix with `derive@`
|
65 | /// Asserts that two expressions are not equal to each other (using [`derive@PartialEq`]).
| +++++++
I'll add a regression test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
None of
- this PR
- the issue
- the other PR
explain what the problem on the impl side is and what is being changed. Please add some information to the main post of this PR.
If regular rustc will never encounter filter_by_name_and_namespace
that return more than one element, it should stay as it is and you could make the filtering function a free function inside rustdoc.
3cafb6d
to
2b9e953
Compare
@oli-obk Thanks to your comment, I realized the change in the compiler was unnecessary and thus completely removed it (and the commit as well). The other thing I changed in the new added commit is in case we have multiple candidates with the same "kind". In such case, it's not possible to disambiguate so the function doesn't emit a warning and we handle the candidates as if we only had one. It seems to be what we had before this PR so I brought back this behaviour. |
2b9e953
to
ac4ee88
Compare
Removed unnecessary variable. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really understand how this is supposed to work ...
//~^ ERROR | ||
pub trait Trait { | ||
type IDENT; | ||
const IDENT: usize; | ||
} | ||
|
||
/// [`Trait2::IDENT`] | ||
//~^ ERROR |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please include the message in the ERROR highlight.
//~^ ERROR | |
pub trait Trait { | |
type IDENT; | |
const IDENT: usize; | |
} | |
/// [`Trait2::IDENT`] | |
//~^ ERROR | |
//~^ ERROR both an associated constant and an associated type | |
pub trait Trait { | |
type IDENT; | |
const IDENT: usize; | |
} | |
/// [`Trait2::IDENT`] | |
//~^ ERROR both an associated function and an associated type |
//~^ ERROR | ||
/// [`Trait::Trait`] | ||
//~^ ERROR |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
error: `u32::MAX` is both an associated constant and a builtin type | ||
--> $DIR/issue-108653-associated-items-6.rs:4:7 | ||
| | ||
LL | /// [`u32::MAX`] | ||
| ^^^^^^^^ ambiguous link | ||
| | ||
note: the lint level is defined here | ||
--> $DIR/issue-108653-associated-items-6.rs:1:9 | ||
| | ||
LL | #![deny(rustdoc::broken_intra_doc_links)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
help: to link to the associated constant, prefix with `const@` | ||
| | ||
LL | /// [`const@u32::MAX`] | ||
| ++++++ | ||
help: to link to the builtin type, prefix with `prim@` | ||
| | ||
LL | /// [`prim@u32::MAX`] | ||
| +++++ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This still doesn't seem right? One of these is a constant in a module, and the other is an associated constant to the u32
primitive type (I think the error has those backwards as well and thinks the one in the module is an associated constant).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah interesting: it considers MAX
as a primitive from the module and as an associated constant of u32
. The naming isn't great though...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, dictionary.com is convinced that it should be spelled "built-in".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well I renamed it "primitive type" so that fixes this issue too. 😆
help: to link to the associated constant, prefix with `const@` | ||
| | ||
LL | /// [`const@u32::MAX`] | ||
| ++++++ | ||
help: to link to the associated type, prefix with `type@` | ||
| | ||
LL | /// [`type@u32::MAX`] | ||
| +++++ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unrelated to this pr, but I wonder if we could include the span of the definition in this suggestion? i.e. point to line 11 here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean adding a "item defined here" note?
@@ -324,14 +335,21 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> { | |||
prim_ty: PrimitiveType, | |||
ns: Namespace, | |||
item_name: Symbol, | |||
) -> Option<(Res, DefId)> { | |||
) -> Vec<(Res, DefId)> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you return a PerNS
here instead to enforce at compile time that there's only one result per namespace?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't get what you mean by "enforce at compile time". We'd need to construct a PerNS
by calling prim_ty.impls(tcx)
which is not a constant method and cannot be one since it makes allocations in simplified_types
. Did you have something else in mind?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this function going to return a single item per namespace? I can figure you'd have multiple items in the same namespace, but different disambiguators:
#![allow(nonstandard_style)]
/// [x](method@Foo::data)
pub struct Foo;
pub trait Bar {
fn data(&self) {}
}
impl Bar for Foo {
fn data(&self) {}
}
pub trait Baz {
const data: u32;
}
impl Baz for Foo {
const data: u32 = 0;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's only for primitive types. So normally, this method should never return more than one item. It's possible to create duplicates with trait impls on primitive though, hence why I did this change.
if !items.is_empty() { | ||
items | ||
} else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if !items.is_empty() { | |
items | |
} else { | |
// Inherent associated items take precedence over items that come from trait impls. | |
if !items.is_empty() { | |
items | |
} else { |
// FIXME(#74563): warn about ambiguity | ||
debug!("the candidates were {:?}", candidates.clone().collect::<Vec<_>>()); | ||
candidates.next() | ||
debug!("the candidates were {:?}", candidates); | ||
candidates |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this behaves as expected when there are multiple traits with the same associated item. Consider:
//! [usize::Item]
pub trait Foo {
type Item;
}
pub trait Bar {
type Item;
}
impl Foo for usize { type Item = u32; }
impl Bar for usize { type Item = i32; }
Today that compiles without warning; after your change it gives an ambiguity error. That's correct, but we don't have a way to let users disambiguate it today (#74563), so I'm not sure the warning makes sense to emit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was compiling without warning with this version too. I added a UI test with this code in tests/rustdoc-ui/intra-doc/issue-108653-associated-items-9.rs
just in case.
.. | ||
} | ||
) | ||
fn is_derive_trait_collision<T>(ns: &PerNS<Result<Vec<(Res, T)>, ResolutionFailure<'_>>>) -> bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change doesn't make sense to me. Why are we considering multiple items in the same namespace? At that point it's already ambiguous and the result of this function doesn't matter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems the function wasn't needed so I removed it completely.
if candidates.len() > 1 && !ambiguity_error(self.cx, &diag, &key.path_str, &candidates) { | ||
candidates = vec![candidates[0]]; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand what's going on here. Why are you arbitrarily picking a candidate here? This happens too late, you can no longer be sure that all the candidates came from trait selection instead of from somewhere where you need to report the ambiguity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It allows to fix the following case (using the code example you provided):
//! [usize::Item]
pub trait Foo { type Item; }
pub trait Bar { type Item; }
impl Foo for usize { type Item = u32; }
impl Bar for usize { type Item = i32; }
Without the deduplication logic present in ambiguity_error
, it would output:
error: `usize::Item` is both an associated type and an associated type
--> tests/rustdoc-ui/intra-doc/issue-108653-associated-items-9.rs:5:6
|
5 | //! [usize::Item]
| ^^^^^^^^^^^ ambiguous link
|
note: the lint level is defined here
--> tests/rustdoc-ui/intra-doc/issue-108653-associated-items-9.rs:3:9
|
3 | #![deny(warnings)]
| ^^^^^^^^
= note: `#[deny(rustdoc::broken_intra_doc_links)]` implied by `#[deny(warnings)]`
help: to link to the associated type, prefix with `type@`
|
5 | //! [type@usize::Item]
| +++++
help: to link to the associated type, prefix with `type@`
|
5 | //! [type@usize::Item]
| +++++
error: aborting due to previous error
I'll add a comment to explain why this is needed.
☔ The latest upstream changes (presumably #109496) made this pull request unmergeable. Please resolve the merge conflicts. |
ac4ee88
to
537fdbd
Compare
Applied suggestions where possible and added a new UI test to prevent regression. |
I don't have time for reviews right now. Happy for @notriddle or @oli-obk to be in charge here. |
Thanks a lot for your input on this! Let's set @notriddle then. r? @notriddle |
Could not assign reviewer from: |
So |
@oli-obk Now that this PR has no changes to rustc in it, do the rest of the changes look reasonable to you? |
I only looked at filter_assoc_items_by_name_and_namespace and its uses, but those seemed fine to me |
@bors r=oli-obk,notriddle |
…llaumeGomez Rollup of 7 pull requests Successful merges: - rust-lang#109104 (rustdoc: Fix invalid suggestions on ambiguous intra doc links v2) - rust-lang#109443 (Move `doc(primitive)` future incompat warning to `invalid_doc_attributes`) - rust-lang#109680 (Fix subslice capture in closure) - rust-lang#109798 (fluent_messages macro: don't emit the OS error in a note) - rust-lang#109805 (Source map cleanups) - rust-lang#109818 (rustdoc: Add GUI test for jump to collapsed item) - rust-lang#109820 (rustdoc-search: update docs for comma in `?` help popover) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
Fixes #108653.
This is another approach to fixing the same issue. This time, we keep the computed information around instead of re-computing it.
Strangely enough, the order for ambiguities seem to have been changed. Not an issue but it creates a lot of diff...
So which version do you prefer?
r? @notriddle