Skip to content

Commit df64d82

Browse files
authored
Rollup merge of rust-lang#101055 - TaKO8Ki:use-smaller-span, r=compiler-errors
Use smaller span for suggestions
2 parents f37d3ea + b33c3d6 commit df64d82

File tree

10 files changed

+35
-33
lines changed

10 files changed

+35
-33
lines changed

compiler/rustc_attr/src/session_diagnostics.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -223,14 +223,12 @@ impl<'a> SessionDiagnostic<'a> for UnsupportedLiteral {
223223
error_code!(E0565),
224224
);
225225
if self.is_bytestr {
226-
if let Ok(lint_str) = sess.source_map().span_to_snippet(self.span) {
227-
diag.span_suggestion(
228-
self.span,
229-
fluent::attr::unsupported_literal_suggestion,
230-
&lint_str[1..],
231-
Applicability::MaybeIncorrect,
232-
);
233-
}
226+
diag.span_suggestion(
227+
sess.source_map().start_point(self.span),
228+
fluent::attr::unsupported_literal_suggestion,
229+
"",
230+
Applicability::MaybeIncorrect,
231+
);
234232
}
235233
diag
236234
}

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

+5-6
Original file line numberDiff line numberDiff line change
@@ -1299,7 +1299,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12991299
// local binding
13001300
if let hir::def::Res::Local(hir_id) = path.res {
13011301
let span = tcx.hir().span(hir_id);
1302-
let snippet = tcx.sess.source_map().span_to_snippet(span);
13031302
let filename = tcx.sess.source_map().span_to_filename(span);
13041303

13051304
let parent_node =
@@ -1309,22 +1308,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13091308
concrete_type,
13101309
);
13111310

1312-
match (filename, parent_node, snippet) {
1311+
match (filename, parent_node) {
13131312
(
13141313
FileName::Real(_),
13151314
Node::Local(hir::Local {
13161315
source: hir::LocalSource::Normal,
13171316
ty,
13181317
..
13191318
}),
1320-
Ok(ref snippet),
13211319
) => {
1320+
let type_span = ty.map(|ty| ty.span.with_lo(span.hi())).unwrap_or(span.shrink_to_hi());
13221321
err.span_suggestion(
13231322
// account for `let x: _ = 42;`
1324-
// ^^^^
1325-
span.to(ty.as_ref().map(|ty| ty.span).unwrap_or(span)),
1323+
// ^^^
1324+
type_span,
13261325
&msg,
1327-
format!("{}: {}", snippet, concrete_type),
1326+
format!(": {concrete_type}"),
13281327
Applicability::MaybeIncorrect,
13291328
);
13301329
}

compiler/rustc_typeck/src/structured_errors/wrong_number_of_generic_args.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -762,16 +762,13 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
762762
// If there is a single unbound associated type and a single excess generic param
763763
// suggest replacing the generic param with the associated type bound
764764
if provided_args_matches_unbound_traits && !unbound_types.is_empty() {
765-
let mut suggestions = vec![];
766765
let unused_generics = &self.gen_args.args[self.num_expected_type_or_const_args()..];
767-
for (potential, name) in iter::zip(unused_generics, &unbound_types) {
768-
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(potential.span()) {
769-
suggestions.push((potential.span(), format!("{} = {}", name, snippet)));
770-
}
771-
}
766+
let suggestions = iter::zip(unused_generics, &unbound_types)
767+
.map(|(potential, name)| (potential.span().shrink_to_lo(), format!("{name} = ")))
768+
.collect::<Vec<_>>();
772769

773770
if !suggestions.is_empty() {
774-
err.multipart_suggestion(
771+
err.multipart_suggestion_verbose(
775772
&format!(
776773
"replace the generic bound{s} with the associated type{s}",
777774
s = pluralize!(unbound_types.len())

src/test/ui/conditional-compilation/cfg-attr-syntax-validation.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ error[E0565]: literal in `cfg` predicate value must be a string
5050
--> $DIR/cfg-attr-syntax-validation.rs:25:11
5151
|
5252
LL | #[cfg(a = b"hi")]
53-
| ^^^^^ help: consider removing the prefix: `"hi"`
53+
| -^^^^
54+
| |
55+
| help: consider removing the prefix
5456

5557
error: expected unsuffixed literal or identifier, found `concat!("nonexistent")`
5658
--> $DIR/cfg-attr-syntax-validation.rs:30:25

src/test/ui/const-generics/issues/issue-87493.stderr

+5-3
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@ error[E0107]: this trait takes 0 generic arguments but 1 generic argument was su
1313
--> $DIR/issue-87493.rs:8:8
1414
|
1515
LL | T: MyTrait<Assoc == S::Assoc>,
16-
| ^^^^^^^ ----------------- help: replace the generic bound with the associated type: `Assoc = Assoc == S::Assoc`
17-
| |
18-
| expected 0 generic arguments
16+
| ^^^^^^^ expected 0 generic arguments
1917
|
2018
note: trait defined here, with 0 generic parameters
2119
--> $DIR/issue-87493.rs:1:11
2220
|
2321
LL | pub trait MyTrait {
2422
| ^^^^^^^
23+
help: replace the generic bound with the associated type
24+
|
25+
LL | T: MyTrait<Assoc = Assoc == S::Assoc>,
26+
| +++++++
2527

2628
error: aborting due to 2 previous errors
2729

src/test/ui/deprecation/deprecation-sanity.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ error[E0565]: literal in `deprecated` value must be a string
4444
--> $DIR/deprecation-sanity.rs:19:25
4545
|
4646
LL | #[deprecated(note = b"test")]
47-
| ^^^^^^^ help: consider removing the prefix: `"test"`
47+
| -^^^^^^
48+
| |
49+
| help: consider removing the prefix
4850

4951
error[E0565]: item in `deprecated` must be a key/value pair
5052
--> $DIR/deprecation-sanity.rs:22:18

src/test/ui/error-codes/E0107.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ LL | pub trait T {
142142
help: replace the generic bounds with the associated types
143143
|
144144
LL | fn trait_bound_generic<I: T<A = u8, B = u16>>(_i: I) {
145-
| ~~~~~~ ~~~~~~~
145+
| +++ +++
146146

147147
error: aborting due to 10 previous errors
148148

src/test/ui/error-codes/E0565-2.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error[E0565]: literal in `deprecated` value must be a string
22
--> $DIR/E0565-2.rs:2:22
33
|
44
LL | #[deprecated(since = b"1.29", note = "hi")]
5-
| ^^^^^^^ help: consider removing the prefix: `"1.29"`
5+
| -^^^^^^
6+
| |
7+
| help: consider removing the prefix
68

79
error: aborting due to previous error
810

src/test/ui/methods/method-on-ambiguous-numeric-type.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ LL | let x = y.neg();
1818
help: you must specify a type for this binding, like `f32`
1919
|
2020
LL | let y: f32 = 2.0;
21-
| ~~~~~~
21+
| +++++
2222

2323
error[E0689]: can't call method `pow` on ambiguous numeric type `{integer}`
2424
--> $DIR/method-on-ambiguous-numeric-type.rs:19:26
@@ -37,7 +37,7 @@ LL | local_bar.pow(2);
3737
help: you must specify a type for this binding, like `i32`
3838
|
3939
LL | ($ident:ident) => { let $ident: i32 = 42; }
40-
| ~~~~~~~~~~~
40+
| +++++
4141

4242
error[E0689]: can't call method `pow` on ambiguous numeric type `{integer}`
4343
--> $DIR/method-on-ambiguous-numeric-type.rs:30:9
@@ -46,10 +46,10 @@ LL | bar.pow(2);
4646
| ^^^
4747
|
4848
help: you must specify a type for this binding, like `i32`
49-
--> $DIR/auxiliary/macro-in-other-crate.rs:3:29
49+
--> $DIR/auxiliary/macro-in-other-crate.rs:3:35
5050
|
5151
LL | ($ident:ident) => { let $ident: i32 = 42; }
52-
| ~~~~~~~~~~~
52+
| +++++
5353

5454
error: aborting due to 5 previous errors
5555

src/test/ui/suggestions/use-type-argument-instead-of-assoc-type.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ LL | pub trait T<X, Y> {
1212
help: replace the generic bounds with the associated types
1313
|
1414
LL | i: Box<dyn T<usize, usize, A = usize, C = usize, B=usize>>,
15-
| ~~~~~~~~~ ~~~~~~~~~
15+
| +++ +++
1616

1717
error[E0191]: the value of the associated types `A` (from trait `T`), `C` (from trait `T`) must be specified
1818
--> $DIR/use-type-argument-instead-of-assoc-type.rs:7:16

0 commit comments

Comments
 (0)