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

Lines changed: 13 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 21 additions & 14 deletions
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

Lines changed: 14 additions & 7 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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
|

0 commit comments

Comments
 (0)