Skip to content

Commit 16cd84e

Browse files
committed
Auto merge of #50724 - zackmdavis:applicability_rush, r=Manishearth
add suggestion applicabilities to librustc and libsyntax A down payment on #50723. Interested in feedback on whether my `MaybeIncorrect` vs. `MachineApplicable` judgement calls are well-calibrated (and that we have a consensus on what this means). r? @Manishearth cc @killercup @estebank
2 parents 68e0e58 + 98a0429 commit 16cd84e

35 files changed

+447
-117
lines changed

Diff for: src/librustc/infer/error_reporting/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ use ty::{self, Region, Ty, TyCtxt, TypeFoldable, TypeVariants};
7070
use ty::error::TypeError;
7171
use syntax::ast::DUMMY_NODE_ID;
7272
use syntax_pos::{Pos, Span};
73-
use errors::{DiagnosticBuilder, DiagnosticStyledString};
73+
use errors::{Applicability, DiagnosticBuilder, DiagnosticStyledString};
7474

7575
use rustc_data_structures::indexed_vec::Idx;
7676

@@ -1097,7 +1097,10 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
10971097
if let Some((sp, has_lifetimes)) = type_param_span {
10981098
let tail = if has_lifetimes { " + " } else { "" };
10991099
let suggestion = format!("{}: {}{}", bound_kind, sub, tail);
1100-
err.span_suggestion_short(sp, consider, suggestion);
1100+
err.span_suggestion_short_with_applicability(
1101+
sp, consider, suggestion,
1102+
Applicability::MaybeIncorrect // Issue #41966
1103+
);
11011104
} else {
11021105
err.help(consider);
11031106
}

Diff for: src/librustc/lint/levels.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use std::cmp;
1212

13-
use errors::DiagnosticBuilder;
13+
use errors::{Applicability, DiagnosticBuilder};
1414
use hir::HirId;
1515
use ich::StableHashingContext;
1616
use lint::builtin;
@@ -265,10 +265,11 @@ impl<'a> LintLevelsBuilder<'a> {
265265
store.check_lint_name(&name_lower) {
266266
db.emit();
267267
} else {
268-
db.span_suggestion(
268+
db.span_suggestion_with_applicability(
269269
li.span,
270270
"lowercase the lint name",
271-
name_lower
271+
name_lower,
272+
Applicability::MachineApplicable
272273
).emit();
273274
}
274275
} else {

Diff for: src/librustc/middle/liveness.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ use self::VarKind::*;
109109
use hir::def::*;
110110
use ty::{self, TyCtxt};
111111
use lint;
112+
use errors::Applicability;
112113
use util::nodemap::{NodeMap, NodeSet};
113114

114115
use std::collections::VecDeque;
@@ -1535,11 +1536,15 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
15351536
let mut err = self.ir.tcx
15361537
.struct_span_lint_node(lint::builtin::UNUSED_VARIABLES, id, sp, &msg);
15371538
if self.ir.variable_is_shorthand(var) {
1538-
err.span_suggestion(sp, "try ignoring the field",
1539-
format!("{}: _", name));
1539+
err.span_suggestion_with_applicability(sp, "try ignoring the field",
1540+
format!("{}: _", name),
1541+
Applicability::MachineApplicable);
15401542
} else {
1541-
err.span_suggestion_short(sp, &suggest_underscore_msg,
1542-
format!("_{}", name));
1543+
err.span_suggestion_short_with_applicability(
1544+
sp, &suggest_underscore_msg,
1545+
format!("_{}", name),
1546+
Applicability::MachineApplicable,
1547+
);
15431548
}
15441549
err.emit()
15451550
}

Diff for: src/librustc/traits/error_reporting.rs

+22-13
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use super::{
2727
Overflow,
2828
};
2929

30-
use errors::DiagnosticBuilder;
30+
use errors::{Applicability, DiagnosticBuilder};
3131
use hir;
3232
use hir::def_id::DefId;
3333
use infer::{self, InferCtxt};
@@ -852,9 +852,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
852852
if let Some(ref expr) = local.init {
853853
if let hir::ExprIndex(_, _) = expr.node {
854854
if let Ok(snippet) = self.tcx.sess.codemap().span_to_snippet(expr.span) {
855-
err.span_suggestion(expr.span,
856-
"consider borrowing here",
857-
format!("&{}", snippet));
855+
err.span_suggestion_with_applicability(
856+
expr.span,
857+
"consider borrowing here",
858+
format!("&{}", snippet),
859+
Applicability::MachineApplicable
860+
);
858861
}
859862
}
860863
}
@@ -897,7 +900,9 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
897900
let format_str = format!("consider removing {} leading `&`-references",
898901
remove_refs);
899902

900-
err.span_suggestion_short(sp, &format_str, String::from(""));
903+
err.span_suggestion_short_with_applicability(
904+
sp, &format_str, String::from(""), Applicability::MachineApplicable
905+
);
901906
break;
902907
}
903908
} else {
@@ -1042,10 +1047,11 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
10421047
let sugg = fields.iter()
10431048
.map(|(name, _)| name.to_owned())
10441049
.collect::<Vec<String>>().join(", ");
1045-
err.span_suggestion(found_span,
1046-
"change the closure to take multiple arguments instead of \
1047-
a single tuple",
1048-
format!("|{}|", sugg));
1050+
err.span_suggestion_with_applicability(found_span,
1051+
"change the closure to take multiple \
1052+
arguments instead of a single tuple",
1053+
format!("|{}|", sugg),
1054+
Applicability::MachineApplicable);
10491055
}
10501056
}
10511057
if let &[ArgKind::Tuple(_, ref fields)] = &expected_args[..] {
@@ -1073,10 +1079,13 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
10731079
"".to_owned()
10741080
},
10751081
);
1076-
err.span_suggestion(found_span,
1077-
"change the closure to accept a tuple instead of \
1078-
individual arguments",
1079-
sugg);
1082+
err.span_suggestion_with_applicability(
1083+
found_span,
1084+
"change the closure to accept a tuple instead of \
1085+
individual arguments",
1086+
sugg,
1087+
Applicability::MachineApplicable
1088+
);
10801089
}
10811090
}
10821091
}

Diff for: src/librustc_errors/diagnostic.rs

+17
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,23 @@ impl Diagnostic {
330330
self
331331
}
332332

333+
pub fn span_suggestion_short_with_applicability(
334+
&mut self, sp: Span, msg: &str, suggestion: String, applicability: Applicability
335+
) -> &mut Self {
336+
self.suggestions.push(CodeSuggestion {
337+
substitutions: vec![Substitution {
338+
parts: vec![SubstitutionPart {
339+
snippet: suggestion,
340+
span: sp,
341+
}],
342+
}],
343+
msg: msg.to_owned(),
344+
show_code_when_inline: false,
345+
applicability: applicability,
346+
});
347+
self
348+
}
349+
333350
pub fn set_span<S: Into<MultiSpan>>(&mut self, sp: S) -> &mut Self {
334351
self.span = sp.into();
335352
self

Diff for: src/librustc_errors/diagnostic_builder.rs

+6
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@ impl<'a> DiagnosticBuilder<'a> {
205205
suggestions: Vec<String>,
206206
applicability: Applicability)
207207
-> &mut Self);
208+
forward!(pub fn span_suggestion_short_with_applicability(&mut self,
209+
sp: Span,
210+
msg: &str,
211+
suggestion: String,
212+
applicability: Applicability)
213+
-> &mut Self);
208214
forward!(pub fn set_span<S: Into<MultiSpan>>(&mut self, sp: S) -> &mut Self);
209215
forward!(pub fn code(&mut self, s: DiagnosticId) -> &mut Self);
210216

Diff for: src/libsyntax/attr.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use ast::{MetaItem, MetaItemKind, NestedMetaItem, NestedMetaItemKind};
2020
use ast::{Lit, LitKind, Expr, ExprKind, Item, Local, Stmt, StmtKind};
2121
use codemap::{BytePos, Spanned, respan, dummy_spanned};
2222
use syntax_pos::Span;
23-
use errors::Handler;
23+
use errors::{Applicability, Handler};
2424
use feature_gate::{Features, GatedCfg};
2525
use parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration};
2626
use parse::parser::Parser;
@@ -1067,14 +1067,20 @@ pub fn find_repr_attrs(diagnostic: &Handler, attr: &Attribute) -> Vec<ReprAttr>
10671067
"incorrect `repr(align)` attribute format");
10681068
match value.node {
10691069
ast::LitKind::Int(int, ast::LitIntType::Unsuffixed) => {
1070-
err.span_suggestion(item.span,
1071-
"use parentheses instead",
1072-
format!("align({})", int));
1070+
err.span_suggestion_with_applicability(
1071+
item.span,
1072+
"use parentheses instead",
1073+
format!("align({})", int),
1074+
Applicability::MachineApplicable
1075+
);
10731076
}
10741077
ast::LitKind::Str(s, _) => {
1075-
err.span_suggestion(item.span,
1076-
"use parentheses instead",
1077-
format!("align({})", s));
1078+
err.span_suggestion_with_applicability(
1079+
item.span,
1080+
"use parentheses instead",
1081+
format!("align({})", s),
1082+
Applicability::MachineApplicable
1083+
);
10781084
}
10791085
_ => {}
10801086
}

Diff for: src/libsyntax/ext/expand.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use ast::{MacStmtStyle, StmtKind, ItemKind};
1313
use attr::{self, HasAttrs};
1414
use codemap::{ExpnInfo, NameAndSpan, MacroBang, MacroAttribute, dummy_spanned, respan};
1515
use config::{is_test_or_bench, StripUnconfigured};
16-
use errors::FatalError;
16+
use errors::{Applicability, FatalError};
1717
use ext::base::*;
1818
use ext::derive::{add_derived_markers, collect_derives};
1919
use ext::hygiene::{self, Mark, SyntaxContext};
@@ -331,7 +331,11 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
331331
let trait_list = traits.iter()
332332
.map(|t| format!("{}", t)).collect::<Vec<_>>();
333333
let suggestion = format!("#[derive({})]", trait_list.join(", "));
334-
err.span_suggestion(span, "try an outer attribute", suggestion);
334+
err.span_suggestion_with_applicability(
335+
span, "try an outer attribute", suggestion,
336+
// We don't 𝑘𝑛𝑜𝑤 that the following item is an ADT
337+
Applicability::MaybeIncorrect
338+
);
335339
}
336340
err.emit();
337341
}

Diff for: src/libsyntax/parse/lexer/mod.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use ast::{self, Ident};
1212
use syntax_pos::{self, BytePos, CharPos, Pos, Span, NO_EXPANSION};
1313
use codemap::{CodeMap, FilePathMapping};
14-
use errors::{FatalError, DiagnosticBuilder};
14+
use errors::{Applicability, FatalError, DiagnosticBuilder};
1515
use parse::{token, ParseSess};
1616
use str::char_at;
1717
use symbol::{Symbol, keywords};
@@ -1379,11 +1379,12 @@ impl<'a> StringReader<'a> {
13791379
self.sess.span_diagnostic
13801380
.struct_span_err(span,
13811381
"character literal may only contain one codepoint")
1382-
.span_suggestion(span,
1383-
"if you meant to write a `str` literal, \
1384-
use double quotes",
1385-
format!("\"{}\"", &self.src[start..end]))
1386-
.emit();
1382+
.span_suggestion_with_applicability(
1383+
span,
1384+
"if you meant to write a `str` literal, use double quotes",
1385+
format!("\"{}\"", &self.src[start..end]),
1386+
Applicability::MachineApplicable
1387+
).emit();
13871388
return Ok(token::Literal(token::Str_(Symbol::intern("??")), None))
13881389
}
13891390
if self.ch_is('\n') || self.is_eof() || self.ch_is('/') {

0 commit comments

Comments
 (0)