Skip to content

Commit 93f5ba0

Browse files
committed
Auto merge of #59084 - estebank:diagnostic-spans, r=davidtwco
Tweak some diagnostic spans
2 parents 0633c55 + 59f0f2e commit 93f5ba0

File tree

81 files changed

+496
-546
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+496
-546
lines changed

src/librustc/mir/interpret/error.rs

+13
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ impl<'a, 'gcx, 'tcx> ConstEvalErr<'tcx> {
100100
tcx: TyCtxtAt<'a, 'gcx, 'tcx>,
101101
message: &str,
102102
lint_root: hir::HirId,
103+
span: Option<Span>,
103104
) -> ErrorHandled {
104105
let lint = self.struct_generic(
105106
tcx,
@@ -108,6 +109,18 @@ impl<'a, 'gcx, 'tcx> ConstEvalErr<'tcx> {
108109
);
109110
match lint {
110111
Ok(mut lint) => {
112+
if let Some(span) = span {
113+
let primary_spans = lint.span.primary_spans().to_vec();
114+
// point at the actual error as the primary span
115+
lint.replace_span_with(span);
116+
// point to the `const` statement as a secondary span
117+
// they don't have any label
118+
for sp in primary_spans {
119+
if sp != span {
120+
lint.span_label(sp, "");
121+
}
122+
}
123+
}
111124
lint.emit();
112125
ErrorHandled::Reported
113126
},

src/librustc_errors/diagnostic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ impl Diagnostic {
366366
}],
367367
}],
368368
msg: msg.to_owned(),
369-
style: SuggestionStyle::HideCodeInline,
369+
style: SuggestionStyle::HideCodeAlways,
370370
applicability,
371371
});
372372
self

src/librustc_metadata/native_libs.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ impl<'a, 'tcx> ItemLikeVisitor<'tcx> for Collector<'a, 'tcx> {
7474
"dylib" => cstore::NativeUnknown,
7575
"framework" => cstore::NativeFramework,
7676
k => {
77-
struct_span_err!(self.tcx.sess, m.span, E0458,
77+
struct_span_err!(self.tcx.sess, item.span(), E0458,
7878
"unknown kind: `{}`", k)
79-
.span_label(item.span(), "unknown kind").emit();
79+
.span_label(item.span(), "unknown kind")
80+
.span_label(m.span, "").emit();
8081
cstore::NativeUnknown
8182
}
8283
};

src/librustc_mir/const_eval.rs

+2
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,7 @@ pub fn const_eval_raw_provider<'a, 'tcx>(
668668
tcx.at(tcx.def_span(def_id)),
669669
"any use of this value will cause an error",
670670
hir_id,
671+
Some(err.span),
671672
)
672673
},
673674
// promoting runtime code is only allowed to error if it references broken constants
@@ -684,6 +685,7 @@ pub fn const_eval_raw_provider<'a, 'tcx>(
684685
tcx.at(span),
685686
"reaching this expression at runtime will panic or abort",
686687
tcx.hir().as_local_hir_id(def_id).unwrap(),
688+
Some(err.span),
687689
)
688690
}
689691
// anything else (array lengths, enum initializers, constant patterns) are reported

src/librustc_mir/transform/const_prop.rs

+1
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ impl<'a, 'mir, 'tcx> ConstPropagator<'a, 'mir, 'tcx> {
237237
self.ecx.tcx,
238238
"this expression will panic at runtime",
239239
lint_root,
240+
None,
240241
);
241242
}
242243
}

src/librustc_resolve/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4932,7 +4932,7 @@ impl<'a> Resolver<'a> {
49324932
Some((directive, _, true)) if should_remove_import && !directive.is_glob() => {
49334933
// Simple case - remove the entire import. Due to the above match arm, this can
49344934
// only be a single use so just remove it entirely.
4935-
err.span_suggestion(
4935+
err.tool_only_span_suggestion(
49364936
directive.use_span_with_attributes,
49374937
"remove unnecessary import",
49384938
String::new(),
@@ -5112,7 +5112,7 @@ impl<'a> Resolver<'a> {
51125112
// extra for the comma.
51135113
span.lo().0 - (prev_comma.as_bytes().len() as u32) - 1
51145114
));
5115-
err.span_suggestion(
5115+
err.tool_only_span_suggestion(
51165116
span, message, String::new(), Applicability::MaybeIncorrect,
51175117
);
51185118
return;

src/librustc_typeck/check/method/suggest.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,18 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
6969
error: MethodError<'tcx>,
7070
args: Option<&'gcx [hir::Expr]>,
7171
) {
72+
let orig_span = span;
73+
let mut span = span;
7274
// Avoid suggestions when we don't know what's going on.
7375
if rcvr_ty.references_error() {
7476
return;
7577
}
7678

77-
let report_candidates = |err: &mut DiagnosticBuilder<'_>,
78-
mut sources: Vec<CandidateSource>| {
79+
let report_candidates = |
80+
span: Span,
81+
err: &mut DiagnosticBuilder<'_>,
82+
mut sources: Vec<CandidateSource>,
83+
| {
7984
sources.sort();
8085
sources.dedup();
8186
// Dynamic limit to avoid hiding just one candidate, which is silly.
@@ -293,9 +298,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
293298
err.emit();
294299
return;
295300
} else {
301+
span = item_name.span;
296302
let mut err = struct_span_err!(
297303
tcx.sess,
298-
item_name.span,
304+
span,
299305
E0599,
300306
"no {} named `{}` found for type `{}` in the current scope",
301307
item_kind,
@@ -305,7 +311,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
305311
if let Some(suggestion) = suggestion {
306312
// enum variant
307313
err.span_suggestion(
308-
item_name.span,
314+
span,
309315
"did you mean",
310316
suggestion.to_string(),
311317
Applicability::MaybeIncorrect,
@@ -392,7 +398,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
392398
}
393399
} else {
394400
err.span_label(span, format!("{} not found in `{}`", item_kind, ty_str));
395-
self.tcx.sess.trait_methods_not_found.borrow_mut().insert(span);
401+
self.tcx.sess.trait_methods_not_found.borrow_mut().insert(orig_span);
396402
}
397403

398404
if self.is_fn_ty(&rcvr_ty, span) {
@@ -434,9 +440,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
434440
self.ty_to_string(actual), item_name));
435441
}
436442

437-
report_candidates(&mut err, static_sources);
443+
report_candidates(span, &mut err, static_sources);
438444
} else if static_sources.len() > 1 {
439-
report_candidates(&mut err, static_sources);
445+
report_candidates(span, &mut err, static_sources);
440446
}
441447

442448
if !unsatisfied_predicates.is_empty() {
@@ -481,7 +487,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
481487
"multiple applicable items in scope");
482488
err.span_label(span, format!("multiple `{}` found", item_name));
483489

484-
report_candidates(&mut err, sources);
490+
report_candidates(span, &mut err, sources);
485491
err.emit();
486492
}
487493

src/libsyntax/parse/parser.rs

+14-7
Original file line numberDiff line numberDiff line change
@@ -5598,8 +5598,14 @@ impl<'a> Parser<'a> {
55985598

55995599
if !negative_bounds.is_empty() || was_negative {
56005600
let plural = negative_bounds.len() > 1;
5601-
let mut err = self.struct_span_err(negative_bounds,
5602-
"negative trait bounds are not supported");
5601+
let last_span = negative_bounds.last().map(|sp| *sp);
5602+
let mut err = self.struct_span_err(
5603+
negative_bounds,
5604+
"negative trait bounds are not supported",
5605+
);
5606+
if let Some(sp) = last_span {
5607+
err.span_label(sp, "negative trait bounds are not supported");
5608+
}
56035609
if let Some(bound_list) = colon_span {
56045610
let bound_list = bound_list.to(self.prev_span);
56055611
let mut new_bound_list = String::new();
@@ -5612,11 +5618,12 @@ impl<'a> Parser<'a> {
56125618
}
56135619
new_bound_list = new_bound_list.replacen(" +", ":", 1);
56145620
}
5615-
err.span_suggestion_short(bound_list,
5616-
&format!("remove the trait bound{}",
5617-
if plural { "s" } else { "" }),
5618-
new_bound_list,
5619-
Applicability::MachineApplicable);
5621+
err.span_suggestion_hidden(
5622+
bound_list,
5623+
&format!("remove the trait bound{}", if plural { "s" } else { "" }),
5624+
new_bound_list,
5625+
Applicability::MachineApplicable,
5626+
);
56205627
}
56215628
err.emit();
56225629
}

src/test/ui/array_const_index-0.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: any use of this value will cause an error
2-
--> $DIR/array_const_index-0.rs:2:1
2+
--> $DIR/array_const_index-0.rs:2:16
33
|
44
LL | const B: i32 = (&A)[1];
5-
| ^^^^^^^^^^^^^^^-------^
5+
| ---------------^^^^^^^-
66
| |
77
| index out of bounds: the len is 0 but the index is 1
88
|

src/test/ui/array_const_index-1.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: any use of this value will cause an error
2-
--> $DIR/array_const_index-1.rs:2:1
2+
--> $DIR/array_const_index-1.rs:2:16
33
|
44
LL | const B: i32 = A[1];
5-
| ^^^^^^^^^^^^^^^----^
5+
| ---------------^^^^-
66
| |
77
| index out of bounds: the len is 0 but the index is 1
88
|

src/test/ui/associated-const/associated-const-no-item.stderr

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ error[E0599]: no associated item named `ID` found for type `i32` in the current
22
--> $DIR/associated-const-no-item.rs:5:23
33
|
44
LL | const X: i32 = <i32>::ID;
5-
| -------^^
6-
| |
7-
| associated item not found in `i32`
5+
| ^^ associated item not found in `i32`
86
|
97
= help: items from traits can only be used if the trait is implemented and in scope
108
= note: the following trait defines an item `ID`, perhaps you need to implement it:

src/test/ui/bad/bad-extern-link-attrs.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ LL | #[link(name = "")]
1111
| ^^^^^^^^^^^^^^^^^^ empty name given
1212

1313
error[E0458]: unknown kind: `bar`
14-
--> $DIR/bad-extern-link-attrs.rs:4:1
14+
--> $DIR/bad-extern-link-attrs.rs:4:22
1515
|
1616
LL | #[link(name = "foo", kind = "bar")]
17-
| ^^^^^^^^^^^^^^^^^^^^^------------^^
17+
| ---------------------^^^^^^^^^^^^--
1818
| |
1919
| unknown kind
2020

src/test/ui/bogus-tag.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | enum Color { Rgb(isize, isize, isize), Rgba(isize, isize, isize, isize), }
55
| ---------- variant `Hsl` not found here
66
...
77
LL | Color::Hsl(h, s, l) => { println!("hsl"); }
8-
| -------^^^--------- variant not found in `Color`
8+
| ^^^ variant not found in `Color`
99

1010
error: aborting due to previous error
1111

src/test/ui/consts/const-err-early.stderr

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: any use of this value will cause an error
2-
--> $DIR/const-err-early.rs:3:1
2+
--> $DIR/const-err-early.rs:3:19
33
|
44
LL | pub const A: i8 = -std::i8::MIN;
5-
| ^^^^^^^^^^^^^^^^^^-------------^
5+
| ------------------^^^^^^^^^^^^^-
66
| |
77
| attempt to negate with overflow
88
|
@@ -13,34 +13,34 @@ LL | #![deny(const_err)]
1313
| ^^^^^^^^^
1414

1515
error: any use of this value will cause an error
16-
--> $DIR/const-err-early.rs:4:1
16+
--> $DIR/const-err-early.rs:4:19
1717
|
1818
LL | pub const B: u8 = 200u8 + 200u8;
19-
| ^^^^^^^^^^^^^^^^^^-------------^
19+
| ------------------^^^^^^^^^^^^^-
2020
| |
2121
| attempt to add with overflow
2222

2323
error: any use of this value will cause an error
24-
--> $DIR/const-err-early.rs:5:1
24+
--> $DIR/const-err-early.rs:5:19
2525
|
2626
LL | pub const C: u8 = 200u8 * 4;
27-
| ^^^^^^^^^^^^^^^^^^---------^
27+
| ------------------^^^^^^^^^-
2828
| |
2929
| attempt to multiply with overflow
3030

3131
error: any use of this value will cause an error
32-
--> $DIR/const-err-early.rs:6:1
32+
--> $DIR/const-err-early.rs:6:19
3333
|
3434
LL | pub const D: u8 = 42u8 - (42u8 + 1);
35-
| ^^^^^^^^^^^^^^^^^^-----------------^
35+
| ------------------^^^^^^^^^^^^^^^^^-
3636
| |
3737
| attempt to subtract with overflow
3838

3939
error: any use of this value will cause an error
40-
--> $DIR/const-err-early.rs:7:1
40+
--> $DIR/const-err-early.rs:7:19
4141
|
4242
LL | pub const E: u8 = [5u8][1];
43-
| ^^^^^^^^^^^^^^^^^^--------^
43+
| ------------------^^^^^^^^-
4444
| |
4545
| index out of bounds: the len is 1 but the index is 1
4646

src/test/ui/consts/const-err-multi.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: any use of this value will cause an error
2-
--> $DIR/const-err-multi.rs:3:1
2+
--> $DIR/const-err-multi.rs:3:19
33
|
44
LL | pub const A: i8 = -std::i8::MIN;
5-
| ^^^^^^^^^^^^^^^^^^-------------^
5+
| ------------------^^^^^^^^^^^^^-
66
| |
77
| attempt to negate with overflow
88
|
@@ -13,26 +13,26 @@ LL | #![deny(const_err)]
1313
| ^^^^^^^^^
1414

1515
error: any use of this value will cause an error
16-
--> $DIR/const-err-multi.rs:5:1
16+
--> $DIR/const-err-multi.rs:5:19
1717
|
1818
LL | pub const B: i8 = A;
19-
| ^^^^^^^^^^^^^^^^^^-^
19+
| ------------------^-
2020
| |
2121
| referenced constant has errors
2222

2323
error: any use of this value will cause an error
24-
--> $DIR/const-err-multi.rs:7:1
24+
--> $DIR/const-err-multi.rs:7:19
2525
|
2626
LL | pub const C: u8 = A as u8;
27-
| ^^^^^^^^^^^^^^^^^^-------^
27+
| ------------------^^^^^^^-
2828
| |
2929
| referenced constant has errors
3030

3131
error: any use of this value will cause an error
32-
--> $DIR/const-err-multi.rs:9:1
32+
--> $DIR/const-err-multi.rs:9:19
3333
|
3434
LL | pub const D: i8 = 50 - A;
35-
| ^^^^^^^^^^^^^^^^^^------^
35+
| ------------------^^^^^^-
3636
| |
3737
| referenced constant has errors
3838

src/test/ui/consts/const-err.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
warning: any use of this value will cause an error
2-
--> $DIR/const-err.rs:10:1
2+
--> $DIR/const-err.rs:10:17
33
|
44
LL | const FOO: u8 = [5u8][1];
5-
| ^^^^^^^^^^^^^^^^--------^
5+
| ----------------^^^^^^^^-
66
| |
77
| index out of bounds: the len is 1 but the index is 1
88
|

src/test/ui/consts/const-eval/conditional_array_execution.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
warning: any use of this value will cause an error
2-
--> $DIR/conditional_array_execution.rs:5:1
2+
--> $DIR/conditional_array_execution.rs:5:19
33
|
44
LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
5-
| ^^^^^^^^^^^^^^^^^^-----^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ------------------^^^^^---------------------------
66
| |
77
| attempt to subtract with overflow
88
|

0 commit comments

Comments
 (0)