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

Account for associated consts in the "unstable assoc item name colission" lint #81713

Merged
merged 1 commit into from
Feb 25, 2021
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_middle/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
47 changes: 35 additions & 12 deletions compiler/rustc_typeck/src/check/method/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
7 changes: 5 additions & 2 deletions src/test/ui/inference/inference_unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
16 changes: 13 additions & 3 deletions src/test/ui/inference/inference_unstable.stderr
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/rust-lang/rust/issues/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: `<char as IpuItertools>::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 <https://github.com/rust-lang/rust/issues/48919>
= help: add `#![feature(assoc_const_ipu_iter)]` to the crate attributes to enable `inference_unstable_iterator::IpuIterator::C`

warning: 2 warnings emitted