Skip to content

Prefer ambiguous candidates when deduplicating traits in scope, so ambiguous_glob_imported_traits doesn't depend on import order - #161805

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
LorrensP-2158466:res-fix-ambig-import-trait-lint
Aug 28, 2026
Merged

Prefer ambiguous candidates when deduplicating traits in scope, so ambiguous_glob_imported_traits doesn't depend on import order#161805
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
LorrensP-2158466:res-fix-ambig-import-trait-lint

Conversation

@LorrensP-2158466

Copy link
Copy Markdown
Contributor

Fixes #160742

When inserting candidates for a method pick we deduplicate candidates based on the trait id, this "deleted" traits that where (not) ambiguous:

// depending on the order, the second one is removed
mod prelude {
    pub use crate::expression::IntoSql;
    pub use crate::expression::IntoSql as _;
}

use module::*; // imports some item names IntoSql
use prelude::*; // imports trait `IntoSql` and `IntoSql as _`

This caused the lint ambiguous_glob_imported_traits to not be triggered if the as _ came first, even though both are actually in scope.

We now deduplicate based on (def_id, lint_ambiguous) and later check that if an ambiguous candidate is present in collapse_candidates_to_trait_pick, we mark the pick as lint_ambiguous. (its also easy to reverse the behaviour of this change).

Also added a test with 2 revisions placing the as _ export first or last.

It's the most "clean" way I could come up, hopefully someone more versed in this part of the compiler could tell me how it is done in a better way :).

cc @petrochenkov, since I feel like you know if the lint should be triggered or not in this case.

LLM disclosure: I used a LLM to create a shorter PR title, because i couldn't come up with a short one.

@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. labels Aug 26, 2026
@rustbot

rustbot commented Aug 26, 2026

Copy link
Copy Markdown
Collaborator

r? @khyperia

rustbot has assigned @khyperia.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 75 candidates
  • Random selection from 19 candidates

@LorrensP-2158466
LorrensP-2158466 force-pushed the res-fix-ambig-import-trait-lint branch from 0759927 to 7fb57c7 Compare August 26, 2026 09:58
@petrochenkov petrochenkov self-assigned this Aug 26, 2026
@rust-log-analyzer

This comment has been minimized.

Comment thread tests/ui/imports/ambiguous-trait-in-scope-and-underscore.rs Outdated
@petrochenkov

Copy link
Copy Markdown
Contributor

r=me after addressing #161805 (comment) and fixing tidy.
@rustbot author

you know if the lint should be triggered or not in this case.

The treatment of ambiguous glob sets needs to be reworked in general, and the answer on this question may depend on the results of the rework.

For now let's merge this, this PR makes the lint more conservative and (more importantly) order-independent, we can relax it later if it becomes possible with the glob set rework.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 27, 2026
@rustbot

rustbot commented Aug 27, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

…, where the name is ambiguous, make to always

report the `ambiguous_glob_imported_traits` lint independent of the ordering of exports. Added a test to make sure both ordering trigger the lint.
@LorrensP-2158466
LorrensP-2158466 force-pushed the res-fix-ambig-import-trait-lint branch from 7fb57c7 to d7d2dcd Compare August 27, 2026 14:07
@LorrensP-2158466

LorrensP-2158466 commented Aug 27, 2026

Copy link
Copy Markdown
Contributor Author

Fixed tidy and and simplified the test (generic names, clutter, ...).

The treatment of ambiguous glob sets needs to be reworked in general, and the answer on this question may depend on the results of the rework.

For now let's merge this, this PR makes the lint more conservative and (more importantly) order-independent, we can relax it later if it becomes possible with the glob set rework.

Sounds good to me!

@rustbot ready (can't r=you)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 27, 2026
@petrochenkov

Copy link
Copy Markdown
Contributor

@bors r+

@rust-bors

rust-bors Bot commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

📌 Commit d7d2dcd has been approved by petrochenkov

It is now in the queue for this repository.

@rust-bors rust-bors Bot 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 Aug 27, 2026
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Aug 27, 2026
…ort-trait-lint, r=petrochenkov

Prefer ambiguous candidates when deduplicating traits in scope, so `ambiguous_glob_imported_traits` doesn't depend on import order

Fixes rust-lang#160742

When inserting candidates for a method pick we deduplicate candidates based on the trait id, this "deleted" traits that where (not) ambiguous:
```rust
// depending on the order, the second one is removed
mod prelude {
    pub use crate::expression::IntoSql;
    pub use crate::expression::IntoSql as _;
}

use module::*; // imports some item names IntoSql
use prelude::*; // imports trait `IntoSql` and `IntoSql as _`
```

This caused the lint `ambiguous_glob_imported_traits` to not be triggered if the `as _` came first, even though both are actually in scope.

We now deduplicate based on `(def_id, lint_ambiguous)` and later check that if an ambiguous candidate is present in `collapse_candidates_to_trait_pick`, we mark the pick as `lint_ambiguous`. (its also easy to reverse the behaviour of this change).

Also added a test with 2 revisions placing the `as _` export first or last.

It's the most "clean" way I could come up, hopefully someone more versed in this part of the compiler could tell me how it is done in a better way :).

cc @petrochenkov, since I feel like you know if the lint should be triggered or not in this case.

LLM disclosure: I used a LLM to create a shorter PR title, because i couldn't come up with a short one.
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Aug 27, 2026
…ort-trait-lint, r=petrochenkov

Prefer ambiguous candidates when deduplicating traits in scope, so `ambiguous_glob_imported_traits` doesn't depend on import order

Fixes rust-lang#160742

When inserting candidates for a method pick we deduplicate candidates based on the trait id, this "deleted" traits that where (not) ambiguous:
```rust
// depending on the order, the second one is removed
mod prelude {
    pub use crate::expression::IntoSql;
    pub use crate::expression::IntoSql as _;
}

use module::*; // imports some item names IntoSql
use prelude::*; // imports trait `IntoSql` and `IntoSql as _`
```

This caused the lint `ambiguous_glob_imported_traits` to not be triggered if the `as _` came first, even though both are actually in scope.

We now deduplicate based on `(def_id, lint_ambiguous)` and later check that if an ambiguous candidate is present in `collapse_candidates_to_trait_pick`, we mark the pick as `lint_ambiguous`. (its also easy to reverse the behaviour of this change).

Also added a test with 2 revisions placing the `as _` export first or last.

It's the most "clean" way I could come up, hopefully someone more versed in this part of the compiler could tell me how it is done in a better way :).

cc @petrochenkov, since I feel like you know if the lint should be triggered or not in this case.

LLM disclosure: I used a LLM to create a shorter PR title, because i couldn't come up with a short one.
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Aug 27, 2026
…ort-trait-lint, r=petrochenkov

Prefer ambiguous candidates when deduplicating traits in scope, so `ambiguous_glob_imported_traits` doesn't depend on import order

Fixes rust-lang#160742

When inserting candidates for a method pick we deduplicate candidates based on the trait id, this "deleted" traits that where (not) ambiguous:
```rust
// depending on the order, the second one is removed
mod prelude {
    pub use crate::expression::IntoSql;
    pub use crate::expression::IntoSql as _;
}

use module::*; // imports some item names IntoSql
use prelude::*; // imports trait `IntoSql` and `IntoSql as _`
```

This caused the lint `ambiguous_glob_imported_traits` to not be triggered if the `as _` came first, even though both are actually in scope.

We now deduplicate based on `(def_id, lint_ambiguous)` and later check that if an ambiguous candidate is present in `collapse_candidates_to_trait_pick`, we mark the pick as `lint_ambiguous`. (its also easy to reverse the behaviour of this change).

Also added a test with 2 revisions placing the `as _` export first or last.

It's the most "clean" way I could come up, hopefully someone more versed in this part of the compiler could tell me how it is done in a better way :).

cc @petrochenkov, since I feel like you know if the lint should be triggered or not in this case.

LLM disclosure: I used a LLM to create a shorter PR title, because i couldn't come up with a short one.
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Aug 27, 2026
…ort-trait-lint, r=petrochenkov

Prefer ambiguous candidates when deduplicating traits in scope, so `ambiguous_glob_imported_traits` doesn't depend on import order

Fixes rust-lang#160742

When inserting candidates for a method pick we deduplicate candidates based on the trait id, this "deleted" traits that where (not) ambiguous:
```rust
// depending on the order, the second one is removed
mod prelude {
    pub use crate::expression::IntoSql;
    pub use crate::expression::IntoSql as _;
}

use module::*; // imports some item names IntoSql
use prelude::*; // imports trait `IntoSql` and `IntoSql as _`
```

This caused the lint `ambiguous_glob_imported_traits` to not be triggered if the `as _` came first, even though both are actually in scope.

We now deduplicate based on `(def_id, lint_ambiguous)` and later check that if an ambiguous candidate is present in `collapse_candidates_to_trait_pick`, we mark the pick as `lint_ambiguous`. (its also easy to reverse the behaviour of this change).

Also added a test with 2 revisions placing the `as _` export first or last.

It's the most "clean" way I could come up, hopefully someone more versed in this part of the compiler could tell me how it is done in a better way :).

cc @petrochenkov, since I feel like you know if the lint should be triggered or not in this case.

LLM disclosure: I used a LLM to create a shorter PR title, because i couldn't come up with a short one.
rust-bors Bot pushed a commit that referenced this pull request Aug 28, 2026
Rollup of 14 pull requests

Successful merges:

 - #150075 (Implement clamp_to)
 - #159103 (fix(reborrow): recursive implementation)
 - #160848 (std: avoid aliasing violations when wrapping opaque C types)
 - #161421 (Include startup crt objects on WASI for more outputs)
 - #161805 (Prefer ambiguous candidates when deduplicating traits in scope, so `ambiguous_glob_imported_traits` doesn't depend on import order)
 - #161862 (Put data segment in specified section with link_section on wasm)
 - #161866 (delegation: add tests fixating behavior of delegating to default trait implementations)
 - #161456 (reduce perf impact of scalar size checks)
 - #161666 (Print vendor instructions in `x vendor`)
 - #161730 (Improve type mismatch annotation for lets with block-wrapped initializers)
 - #161828 (Never type after-stabilization cleanup)
 - #161860 (atomicptr.rs test: remove unused import)
 - #161870 (bind to [::1] instead of 127.0.0.1 in documentation examples for v6 UDP methods)
 - #161876 (rustdoc: Correctly handle when a macro generates multiple items in `--generate-macro-expansion`)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Aug 28, 2026
…ort-trait-lint, r=petrochenkov

Prefer ambiguous candidates when deduplicating traits in scope, so `ambiguous_glob_imported_traits` doesn't depend on import order

Fixes rust-lang#160742

When inserting candidates for a method pick we deduplicate candidates based on the trait id, this "deleted" traits that where (not) ambiguous:
```rust
// depending on the order, the second one is removed
mod prelude {
    pub use crate::expression::IntoSql;
    pub use crate::expression::IntoSql as _;
}

use module::*; // imports some item names IntoSql
use prelude::*; // imports trait `IntoSql` and `IntoSql as _`
```

This caused the lint `ambiguous_glob_imported_traits` to not be triggered if the `as _` came first, even though both are actually in scope.

We now deduplicate based on `(def_id, lint_ambiguous)` and later check that if an ambiguous candidate is present in `collapse_candidates_to_trait_pick`, we mark the pick as `lint_ambiguous`. (its also easy to reverse the behaviour of this change).

Also added a test with 2 revisions placing the `as _` export first or last.

It's the most "clean" way I could come up, hopefully someone more versed in this part of the compiler could tell me how it is done in a better way :).

cc @petrochenkov, since I feel like you know if the lint should be triggered or not in this case.

LLM disclosure: I used a LLM to create a shorter PR title, because i couldn't come up with a short one.
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Aug 28, 2026
…ort-trait-lint, r=petrochenkov

Prefer ambiguous candidates when deduplicating traits in scope, so `ambiguous_glob_imported_traits` doesn't depend on import order

Fixes rust-lang#160742

When inserting candidates for a method pick we deduplicate candidates based on the trait id, this "deleted" traits that where (not) ambiguous:
```rust
// depending on the order, the second one is removed
mod prelude {
    pub use crate::expression::IntoSql;
    pub use crate::expression::IntoSql as _;
}

use module::*; // imports some item names IntoSql
use prelude::*; // imports trait `IntoSql` and `IntoSql as _`
```

This caused the lint `ambiguous_glob_imported_traits` to not be triggered if the `as _` came first, even though both are actually in scope.

We now deduplicate based on `(def_id, lint_ambiguous)` and later check that if an ambiguous candidate is present in `collapse_candidates_to_trait_pick`, we mark the pick as `lint_ambiguous`. (its also easy to reverse the behaviour of this change).

Also added a test with 2 revisions placing the `as _` export first or last.

It's the most "clean" way I could come up, hopefully someone more versed in this part of the compiler could tell me how it is done in a better way :).

cc @petrochenkov, since I feel like you know if the lint should be triggered or not in this case.

LLM disclosure: I used a LLM to create a shorter PR title, because i couldn't come up with a short one.
rust-bors Bot pushed a commit that referenced this pull request Aug 28, 2026
…uwer

Rollup of 21 pull requests

Successful merges:

 - #158609 (Update sccache to 0.16.0)
 - #150075 (Implement clamp_to)
 - #159103 (fix(reborrow): recursive implementation)
 - #160562 (add target feature ABI checks for SPARC)
 - #160848 (std: avoid aliasing violations when wrapping opaque C types)
 - #161421 (Include startup crt objects on WASI for more outputs)
 - #161805 (Prefer ambiguous candidates when deduplicating traits in scope, so `ambiguous_glob_imported_traits` doesn't depend on import order)
 - #161862 (Put data segment in specified section with link_section on wasm)
 - #161866 (delegation: add tests fixating behavior of delegating to default trait implementations)
 - #161456 (reduce perf impact of scalar size checks)
 - #161528 (Add regression test to ensure optimal compilation)
 - #161666 (Print vendor instructions in `x vendor`)
 - #161730 (Improve type mismatch annotation for lets with block-wrapped initializers)
 - #161828 (Never type after-stabilization cleanup)
 - #161859 (Do not optimize MIR for comptime ConstFns)
 - #161860 (atomicptr.rs test: remove unused import)
 - #161870 (bind to [::1] instead of 127.0.0.1 in documentation examples for v6 UDP methods)
 - #161876 (rustdoc: Correctly handle when a macro generates multiple items in `--generate-macro-expansion`)
 - #161889 (Add link to ownership section in ptr::read docs)
 - #161890 (rustdoc: some clarifying comments)
 - #161891 (Mark `extern_item_impls` feature as incomplete)

Failed merges:

 - #161702 (Use `drop_guard` in some places in {core,alloc,std})
rust-bors Bot pushed a commit that referenced this pull request Aug 28, 2026
…uwer

Rollup of 21 pull requests

Successful merges:

 - #150075 (Implement clamp_to)
 - #159103 (fix(reborrow): recursive implementation)
 - #160562 (add target feature ABI checks for SPARC)
 - #160848 (std: avoid aliasing violations when wrapping opaque C types)
 - #161421 (Include startup crt objects on WASI for more outputs)
 - #161805 (Prefer ambiguous candidates when deduplicating traits in scope, so `ambiguous_glob_imported_traits` doesn't depend on import order)
 - #161862 (Put data segment in specified section with link_section on wasm)
 - #161866 (delegation: add tests fixating behavior of delegating to default trait implementations)
 - #157218 (Track items behind `cfg_select` in the same way we do for `cfg`)
 - #161456 (reduce perf impact of scalar size checks)
 - #161528 (Add regression test to ensure optimal compilation)
 - #161666 (Print vendor instructions in `x vendor`)
 - #161730 (Improve type mismatch annotation for lets with block-wrapped initializers)
 - #161828 (Never type after-stabilization cleanup)
 - #161859 (Do not optimize MIR for comptime ConstFns)
 - #161860 (atomicptr.rs test: remove unused import)
 - #161870 (bind to [::1] instead of 127.0.0.1 in documentation examples for v6 UDP methods)
 - #161876 (rustdoc: Correctly handle when a macro generates multiple items in `--generate-macro-expansion`)
 - #161889 (Add link to ownership section in ptr::read docs)
 - #161890 (rustdoc: some clarifying comments)
 - #161891 (Mark `extern_item_impls` feature as incomplete)

Failed merges:

 - #161702 (Use `drop_guard` in some places in {core,alloc,std})
@rust-bors
rust-bors Bot merged commit 02712c7 into rust-lang:main Aug 28, 2026
13 checks passed
@rustbot rustbot added this to the 1.100.0 milestone Aug 28, 2026
rust-bors Bot pushed a commit that referenced this pull request Aug 28, 2026
Rollup merge of #161805 - LorrensP-2158466:res-fix-ambig-import-trait-lint, r=petrochenkov

Prefer ambiguous candidates when deduplicating traits in scope, so `ambiguous_glob_imported_traits` doesn't depend on import order

Fixes #160742

When inserting candidates for a method pick we deduplicate candidates based on the trait id, this "deleted" traits that where (not) ambiguous:
```rust
// depending on the order, the second one is removed
mod prelude {
    pub use crate::expression::IntoSql;
    pub use crate::expression::IntoSql as _;
}

use module::*; // imports some item names IntoSql
use prelude::*; // imports trait `IntoSql` and `IntoSql as _`
```

This caused the lint `ambiguous_glob_imported_traits` to not be triggered if the `as _` came first, even though both are actually in scope.

We now deduplicate based on `(def_id, lint_ambiguous)` and later check that if an ambiguous candidate is present in `collapse_candidates_to_trait_pick`, we mark the pick as `lint_ambiguous`. (its also easy to reverse the behaviour of this change).

Also added a test with 2 revisions placing the `as _` export first or last.

It's the most "clean" way I could come up, hopefully someone more versed in this part of the compiler could tell me how it is done in a better way :).

cc @petrochenkov, since I feel like you know if the lint should be triggered or not in this case.

LLM disclosure: I used a LLM to create a shorter PR title, because i couldn't come up with a short one.
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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Order of imports/exports for a trait and a trait as _ matters when it should not.

5 participants