-
Notifications
You must be signed in to change notification settings - Fork 14k
FCW Lint when using an ambiguously glob imported trait #149058
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
base: main
Are you sure you want to change the base?
Changes from all commits
2a03c50
8adfde3
c925e11
a619ad8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,3 +1,4 @@ | ||||||
| use std::fmt::Debug; | ||||||
| use std::ops::Deref; | ||||||
|
|
||||||
| use rustc_hir as hir; | ||||||
|
|
@@ -12,7 +13,7 @@ use rustc_hir_analysis::hir_ty_lowering::{ | |||||
| use rustc_infer::infer::{ | ||||||
| BoundRegionConversionTime, DefineOpaqueTypes, InferOk, RegionVariableOrigin, | ||||||
| }; | ||||||
| use rustc_lint::builtin::SUPERTRAIT_ITEM_SHADOWING_USAGE; | ||||||
| use rustc_lint::builtin::{AMBIGUOUS_TRAIT_GLOB_IMPORTS, SUPERTRAIT_ITEM_SHADOWING_USAGE}; | ||||||
| use rustc_middle::traits::ObligationCauseCode; | ||||||
| use rustc_middle::ty::adjustment::{ | ||||||
| Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability, PointerCoercion, | ||||||
|
|
@@ -149,6 +150,9 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> { | |||||
| // Lint when an item is shadowing a supertrait item. | ||||||
| self.lint_shadowed_supertrait_items(pick, segment); | ||||||
|
|
||||||
| // Lint when a trait is ambiguously imported | ||||||
| self.lint_ambiguously_glob_imported_trait(pick, segment); | ||||||
|
|
||||||
| // Add any trait/regions obligations specified on the method's type parameters. | ||||||
| // We won't add these if we encountered an illegal sized bound, so that we can use | ||||||
| // a custom error in that case. | ||||||
|
|
@@ -322,7 +326,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>` | ||||||
|
|
@@ -716,6 +720,26 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> { | |||||
| ); | ||||||
| } | ||||||
|
|
||||||
| fn lint_ambiguously_glob_imported_trait( | ||||||
| &self, | ||||||
| pick: &probe::Pick<'_>, | ||||||
| segment: &hir::PathSegment<'tcx>, | ||||||
| ) { | ||||||
| if let probe::PickKind::TraitPick(true) = pick.kind { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| } else { | ||||||
| return; | ||||||
| }; | ||||||
| let trait_name = self.tcx.item_name(pick.item.container_id(self.tcx)); | ||||||
| let import_span = self.tcx.hir_span_if_local(pick.import_ids[0].to_def_id()).unwrap(); | ||||||
|
|
||||||
| self.tcx.node_lint(AMBIGUOUS_TRAIT_GLOB_IMPORTS, segment.hir_id, |diag| { | ||||||
| diag.primary_message(format!("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")); | ||||||
| }); | ||||||
|
Comment on lines
+735
to
+740
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to add some kind of suggestion like: |
||||||
| } | ||||||
|
|
||||||
| fn upcast( | ||||||
| &mut self, | ||||||
| source_trait_ref: ty::PolyTraitRef<'tcx>, | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| mod m1 { | ||
| pub trait Trait { | ||
| fn method1(&self) {} | ||
| } | ||
| impl Trait for u8 {} | ||
| } | ||
| mod m2 { | ||
| pub trait Trait { | ||
| fn method2(&self) {} | ||
| } | ||
| impl Trait for u8 {} | ||
| } | ||
|
|
||
| pub 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_trait_glob_imports] | ||
| //~| 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 test2() { | ||
| // Create an ambiguous import for `Trait` in another order | ||
| 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_trait_glob_imports] | ||
| //~| WARNING: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
| } | ||
|
|
||
| fn main() {} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you copy all the tests from #148946 as well? |
||
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.
The FIXME still needs to be addressed.