Skip to content

Commit ae89abd

Browse files
Rollup merge of rust-lang#59084 - estebank:diagnostic-spans, r=davidtwco
Tweak some diagnostic spans
2 parents b76fa33 + d1656f1 commit ae89abd

File tree

80 files changed

+500
-543
lines changed

Some content is hidden

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

80 files changed

+500
-543
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

+21-14
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,26 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
6060
}
6161
}
6262

63-
pub fn report_method_error<'b>(&self,
64-
span: Span,
65-
rcvr_ty: Ty<'tcx>,
66-
item_name: ast::Ident,
67-
source: SelfSource<'b>,
68-
error: MethodError<'tcx>,
69-
args: Option<&'gcx [hir::Expr]>) {
63+
pub fn report_method_error<'b>(
64+
&self,
65+
span: Span,
66+
rcvr_ty: Ty<'tcx>,
67+
item_name: ast::Ident,
68+
source: SelfSource<'b>,
69+
error: MethodError<'tcx>,
70+
args: Option<&'gcx [hir::Expr]>,
71+
) {
72+
let mut span = span;
7073
// Avoid suggestions when we don't know what's going on.
7174
if rcvr_ty.references_error() {
7275
return;
7376
}
7477

75-
let report_candidates = |err: &mut DiagnosticBuilder<'_>,
76-
mut sources: Vec<CandidateSource>| {
78+
let report_candidates = |
79+
span: Span,
80+
err: &mut DiagnosticBuilder<'_>,
81+
mut sources: Vec<CandidateSource>,
82+
| {
7783
sources.sort();
7884
sources.dedup();
7985
// Dynamic limit to avoid hiding just one candidate, which is silly.
@@ -291,9 +297,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
291297
err.emit();
292298
return;
293299
} else {
300+
span = item_name.span;
294301
let mut err = struct_span_err!(
295302
tcx.sess,
296-
item_name.span,
303+
span,
297304
E0599,
298305
"no {} named `{}` found for type `{}` in the current scope",
299306
item_kind,
@@ -303,7 +310,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
303310
if let Some(suggestion) = suggestion {
304311
// enum variant
305312
err.span_suggestion(
306-
item_name.span,
313+
span,
307314
"did you mean",
308315
suggestion.to_string(),
309316
Applicability::MaybeIncorrect,
@@ -414,9 +421,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
414421
self.ty_to_string(actual), item_name));
415422
}
416423

417-
report_candidates(&mut err, static_sources);
424+
report_candidates(span, &mut err, static_sources);
418425
} else if static_sources.len() > 1 {
419-
report_candidates(&mut err, static_sources);
426+
report_candidates(span, &mut err, static_sources);
420427
}
421428

422429
if !unsatisfied_predicates.is_empty() {
@@ -461,7 +468,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
461468
"multiple applicable items in scope");
462469
err.span_label(span, format!("multiple `{}` found", item_name));
463470

464-
report_candidates(&mut err, sources);
471+
report_candidates(span, &mut err, sources);
465472
err.emit();
466473
}
467474

src/libsyntax/parse/parser.rs

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

55945594
if !negative_bounds.is_empty() || was_negative {
55955595
let plural = negative_bounds.len() > 1;
5596-
let mut err = self.struct_span_err(negative_bounds,
5597-
"negative trait bounds are not supported");
5596+
let last_span = negative_bounds.last().map(|sp| *sp);
5597+
let mut err = self.struct_span_err(
5598+
negative_bounds,
5599+
"negative trait bounds are not supported",
5600+
);
5601+
if let Some(sp) = last_span {
5602+
err.span_label(sp, "negative trait bounds are not supported");
5603+
}
55985604
if let Some(bound_list) = colon_span {
55995605
let bound_list = bound_list.to(self.prev_span);
56005606
let mut new_bound_list = String::new();
@@ -5607,11 +5613,12 @@ impl<'a> Parser<'a> {
56075613
}
56085614
new_bound_list = new_bound_list.replacen(" +", ":", 1);
56095615
}
5610-
err.span_suggestion_short(bound_list,
5611-
&format!("remove the trait bound{}",
5612-
if plural { "s" } else { "" }),
5613-
new_bound_list,
5614-
Applicability::MachineApplicable);
5616+
err.span_suggestion_hidden(
5617+
bound_list,
5618+
&format!("remove the trait bound{}", if plural { "s" } else { "" }),
5619+
new_bound_list,
5620+
Applicability::MachineApplicable,
5621+
);
56155622
}
56165623
err.emit();
56175624
}

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)