Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions compiler/rustc_hir_typeck/src/method/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
})
}

probe::TraitPick(_) => {
probe::TraitPick { .. } => {
let trait_def_id = pick.item.container_id(self.tcx);

// Make a trait reference `$0 : Trait<$1...$n>`
Expand Down Expand Up @@ -756,7 +756,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
pick: &probe::Pick<'_>,
segment: &hir::PathSegment<'tcx>,
) {
if pick.kind != probe::PickKind::TraitPick(true) {
if pick.kind != (probe::PickKind::TraitPick { is_ambiguously_imported: true }) {
return;
}
let trait_name = self.tcx.item_name(pick.item.container_id(self.tcx));
Expand All @@ -767,11 +767,11 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
segment.hir_id,
rustc_errors::DiagDecorator(|diag| {
diag.primary_message(format!(
"Use of ambiguously glob imported trait `{trait_name}`"
"use of ambiguously glob imported trait `{trait_name}`"
))
.span(segment.ident.span)
.span_label(import_span, format!("`{trait_name}` imported ambiguously here"))
.help(format!("Import `{trait_name}` explicitly"));
.help(format!("import `{trait_name}` explicitly"));
}),
);
}
Expand Down
53 changes: 30 additions & 23 deletions compiler/rustc_hir_typeck/src/method/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub(crate) struct Candidate<'tcx> {
pub(crate) enum CandidateKind<'tcx> {
InherentImplCandidate { impl_def_id: DefId, receiver_steps: usize },
ObjectCandidate(ty::PolyTraitRef<'tcx>),
TraitCandidate(ty::PolyTraitRef<'tcx>, bool /* lint_ambiguous */),
TraitCandidate { trait_ref: ty::PolyTraitRef<'tcx>, is_ambiguously_imported: bool },
WhereClauseCandidate(ty::PolyTraitRef<'tcx>),
}

Expand Down Expand Up @@ -239,18 +239,17 @@ pub(crate) struct Pick<'tcx> {
/// Only applies for inherent impls.
pub receiver_steps: Option<usize>,

/// Candidates that were shadowed by supertraits.
/// Candidates that were shadowed by subtraits.
pub shadowed_candidates: Vec<ty::AssocItem>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum PickKind<'tcx> {
InherentImplPick,
ObjectPick,
TraitPick(
// Is Ambiguously Imported
bool,
),
TraitPick {
is_ambiguously_imported: bool,
},
WhereClausePick(
// Trait
ty::PolyTraitRef<'tcx>,
Expand Down Expand Up @@ -611,10 +610,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
Candidate {
item,
kind: match item.container {
AssocContainer::Trait => CandidateKind::TraitCandidate(
ty::Binder::dummy(trait_ref),
false,
),
AssocContainer::Trait => CandidateKind::TraitCandidate {
trait_ref: ty::Binder::dummy(trait_ref),
is_ambiguously_imported: false,
},
AssocContainer::InherentImpl => {
CandidateKind::InherentImplCandidate {
impl_def_id: self.tcx.parent(def_id),
Expand Down Expand Up @@ -1143,7 +1142,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
&mut self,
import_ids: &'tcx [LocalDefId],
trait_def_id: DefId,
lint_ambiguous: bool,
is_ambiguously_imported: bool,
) {
let trait_args = self.fresh_args_for_item(self.span, trait_def_id);
let trait_ref = ty::TraitRef::new_from_args(self.tcx, trait_def_id, trait_args);
Expand All @@ -1165,7 +1164,10 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
Candidate {
item,
import_ids,
kind: TraitCandidate(bound_trait_ref, lint_ambiguous),
kind: TraitCandidate {
trait_ref: bound_trait_ref,
is_ambiguously_imported,
},
},
false,
);
Expand All @@ -1188,7 +1190,10 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
Candidate {
item,
import_ids,
kind: TraitCandidate(ty::Binder::dummy(trait_ref), lint_ambiguous),
kind: TraitCandidate {
trait_ref: ty::Binder::dummy(trait_ref),
is_ambiguously_imported,
},
},
false,
);
Expand Down Expand Up @@ -1958,7 +1963,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
ObjectCandidate(_) | WhereClauseCandidate(_) => {
CandidateSource::Trait(candidate.item.container_id(self.tcx))
}
TraitCandidate(trait_ref, _) => self.probe(|_| {
TraitCandidate { trait_ref, is_ambiguously_imported: _ } => self.probe(|_| {
let trait_ref = self.instantiate_binder_with_fresh_vars(
self.span,
BoundRegionConversionTime::FnCall,
Expand Down Expand Up @@ -1988,7 +1993,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
fn candidate_source_from_pick(&self, pick: &Pick<'tcx>) -> CandidateSource {
match pick.kind {
InherentImplPick => CandidateSource::Impl(pick.item.container_id(self.tcx)),
ObjectPick | WhereClausePick(_) | TraitPick(_) => {
ObjectPick | WhereClausePick(_) | TraitPick { .. } => {
CandidateSource::Trait(pick.item.container_id(self.tcx))
}
}
Expand Down Expand Up @@ -2069,7 +2074,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
impl_bounds,
));
}
TraitCandidate(poly_trait_ref, _) => {
TraitCandidate { trait_ref: poly_trait_ref, is_ambiguously_imported: _ } => {
// Some trait methods are excluded for arrays before 2021.
// (`array.into_iter()` wants a slice iterator for compatibility.)
if let Some(method_name) = self.method_name {
Expand Down Expand Up @@ -2373,16 +2378,16 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
}

// They are all the same, so if any of them is ambiguous, we report the pick as ambiguous.
let lint_ambiguous = probes.iter().any(|(p, _)| match p.kind {
TraitCandidate(_, lint) => lint,
let is_ambiguously_imported = probes.iter().any(|(p, _)| match p.kind {
TraitCandidate { is_ambiguously_imported, .. } => is_ambiguously_imported,
_ => false,
});

// FIXME: check the return type here somehow.
// If so, just use this trait and call it a day.
Some(Pick {
item: probes[0].0.item,
kind: TraitPick(lint_ambiguous),
kind: TraitPick { is_ambiguously_imported },
import_ids: probes[0].0.import_ids,
autoderefs: 0,
autoref_or_ptr_adjustment: None,
Expand Down Expand Up @@ -2457,14 +2462,14 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
}
}

let lint_ambiguous = match child_candidate.kind {
TraitCandidate(_, lint) => lint,
let is_ambiguously_imported = match child_candidate.kind {
TraitCandidate { is_ambiguously_imported, .. } => is_ambiguously_imported,
_ => false,
};

Some(Pick {
item: child_candidate.item,
kind: TraitPick(lint_ambiguous),
kind: TraitPick { is_ambiguously_imported },
import_ids: child_candidate.import_ids,
autoderefs: 0,
autoref_or_ptr_adjustment: None,
Expand Down Expand Up @@ -2712,7 +2717,9 @@ impl<'tcx> Candidate<'tcx> {
kind: match self.kind {
InherentImplCandidate { .. } => InherentImplPick,
ObjectCandidate(_) => ObjectPick,
TraitCandidate(_, lint_ambiguous) => TraitPick(lint_ambiguous),
TraitCandidate { is_ambiguously_imported, .. } => {
TraitPick { is_ambiguously_imported }
}
WhereClauseCandidate(trait_ref) => {
// Only trait derived from where-clauses should
// appear here, so they should not contain any
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/imports/ambiguous-trait-and-struct-in-scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ mod module_3 {
use super::*;
use crate::module_2::*;
fn weird() {
1_i32.method(); //~ WARNING Use of ambiguously glob imported trait `Foo` [ambiguous_glob_imported_traits]
1_i32.method(); //~ WARNING use of ambiguously glob imported trait `Foo` [ambiguous_glob_imported_traits]
//~| WARNING this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/imports/ambiguous-trait-and-struct-in-scope.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
warning: Use of ambiguously glob imported trait `Foo`
warning: use of ambiguously glob imported trait `Foo`
--> $DIR/ambiguous-trait-and-struct-in-scope.rs:31:19
|
LL | use crate::module_2::*;
Expand All @@ -7,7 +7,7 @@ LL | fn weird() {
LL | 1_i32.method();
| ^^^^^^
|
= help: Import `Foo` explicitly
= help: import `Foo` explicitly
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #152822 <https://github.com/rust-lang/rust/issues/152822>
= note: `#[warn(ambiguous_glob_imported_traits)]` (part of `#[warn(future_incompatible)]`) on by default
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
warning: Use of ambiguously glob imported trait `Foo`
warning: use of ambiguously glob imported trait `Foo`
--> $DIR/ambiguous-trait-in-scope-and-underscore.rs:40:10
|
LL | use export::*;
Expand All @@ -7,7 +7,7 @@ LL | use export::*;
LL | 1i32.method();
| ^^^^^^
|
= help: Import `Foo` explicitly
= help: import `Foo` explicitly
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #152822 <https://github.com/rust-lang/rust/issues/152822>
= note: `#[warn(ambiguous_glob_imported_traits)]` (part of `#[warn(future_incompatible)]`) on by default
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
warning: Use of ambiguously glob imported trait `Foo`
warning: use of ambiguously glob imported trait `Foo`
--> $DIR/ambiguous-trait-in-scope-and-underscore.rs:40:10
|
LL | use export::*;
Expand All @@ -7,7 +7,7 @@ LL | use export::*;
LL | 1i32.method();
| ^^^^^^
|
= help: Import `Foo` explicitly
= help: import `Foo` explicitly
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #152822 <https://github.com/rust-lang/rust/issues/152822>
= note: `#[warn(ambiguous_glob_imported_traits)]` (part of `#[warn(future_incompatible)]`) on by default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ use export::*;

fn main() {
1i32.method();
//~^ WARNING Use of ambiguously glob imported trait `Foo` [ambiguous_glob_imported_traits]
//~^ WARNING use of ambiguously glob imported trait `Foo` [ambiguous_glob_imported_traits]
//~| WARNING this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
}
14 changes: 7 additions & 7 deletions tests/ui/imports/ambiguous-trait-in-scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn test1() {
// Create an ambiguous import for `Trait` in one order
use m1::*;
use m2::*;
0u8.method1(); //~ WARNING Use of ambiguously glob imported trait `Trait` [ambiguous_glob_imported_traits]
0u8.method1(); //~ WARNING use of ambiguously glob imported trait `Trait` [ambiguous_glob_imported_traits]
//~| WARNING this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
0u8.method2(); //~ ERROR: no method named `method2` found for type `u8` in the current scope
}
Expand All @@ -39,44 +39,44 @@ fn test2() {
use m2::*;
use m1::*;
0u8.method1(); //~ ERROR: no method named `method1` found for type `u8` in the current scope
0u8.method2(); //~ WARNING Use of ambiguously glob imported trait `Trait` [ambiguous_glob_imported_traits]
0u8.method2(); //~ WARNING use of ambiguously glob imported trait `Trait` [ambiguous_glob_imported_traits]
//~| WARNING this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
}

fn test_indirect_reexport() {
use m1_reexport::*;
use m2_reexport::*;
0u8.method1(); //~ WARNING Use of ambiguously glob imported trait `Trait` [ambiguous_glob_imported_traits]
0u8.method1(); //~ WARNING use of ambiguously glob imported trait `Trait` [ambiguous_glob_imported_traits]
//~| WARNING this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
0u8.method2(); //~ ERROR: no method named `method2` found for type `u8` in the current scope
}

fn test_ambig_reexport() {
use ambig_reexport::*;
0u8.method1(); //~ WARNING Use of ambiguously glob imported trait `Trait` [ambiguous_glob_imported_traits]
0u8.method1(); //~ WARNING use of ambiguously glob imported trait `Trait` [ambiguous_glob_imported_traits]
//~| WARNING this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
0u8.method2(); //~ ERROR: no method named `method2` found for type `u8` in the current scope
}

fn test_external() {
use external::m1::*;
use external::m2::*;
0u8.method1(); //~ WARNING Use of ambiguously glob imported trait `Trait` [ambiguous_glob_imported_traits]
0u8.method1(); //~ WARNING use of ambiguously glob imported trait `Trait` [ambiguous_glob_imported_traits]
//~| WARNING this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
0u8.method2(); //~ ERROR: no method named `method2` found for type `u8` in the current scope
}

fn test_external_indirect_reexport() {
use external::m1_reexport::*;
use external::m2_reexport::*;
0u8.method1(); //~ WARNING Use of ambiguously glob imported trait `Trait` [ambiguous_glob_imported_traits]
0u8.method1(); //~ WARNING use of ambiguously glob imported trait `Trait` [ambiguous_glob_imported_traits]
//~| WARNING this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
0u8.method2(); //~ ERROR: no method named `method2` found for type `u8` in the current scope
}

fn test_external_ambig_reexport() {
use external::ambig_reexport::*;
0u8.method1(); //~ WARNING Use of ambiguously glob imported trait `Trait` [ambiguous_glob_imported_traits]
0u8.method1(); //~ WARNING use of ambiguously glob imported trait `Trait` [ambiguous_glob_imported_traits]
//~| WARNING this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
0u8.method2(); //~ ERROR: no method named `method2` found for type `u8` in the current scope
}
Expand Down
Loading
Loading