Skip to content

Commit 992d1e4

Browse files
committed
Auto merge of #54241 - vi:suggest_with_applicability, r=estebank
Remove usages of span_suggestion without Applicability Use `Applicability::Unspecified` for all of them instead. Shall deprecations for the non-`_with_applicability` functions be added? Shall clippy be addressed somehow? r? @estebank
2 parents d16f27f + d0790c4 commit 992d1e4

File tree

22 files changed

+258
-96
lines changed

22 files changed

+258
-96
lines changed

src/librustc/infer/error_reporting/nice_region_error/named_anon_conflict.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use infer::error_reporting::nice_region_error::NiceRegionError;
1414
use ty;
1515
use util::common::ErrorReported;
16+
use errors::Applicability;
1617

1718
impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
1819
/// When given a `ConcreteFailure` for a function with arguments containing a named region and
@@ -111,13 +112,14 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
111112
E0621,
112113
"explicit lifetime required in {}",
113114
error_var
114-
).span_suggestion(
115+
).span_suggestion_with_applicability(
115116
new_ty_span,
116117
&format!("add explicit lifetime `{}` to {}", named, span_label_var),
117-
new_ty.to_string()
118+
new_ty.to_string(),
119+
Applicability::Unspecified,
118120
)
119-
.span_label(span, format!("lifetime `{}` required", named))
120-
.emit();
121+
.span_label(span, format!("lifetime `{}` required", named))
122+
.emit();
121123
return Some(ErrorReported);
122124
}
123125

src/librustc/infer/error_reporting/nice_region_error/static_impl_trait.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use infer::error_reporting::nice_region_error::NiceRegionError;
1414
use infer::lexical_region_resolve::RegionResolutionError;
1515
use ty::{BoundRegion, FreeRegion, RegionKind};
1616
use util::common::ErrorReported;
17+
use errors::Applicability;
1718

1819
impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
1920
/// Print the error message for lifetime errors when the return type is a static impl Trait.
@@ -61,14 +62,15 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
6162
_ => "'_".to_owned(),
6263
};
6364
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(return_sp) {
64-
err.span_suggestion(
65+
err.span_suggestion_with_applicability(
6566
return_sp,
6667
&format!(
6768
"you can add a constraint to the return type to make it last \
6869
less than `'static` and match {}",
6970
lifetime,
7071
),
7172
format!("{} + {}", snippet, lifetime_name),
73+
Applicability::Unspecified,
7274
);
7375
}
7476
err.emit();

src/librustc/session/mod.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use rustc_data_structures::base_n;
2828
use rustc_data_structures::sync::{self, Lrc, Lock, LockCell, OneThread, Once, RwLock};
2929

3030
use syntax::ast::NodeId;
31-
use errors::{self, DiagnosticBuilder, DiagnosticId};
31+
use errors::{self, DiagnosticBuilder, DiagnosticId, Applicability};
3232
use errors::emitter::{Emitter, EmitterWriter};
3333
use syntax::edition::Edition;
3434
use syntax::json::JsonEmitter;
@@ -431,8 +431,13 @@ impl Session {
431431
diag_builder.span_note(span, message);
432432
}
433433
DiagnosticBuilderMethod::SpanSuggestion(suggestion) => {
434-
let span = span_maybe.expect("span_suggestion needs a span");
435-
diag_builder.span_suggestion(span, message, suggestion);
434+
let span = span_maybe.expect("span_suggestion_* needs a span");
435+
diag_builder.span_suggestion_with_applicability(
436+
span,
437+
message,
438+
suggestion,
439+
Applicability::Unspecified,
440+
);
436441
}
437442
}
438443
}

src/librustc_borrowck/borrowck/mod.rs

+31-11
Original file line numberDiff line numberDiff line change
@@ -867,10 +867,20 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
867867
}) = cmt.cat {
868868
db.note(fn_closure_msg);
869869
} else {
870-
db.span_suggestion(sp, msg, suggestion);
870+
db.span_suggestion_with_applicability(
871+
sp,
872+
msg,
873+
suggestion,
874+
Applicability::Unspecified,
875+
);
871876
}
872877
} else {
873-
db.span_suggestion(sp, msg, suggestion);
878+
db.span_suggestion_with_applicability(
879+
sp,
880+
msg,
881+
suggestion,
882+
Applicability::Unspecified,
883+
);
874884
}
875885
}
876886
_ => {
@@ -1236,10 +1246,16 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
12361246
let let_span = self.tcx.hir.span(node_id);
12371247
let suggestion = suggest_ref_mut(self.tcx, let_span);
12381248
if let Some(replace_str) = suggestion {
1239-
db.span_suggestion(
1249+
db.span_suggestion_with_applicability(
12401250
let_span,
12411251
"use a mutable reference instead",
12421252
replace_str,
1253+
// I believe this can be machine applicable,
1254+
// but if there are multiple attempted uses of an immutable
1255+
// reference, I don't know how rustfix handles it, it might
1256+
// attempt fixing them multiple times.
1257+
// @estebank
1258+
Applicability::Unspecified,
12431259
);
12441260
}
12451261
}
@@ -1292,11 +1308,12 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
12921308
)) = ty.map(|t| &t.node)
12931309
{
12941310
let borrow_expr_id = self.tcx.hir.get_parent_node(borrowed_node_id);
1295-
db.span_suggestion(
1311+
db.span_suggestion_with_applicability(
12961312
self.tcx.hir.span(borrow_expr_id),
12971313
"consider removing the `&mut`, as it is an \
12981314
immutable binding to a mutable reference",
1299-
snippet
1315+
snippet,
1316+
Applicability::MachineApplicable,
13001317
);
13011318
} else {
13021319
db.span_suggestion_with_applicability(
@@ -1326,12 +1343,15 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
13261343
&cmt_path_or_string,
13271344
capture_span,
13281345
Origin::Ast)
1329-
.span_suggestion(err.span,
1330-
&format!("to force the closure to take ownership of {} \
1331-
(and any other referenced variables), \
1332-
use the `move` keyword",
1333-
cmt_path_or_string),
1334-
suggestion)
1346+
.span_suggestion_with_applicability(
1347+
err.span,
1348+
&format!("to force the closure to take ownership of {} \
1349+
(and any other referenced variables), \
1350+
use the `move` keyword",
1351+
cmt_path_or_string),
1352+
suggestion,
1353+
Applicability::MachineApplicable,
1354+
)
13351355
.emit();
13361356
self.signal_error();
13371357
}

src/librustc_errors/diagnostic.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ impl Diagnostic {
232232
/// inline it will only show the text message and not the text.
233233
///
234234
/// See `CodeSuggestion` for more information.
235+
#[deprecated(note = "Use `span_suggestion_short_with_applicability`")]
235236
pub fn span_suggestion_short(&mut self, sp: Span, msg: &str, suggestion: String) -> &mut Self {
236237
self.suggestions.push(CodeSuggestion {
237238
substitutions: vec![Substitution {
@@ -263,6 +264,7 @@ impl Diagnostic {
263264
/// * may contain a name of a function, variable or type, but not whole expressions
264265
///
265266
/// See `CodeSuggestion` for more information.
267+
#[deprecated(note = "Use `span_suggestion_with_applicability`")]
266268
pub fn span_suggestion(&mut self, sp: Span, msg: &str, suggestion: String) -> &mut Self {
267269
self.suggestions.push(CodeSuggestion {
268270
substitutions: vec![Substitution {
@@ -278,10 +280,11 @@ impl Diagnostic {
278280
self
279281
}
280282

281-
pub fn multipart_suggestion(
283+
pub fn multipart_suggestion_with_applicability(
282284
&mut self,
283285
msg: &str,
284286
suggestion: Vec<(Span, String)>,
287+
applicability: Applicability,
285288
) -> &mut Self {
286289
self.suggestions.push(CodeSuggestion {
287290
substitutions: vec![Substitution {
@@ -292,12 +295,26 @@ impl Diagnostic {
292295
}],
293296
msg: msg.to_owned(),
294297
show_code_when_inline: true,
295-
applicability: Applicability::Unspecified,
298+
applicability,
296299
});
297300
self
298301
}
299302

303+
#[deprecated(note = "Use `multipart_suggestion_with_applicability`")]
304+
pub fn multipart_suggestion(
305+
&mut self,
306+
msg: &str,
307+
suggestion: Vec<(Span, String)>,
308+
) -> &mut Self {
309+
self.multipart_suggestion_with_applicability(
310+
msg,
311+
suggestion,
312+
Applicability::Unspecified,
313+
)
314+
}
315+
300316
/// Prints out a message with multiple suggested edits of the code.
317+
#[deprecated(note = "Use `span_suggestions_with_applicability`")]
301318
pub fn span_suggestions(&mut self, sp: Span, msg: &str, suggestions: Vec<String>) -> &mut Self {
302319
self.suggestions.push(CodeSuggestion {
303320
substitutions: suggestions.into_iter().map(|snippet| Substitution {

src/librustc_errors/diagnostic_builder.rs

+53-21
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,31 @@ pub struct DiagnosticBuilder<'a> {
4343
/// it easy to declare such methods on the builder.
4444
macro_rules! forward {
4545
// Forward pattern for &self -> &Self
46-
(pub fn $n:ident(&self, $($name:ident: $ty:ty),*) -> &Self) => {
46+
(pub fn $n:ident(&self, $($name:ident: $ty:ty),* $(,)*) -> &Self) => {
4747
pub fn $n(&self, $($name: $ty),*) -> &Self {
48+
#[allow(deprecated)]
4849
self.diagnostic.$n($($name),*);
4950
self
5051
}
5152
};
5253

5354
// Forward pattern for &mut self -> &mut Self
54-
(pub fn $n:ident(&mut self, $($name:ident: $ty:ty),*) -> &mut Self) => {
55+
(pub fn $n:ident(&mut self, $($name:ident: $ty:ty),* $(,)*) -> &mut Self) => {
5556
pub fn $n(&mut self, $($name: $ty),*) -> &mut Self {
57+
#[allow(deprecated)]
5658
self.diagnostic.$n($($name),*);
5759
self
5860
}
5961
};
6062

6163
// Forward pattern for &mut self -> &mut Self, with S: Into<MultiSpan>
6264
// type parameter. No obvious way to make this more generic.
63-
(pub fn $n:ident<S: Into<MultiSpan>>(&mut self, $($name:ident: $ty:ty),*) -> &mut Self) => {
65+
(pub fn $n:ident<S: Into<MultiSpan>>(
66+
&mut self,
67+
$($name:ident: $ty:ty),*
68+
$(,)*) -> &mut Self) => {
6469
pub fn $n<S: Into<MultiSpan>>(&mut self, $($name: $ty),*) -> &mut Self {
70+
#[allow(deprecated)]
6571
self.diagnostic.$n($($name),*);
6672
self
6773
}
@@ -157,49 +163,75 @@ impl<'a> DiagnosticBuilder<'a> {
157163
forward!(pub fn note_expected_found(&mut self,
158164
label: &dyn fmt::Display,
159165
expected: DiagnosticStyledString,
160-
found: DiagnosticStyledString)
161-
-> &mut Self);
166+
found: DiagnosticStyledString,
167+
) -> &mut Self);
162168

163169
forward!(pub fn note_expected_found_extra(&mut self,
164170
label: &dyn fmt::Display,
165171
expected: DiagnosticStyledString,
166172
found: DiagnosticStyledString,
167173
expected_extra: &dyn fmt::Display,
168-
found_extra: &dyn fmt::Display)
169-
-> &mut Self);
174+
found_extra: &dyn fmt::Display,
175+
) -> &mut Self);
170176

171177
forward!(pub fn note(&mut self, msg: &str) -> &mut Self);
172178
forward!(pub fn span_note<S: Into<MultiSpan>>(&mut self,
173179
sp: S,
174-
msg: &str)
175-
-> &mut Self);
180+
msg: &str,
181+
) -> &mut Self);
176182
forward!(pub fn warn(&mut self, msg: &str) -> &mut Self);
177183
forward!(pub fn span_warn<S: Into<MultiSpan>>(&mut self, sp: S, msg: &str) -> &mut Self);
178184
forward!(pub fn help(&mut self , msg: &str) -> &mut Self);
179185
forward!(pub fn span_help<S: Into<MultiSpan>>(&mut self,
180186
sp: S,
181-
msg: &str)
182-
-> &mut Self);
183-
forward!(pub fn span_suggestion_short(&mut self,
184-
sp: Span,
185-
msg: &str,
186-
suggestion: String)
187-
-> &mut Self);
187+
msg: &str,
188+
) -> &mut Self);
189+
190+
#[deprecated(note = "Use `span_suggestion_short_with_applicability`")]
191+
forward!(pub fn span_suggestion_short(
192+
&mut self,
193+
sp: Span,
194+
msg: &str,
195+
suggestion: String,
196+
) -> &mut Self);
197+
198+
#[deprecated(note = "Use `multipart_suggestion_with_applicability`")]
188199
forward!(pub fn multipart_suggestion(
189200
&mut self,
190201
msg: &str,
191-
suggestion: Vec<(Span, String)>
202+
suggestion: Vec<(Span, String)>,
192203
) -> &mut Self);
204+
205+
#[deprecated(note = "Use `span_suggestion_with_applicability`")]
193206
forward!(pub fn span_suggestion(&mut self,
194207
sp: Span,
195208
msg: &str,
196-
suggestion: String)
197-
-> &mut Self);
209+
suggestion: String,
210+
) -> &mut Self);
211+
212+
#[deprecated(note = "Use `span_suggestions_with_applicability`")]
198213
forward!(pub fn span_suggestions(&mut self,
199214
sp: Span,
200215
msg: &str,
201-
suggestions: Vec<String>)
202-
-> &mut Self);
216+
suggestions: Vec<String>,
217+
) -> &mut Self);
218+
219+
pub fn multipart_suggestion_with_applicability(&mut self,
220+
msg: &str,
221+
suggestion: Vec<(Span, String)>,
222+
applicability: Applicability,
223+
) -> &mut Self {
224+
if !self.allow_suggestions {
225+
return self
226+
}
227+
self.diagnostic.multipart_suggestion_with_applicability(
228+
msg,
229+
suggestion,
230+
applicability,
231+
);
232+
self
233+
}
234+
203235
pub fn span_suggestion_with_applicability(&mut self,
204236
sp: Span,
205237
msg: &str,

src/librustc_mir/borrow_check/move_errors.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use core::unicode::property::Pattern_White_Space;
1212
use rustc::mir::*;
1313
use rustc::ty;
14-
use rustc_errors::DiagnosticBuilder;
14+
use rustc_errors::{DiagnosticBuilder,Applicability};
1515
use syntax_pos::Span;
1616

1717
use borrow_check::MirBorrowckCtxt;
@@ -350,16 +350,18 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
350350
// expressions `a[b]`, which roughly desugar to
351351
// `*Index::index(&a, b)` or
352352
// `*IndexMut::index_mut(&mut a, b)`.
353-
err.span_suggestion(
353+
err.span_suggestion_with_applicability(
354354
span,
355355
"consider removing the `*`",
356356
snippet[1..].to_owned(),
357+
Applicability::Unspecified,
357358
);
358359
} else {
359-
err.span_suggestion(
360+
err.span_suggestion_with_applicability(
360361
span,
361362
"consider borrowing here",
362363
format!("&{}", snippet),
364+
Applicability::Unspecified,
363365
);
364366
}
365367

@@ -420,10 +422,11 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
420422
suggestions.sort_unstable_by_key(|&(span, _, _)| span);
421423
suggestions.dedup_by_key(|&mut (span, _, _)| span);
422424
for (span, to_remove, suggestion) in suggestions {
423-
err.span_suggestion(
425+
err.span_suggestion_with_applicability(
424426
span,
425427
&format!("consider removing the `{}`", to_remove),
426-
suggestion
428+
suggestion,
429+
Applicability::MachineApplicable,
427430
);
428431
}
429432
}

0 commit comments

Comments
 (0)