Skip to content

Commit 8542dd0

Browse files
committed
Use more targeted suggestion when confusing i8 with std::i8
1 parent 357bc27 commit 8542dd0

File tree

3 files changed

+21
-26
lines changed

3 files changed

+21
-26
lines changed

compiler/rustc_typeck/src/astconv/mod.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -1575,18 +1575,17 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
15751575
name: Symbol,
15761576
) -> ErrorGuaranteed {
15771577
let mut err = struct_span_err!(self.tcx().sess, span, E0223, "ambiguous associated type");
1578-
if let (true, Ok(snippet)) = (
1579-
self.tcx()
1580-
.resolutions(())
1581-
.confused_type_with_std_module
1582-
.keys()
1583-
.any(|full_span| full_span.contains(span)),
1584-
self.tcx().sess.source_map().span_to_snippet(span),
1585-
) {
1578+
if self
1579+
.tcx()
1580+
.resolutions(())
1581+
.confused_type_with_std_module
1582+
.keys()
1583+
.any(|full_span| full_span.contains(span))
1584+
{
15861585
err.span_suggestion(
1587-
span,
1586+
span.shrink_to_lo(),
15881587
"you are looking for the module in `std`, not the primitive type",
1589-
format!("std::{}", snippet),
1588+
"std::".to_string(),
15901589
Applicability::MachineApplicable,
15911590
);
15921591
} else {

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

+10-14
Original file line numberDiff line numberDiff line change
@@ -327,26 +327,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
327327
);
328328
}
329329
if let Some(span) = tcx.resolutions(()).confused_type_with_std_module.get(&span) {
330-
if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(*span) {
331-
err.span_suggestion(
332-
*span,
333-
"you are looking for the module in `std`, \
334-
not the primitive type",
335-
format!("std::{}", snippet),
336-
Applicability::MachineApplicable,
337-
);
338-
}
330+
err.span_suggestion(
331+
span.shrink_to_lo(),
332+
"you are looking for the module in `std`, not the primitive type",
333+
"std::".to_string(),
334+
Applicability::MachineApplicable,
335+
);
339336
}
340337
if let ty::RawPtr(_) = &actual.kind() {
341338
err.note(
342339
"try using `<*const T>::as_ref()` to get a reference to the \
343-
type behind the pointer: https://doc.rust-lang.org/std/\
344-
primitive.pointer.html#method.as_ref",
340+
type behind the pointer: https://doc.rust-lang.org/std/\
341+
primitive.pointer.html#method.as_ref",
345342
);
346343
err.note(
347-
"using `<*const T>::as_ref()` on a pointer \
348-
which is unaligned or points to invalid \
349-
or uninitialized memory is undefined behavior",
344+
"using `<*const T>::as_ref()` on a pointer which is unaligned or points \
345+
to invalid or uninitialized memory is undefined behavior",
350346
);
351347
}
352348

src/test/ui/suggestions/suggest-std-when-using-type.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | let pi = f32::consts::PI;
77
help: you are looking for the module in `std`, not the primitive type
88
|
99
LL | let pi = std::f32::consts::PI;
10-
| ~~~~~~~~~~~~~~~~
10+
| +++++
1111

1212
error[E0599]: no function or associated item named `from_utf8` found for type `str` in the current scope
1313
--> $DIR/suggest-std-when-using-type.rs:5:14
@@ -18,7 +18,7 @@ LL | str::from_utf8(bytes)
1818
help: you are looking for the module in `std`, not the primitive type
1919
|
2020
LL | std::str::from_utf8(bytes)
21-
| ~~~~~~~~~~~~~~~~~~~
21+
| +++++
2222

2323
error: aborting due to 2 previous errors
2424

0 commit comments

Comments
 (0)