Skip to content

Commit e655941

Browse files
committed
Account for associated consts in the "unstable assoc item name colission" lint
Fix #81663.
1 parent b81f581 commit e655941

File tree

6 files changed

+67
-23
lines changed

6 files changed

+67
-23
lines changed

compiler/rustc_middle/src/lint.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -345,12 +345,12 @@ pub fn struct_lint_level<'s, 'd>(
345345
it will become a hard error";
346346

347347
let explanation = if lint_id == LintId::of(builtin::UNSTABLE_NAME_COLLISIONS) {
348-
"once this method is added to the standard library, \
349-
the ambiguity may cause an error or change in behavior!"
348+
"once this associated item is added to the standard library, the ambiguity may \
349+
cause an error or change in behavior!"
350350
.to_owned()
351351
} else if lint_id == LintId::of(builtin::MUTABLE_BORROW_RESERVATION_CONFLICT) {
352-
"this borrowing pattern was not meant to be accepted, \
353-
and may become a hard error in the future"
352+
"this borrowing pattern was not meant to be accepted, and may become a hard error \
353+
in the future"
354354
.to_owned()
355355
} else if let Some(edition) = future_incompatible.edition {
356356
format!("{} in the {} edition!", STANDARD_MESSAGE, edition)

compiler/rustc_typeck/src/check/method/probe.rs

+35-12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::hir::def_id::DefId;
1010

1111
use rustc_data_structures::fx::FxHashSet;
1212
use rustc_data_structures::sync::Lrc;
13+
use rustc_errors::Applicability;
1314
use rustc_hir as hir;
1415
use rustc_hir::def::Namespace;
1516
use rustc_infer::infer::canonical::OriginalQueryValues;
@@ -1167,7 +1168,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
11671168
//
11681169
// We suppress warning if we're picking the method only because it is a
11691170
// suggestion.
1170-
self.emit_unstable_name_collision_hint(p, &unstable_candidates);
1171+
self.emit_unstable_name_collision_hint(p, &unstable_candidates, self_ty);
11711172
}
11721173
}
11731174
return Some(pick);
@@ -1246,24 +1247,46 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
12461247
&self,
12471248
stable_pick: &Pick<'_>,
12481249
unstable_candidates: &[(&Candidate<'tcx>, Symbol)],
1250+
self_ty: Ty<'tcx>,
12491251
) {
12501252
self.tcx.struct_span_lint_hir(
12511253
lint::builtin::UNSTABLE_NAME_COLLISIONS,
12521254
self.fcx.body_id,
12531255
self.span,
12541256
|lint| {
1255-
let mut diag = lint.build(
1256-
"a method with this name may be added to the standard library in the future",
1257-
);
1258-
// FIXME: This should be a `span_suggestion` instead of `help`
1259-
// However `self.span` only
1260-
// highlights the method name, so we can't use it. Also consider reusing the code from
1261-
// `report_method_error()`.
1262-
diag.help(&format!(
1263-
"call with fully qualified syntax `{}(...)` to keep using the current method",
1264-
self.tcx.def_path_str(stable_pick.item.def_id),
1257+
let def_kind = stable_pick.item.kind.as_def_kind();
1258+
let mut diag = lint.build(&format!(
1259+
"{} {} with this name may be added to the standard library in the future",
1260+
def_kind.article(),
1261+
def_kind.descr(stable_pick.item.def_id),
12651262
));
1266-
1263+
match (stable_pick.item.kind, stable_pick.item.container) {
1264+
(ty::AssocKind::Fn, _) => {
1265+
// FIXME: This should be a `span_suggestion` instead of `help`
1266+
// However `self.span` only
1267+
// highlights the method name, so we can't use it. Also consider reusing
1268+
// the code from `report_method_error()`.
1269+
diag.help(&format!(
1270+
"call with fully qualified syntax `{}(...)` to keep using the current \
1271+
method",
1272+
self.tcx.def_path_str(stable_pick.item.def_id),
1273+
));
1274+
}
1275+
(ty::AssocKind::Const, ty::AssocItemContainer::TraitContainer(def_id)) => {
1276+
diag.span_suggestion(
1277+
self.span,
1278+
"use the fully qualified path to the associated const",
1279+
format!(
1280+
"<{} as {}>::{}",
1281+
self_ty,
1282+
self.tcx.def_path_str(def_id),
1283+
stable_pick.item.ident
1284+
),
1285+
Applicability::MachineApplicable,
1286+
);
1287+
}
1288+
_ => {}
1289+
}
12671290
if self.tcx.sess.is_nightly_build() {
12681291
for (candidate, feature) in unstable_candidates {
12691292
diag.help(&format!(

src/test/ui/inference/auxiliary/inference_unstable_iterator.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ pub trait IpuIterator {
88
fn ipu_flatten(&self) -> u32 {
99
0
1010
}
11+
#[unstable(feature = "assoc_const_ipu_iter", issue = "99999")]
12+
const C: i32;
1113
}
1214

1315
#[stable(feature = "ipu_iterator", since = "1.0.0")]
14-
impl IpuIterator for char {}
16+
impl IpuIterator for char {
17+
const C: i32 = 42;
18+
}

src/test/ui/inference/auxiliary/inference_unstable_itertools.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ pub trait IpuItertools {
22
fn ipu_flatten(&self) -> u32 {
33
1
44
}
5+
6+
const C: i32;
57
}
68

7-
impl IpuItertools for char {}
9+
impl IpuItertools for char {
10+
const C: i32 = 1;
11+
}

src/test/ui/inference/inference_unstable.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ use inference_unstable_itertools::IpuItertools;
1414

1515
fn main() {
1616
assert_eq!('x'.ipu_flatten(), 1);
17-
//~^ WARN a method with this name may be added to the standard library in the future
18-
//~^^ WARN once this method is added to the standard library, the ambiguity may cause an error
17+
//~^ WARN an associated function with this name may be added to the standard library in the future
18+
//~| WARN once this associated item is added to the standard library, the ambiguity may cause an
19+
assert_eq!(char::C, 1);
20+
//~^ WARN an associated constant with this name may be added to the standard library in the future
21+
//~| WARN once this associated item is added to the standard library, the ambiguity may cause an
1922
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1-
warning: a method with this name may be added to the standard library in the future
1+
warning: an associated function with this name may be added to the standard library in the future
22
--> $DIR/inference_unstable.rs:16:20
33
|
44
LL | assert_eq!('x'.ipu_flatten(), 1);
55
| ^^^^^^^^^^^
66
|
77
= note: `#[warn(unstable_name_collisions)]` on by default
8-
= warning: once this method is added to the standard library, the ambiguity may cause an error or change in behavior!
8+
= warning: once this associated item is added to the standard library, the ambiguity may cause an error or change in behavior!
99
= note: for more information, see issue #48919 <https://github.com/rust-lang/rust/issues/48919>
1010
= help: call with fully qualified syntax `inference_unstable_itertools::IpuItertools::ipu_flatten(...)` to keep using the current method
1111
= help: add `#![feature(ipu_flatten)]` to the crate attributes to enable `inference_unstable_iterator::IpuIterator::ipu_flatten`
1212

13-
warning: 1 warning emitted
13+
warning: an associated constant with this name may be added to the standard library in the future
14+
--> $DIR/inference_unstable.rs:19:16
15+
|
16+
LL | assert_eq!(char::C, 1);
17+
| ^^^^^^^ help: use the fully qualified path to the associated const: `<char as IpuItertools>::C`
18+
|
19+
= warning: once this associated item is added to the standard library, the ambiguity may cause an error or change in behavior!
20+
= note: for more information, see issue #48919 <https://github.com/rust-lang/rust/issues/48919>
21+
= help: add `#![feature(assoc_const_ipu_iter)]` to the crate attributes to enable `inference_unstable_iterator::IpuIterator::C`
22+
23+
warning: 2 warnings emitted
1424

0 commit comments

Comments
 (0)