From e65594124197bd75716d372bec1d662b2ded03e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Wed, 3 Feb 2021 08:38:05 -0800 Subject: [PATCH] Account for associated consts in the "unstable assoc item name colission" lint Fix #81663. --- compiler/rustc_middle/src/lint.rs | 8 ++-- .../rustc_typeck/src/check/method/probe.rs | 47 ++++++++++++++----- .../auxiliary/inference_unstable_iterator.rs | 6 ++- .../auxiliary/inference_unstable_itertools.rs | 6 ++- src/test/ui/inference/inference_unstable.rs | 7 ++- .../ui/inference/inference_unstable.stderr | 16 +++++-- 6 files changed, 67 insertions(+), 23 deletions(-) diff --git a/compiler/rustc_middle/src/lint.rs b/compiler/rustc_middle/src/lint.rs index 0bdccf7b5f073..bb8b661ad5d74 100644 --- a/compiler/rustc_middle/src/lint.rs +++ b/compiler/rustc_middle/src/lint.rs @@ -345,12 +345,12 @@ pub fn struct_lint_level<'s, 'd>( it will become a hard error"; let explanation = if lint_id == LintId::of(builtin::UNSTABLE_NAME_COLLISIONS) { - "once this method is added to the standard library, \ - the ambiguity may cause an error or change in behavior!" + "once this associated item is added to the standard library, the ambiguity may \ + cause an error or change in behavior!" .to_owned() } else if lint_id == LintId::of(builtin::MUTABLE_BORROW_RESERVATION_CONFLICT) { - "this borrowing pattern was not meant to be accepted, \ - and may become a hard error in the future" + "this borrowing pattern was not meant to be accepted, and may become a hard error \ + in the future" .to_owned() } else if let Some(edition) = future_incompatible.edition { format!("{} in the {} edition!", STANDARD_MESSAGE, edition) diff --git a/compiler/rustc_typeck/src/check/method/probe.rs b/compiler/rustc_typeck/src/check/method/probe.rs index 158c214759db2..2bfd697e4cdca 100644 --- a/compiler/rustc_typeck/src/check/method/probe.rs +++ b/compiler/rustc_typeck/src/check/method/probe.rs @@ -10,6 +10,7 @@ use crate::hir::def_id::DefId; use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::sync::Lrc; +use rustc_errors::Applicability; use rustc_hir as hir; use rustc_hir::def::Namespace; use rustc_infer::infer::canonical::OriginalQueryValues; @@ -1167,7 +1168,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { // // We suppress warning if we're picking the method only because it is a // suggestion. - self.emit_unstable_name_collision_hint(p, &unstable_candidates); + self.emit_unstable_name_collision_hint(p, &unstable_candidates, self_ty); } } return Some(pick); @@ -1246,24 +1247,46 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { &self, stable_pick: &Pick<'_>, unstable_candidates: &[(&Candidate<'tcx>, Symbol)], + self_ty: Ty<'tcx>, ) { self.tcx.struct_span_lint_hir( lint::builtin::UNSTABLE_NAME_COLLISIONS, self.fcx.body_id, self.span, |lint| { - let mut diag = lint.build( - "a method with this name may be added to the standard library in the future", - ); - // FIXME: This should be a `span_suggestion` instead of `help` - // However `self.span` only - // highlights the method name, so we can't use it. Also consider reusing the code from - // `report_method_error()`. - diag.help(&format!( - "call with fully qualified syntax `{}(...)` to keep using the current method", - self.tcx.def_path_str(stable_pick.item.def_id), + let def_kind = stable_pick.item.kind.as_def_kind(); + let mut diag = lint.build(&format!( + "{} {} with this name may be added to the standard library in the future", + def_kind.article(), + def_kind.descr(stable_pick.item.def_id), )); - + match (stable_pick.item.kind, stable_pick.item.container) { + (ty::AssocKind::Fn, _) => { + // FIXME: This should be a `span_suggestion` instead of `help` + // However `self.span` only + // highlights the method name, so we can't use it. Also consider reusing + // the code from `report_method_error()`. + diag.help(&format!( + "call with fully qualified syntax `{}(...)` to keep using the current \ + method", + self.tcx.def_path_str(stable_pick.item.def_id), + )); + } + (ty::AssocKind::Const, ty::AssocItemContainer::TraitContainer(def_id)) => { + diag.span_suggestion( + self.span, + "use the fully qualified path to the associated const", + format!( + "<{} as {}>::{}", + self_ty, + self.tcx.def_path_str(def_id), + stable_pick.item.ident + ), + Applicability::MachineApplicable, + ); + } + _ => {} + } if self.tcx.sess.is_nightly_build() { for (candidate, feature) in unstable_candidates { diag.help(&format!( diff --git a/src/test/ui/inference/auxiliary/inference_unstable_iterator.rs b/src/test/ui/inference/auxiliary/inference_unstable_iterator.rs index 0dc09704e7fab..91fc398f74183 100644 --- a/src/test/ui/inference/auxiliary/inference_unstable_iterator.rs +++ b/src/test/ui/inference/auxiliary/inference_unstable_iterator.rs @@ -8,7 +8,11 @@ pub trait IpuIterator { fn ipu_flatten(&self) -> u32 { 0 } + #[unstable(feature = "assoc_const_ipu_iter", issue = "99999")] + const C: i32; } #[stable(feature = "ipu_iterator", since = "1.0.0")] -impl IpuIterator for char {} +impl IpuIterator for char { + const C: i32 = 42; +} diff --git a/src/test/ui/inference/auxiliary/inference_unstable_itertools.rs b/src/test/ui/inference/auxiliary/inference_unstable_itertools.rs index 964f35ddadec5..e00adda5c3382 100644 --- a/src/test/ui/inference/auxiliary/inference_unstable_itertools.rs +++ b/src/test/ui/inference/auxiliary/inference_unstable_itertools.rs @@ -2,6 +2,10 @@ pub trait IpuItertools { fn ipu_flatten(&self) -> u32 { 1 } + + const C: i32; } -impl IpuItertools for char {} +impl IpuItertools for char { + const C: i32 = 1; +} diff --git a/src/test/ui/inference/inference_unstable.rs b/src/test/ui/inference/inference_unstable.rs index 0b95770018241..86bb62b8a5f64 100644 --- a/src/test/ui/inference/inference_unstable.rs +++ b/src/test/ui/inference/inference_unstable.rs @@ -14,6 +14,9 @@ use inference_unstable_itertools::IpuItertools; fn main() { assert_eq!('x'.ipu_flatten(), 1); - //~^ WARN a method with this name may be added to the standard library in the future - //~^^ WARN once this method is added to the standard library, the ambiguity may cause an error +//~^ WARN an associated function with this name may be added to the standard library in the future +//~| WARN once this associated item is added to the standard library, the ambiguity may cause an + assert_eq!(char::C, 1); +//~^ WARN an associated constant with this name may be added to the standard library in the future +//~| WARN once this associated item is added to the standard library, the ambiguity may cause an } diff --git a/src/test/ui/inference/inference_unstable.stderr b/src/test/ui/inference/inference_unstable.stderr index df52012463255..2c282e610b29c 100644 --- a/src/test/ui/inference/inference_unstable.stderr +++ b/src/test/ui/inference/inference_unstable.stderr @@ -1,14 +1,24 @@ -warning: a method with this name may be added to the standard library in the future +warning: an associated function with this name may be added to the standard library in the future --> $DIR/inference_unstable.rs:16:20 | LL | assert_eq!('x'.ipu_flatten(), 1); | ^^^^^^^^^^^ | = note: `#[warn(unstable_name_collisions)]` on by default - = warning: once this method is added to the standard library, the ambiguity may cause an error or change in behavior! + = warning: once this associated item is added to the standard library, the ambiguity may cause an error or change in behavior! = note: for more information, see issue #48919 = help: call with fully qualified syntax `inference_unstable_itertools::IpuItertools::ipu_flatten(...)` to keep using the current method = help: add `#![feature(ipu_flatten)]` to the crate attributes to enable `inference_unstable_iterator::IpuIterator::ipu_flatten` -warning: 1 warning emitted +warning: an associated constant with this name may be added to the standard library in the future + --> $DIR/inference_unstable.rs:19:16 + | +LL | assert_eq!(char::C, 1); + | ^^^^^^^ help: use the fully qualified path to the associated const: `::C` + | + = warning: once this associated item is added to the standard library, the ambiguity may cause an error or change in behavior! + = note: for more information, see issue #48919 + = help: add `#![feature(assoc_const_ipu_iter)]` to the crate attributes to enable `inference_unstable_iterator::IpuIterator::C` + +warning: 2 warnings emitted