Skip to content
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

Conversation

GuillaumeGomez
Copy link
Member

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

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Mar 13, 2023
@jyn514
Copy link
Member

jyn514 commented Mar 13, 2023

this seems better than before. you should probably get someone from t-compiler to review the change to AssocItems.

@GuillaumeGomez
Copy link
Member Author

Let's ask to @oli-obk then if they think it looks ok.

@GuillaumeGomez GuillaumeGomez force-pushed the fix-invalid-suggestion-ambiguous-intra-doc2 branch from 131633d to 3cafb6d Compare March 13, 2023 23:05
)
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, _)))
Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Contributor

@notriddle notriddle Mar 13, 2023

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

Copy link
Member Author

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.

Copy link
Member Author

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.

Copy link
Contributor

@oli-obk oli-obk left a 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.

compiler/rustc_middle/src/ty/assoc.rs Outdated Show resolved Hide resolved
@GuillaumeGomez GuillaumeGomez force-pushed the fix-invalid-suggestion-ambiguous-intra-doc2 branch from 3cafb6d to 2b9e953 Compare March 14, 2023 14:06
@GuillaumeGomez
Copy link
Member Author

@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.

@GuillaumeGomez GuillaumeGomez force-pushed the fix-invalid-suggestion-ambiguous-intra-doc2 branch from 2b9e953 to ac4ee88 Compare March 17, 2023 23:23
@GuillaumeGomez
Copy link
Member Author

Removed unnecessary variable.

@notriddle
Copy link
Contributor

@jyn514 @oli-obk does this new version look good for you?

Copy link
Member

@jyn514 jyn514 left a 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 ...

Comment on lines 6 to 13
//~^ ERROR
pub trait Trait {
type IDENT;
const IDENT: usize;
}

/// [`Trait2::IDENT`]
//~^ ERROR
Copy link
Member

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.

Suggested change
//~^ 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

Comment on lines 13 to 15
//~^ ERROR
/// [`Trait::Trait`]
//~^ ERROR
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Comment on lines 1 to 19
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`]
| +++++
Copy link
Member

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).

Copy link
Member Author

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...

Copy link
Contributor

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".

Copy link
Member Author

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. 😆

Comment on lines +12 to +19
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`]
| +++++
Copy link
Member

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.

Copy link
Member Author

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)> {
Copy link
Member

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?

Copy link
Member Author

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?

Copy link
Contributor

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;
}

Copy link
Member Author

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.

Comment on lines 581 to 574
if !items.is_empty() {
items
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if !items.is_empty() {
items
} else {
// Inherent associated items take precedence over items that come from trait impls.
if !items.is_empty() {
items
} else {

Comment on lines 745 to +738
// FIXME(#74563): warn about ambiguity
debug!("the candidates were {:?}", candidates.clone().collect::<Vec<_>>());
candidates.next()
debug!("the candidates were {:?}", candidates);
candidates
Copy link
Member

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.

Copy link
Member Author

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 {
Copy link
Member

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.

Copy link
Member Author

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.

Comment on lines +1261 to +1215
if candidates.len() > 1 && !ambiguity_error(self.cx, &diag, &key.path_str, &candidates) {
candidates = vec![candidates[0]];
}
Copy link
Member

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.

Copy link
Member Author

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.

@bors
Copy link
Contributor

bors commented Mar 22, 2023

☔ The latest upstream changes (presumably #109496) made this pull request unmergeable. Please resolve the merge conflicts.

@GuillaumeGomez GuillaumeGomez force-pushed the fix-invalid-suggestion-ambiguous-intra-doc2 branch from ac4ee88 to 537fdbd Compare March 24, 2023 14:06
@GuillaumeGomez
Copy link
Member Author

Applied suggestions where possible and added a new UI test to prevent regression.

@jyn514
Copy link
Member

jyn514 commented Mar 24, 2023

I don't have time for reviews right now. Happy for @notriddle or @oli-obk to be in charge here.

@GuillaumeGomez
Copy link
Member Author

Thanks a lot for your input on this! Let's set @notriddle then.

r? @notriddle

@rustbot
Copy link
Collaborator

rustbot commented Mar 24, 2023

Could not assign reviewer from: notriddle.
User(s) notriddle are either the PR author or are already assigned, and there are no other candidates.
Use r? to specify someone else to assign.

@GuillaumeGomez
Copy link
Member Author

So is_derive_trait_collision was actually useful.

@notriddle
Copy link
Contributor

@oli-obk Now that this PR has no changes to rustc in it, do the rest of the changes look reasonable to you?

@oli-obk
Copy link
Contributor

oli-obk commented Mar 31, 2023

I only looked at filter_assoc_items_by_name_and_namespace and its uses, but those seemed fine to me

@notriddle
Copy link
Contributor

@bors r=oli-obk,notriddle

@bors
Copy link
Contributor

bors commented Mar 31, 2023

📌 Commit 415a3ca has been approved by oli-obk,notriddle

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 31, 2023
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 31, 2023
…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
@bors
Copy link
Contributor

bors commented Mar 31, 2023

⌛ Testing commit 415a3ca with merge 5e1d329...

@bors bors merged commit 8fe5b56 into rust-lang:master Mar 31, 2023
@rustbot rustbot added this to the 1.70.0 milestone Mar 31, 2023
@GuillaumeGomez GuillaumeGomez deleted the fix-invalid-suggestion-ambiguous-intra-doc2 branch April 1, 2023 08:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[rustdoc] Misleading error message for ambiguous link to associated type/const
6 participants