Skip to content

Commit

Permalink
Auto merge of #50724 - zackmdavis:applicability_rush, r=Manishearth
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed May 28, 2018
2 parents 68e0e58 + 98a0429 commit 16cd84e
Show file tree
Hide file tree
Showing 35 changed files with 447 additions and 117 deletions.
7 changes: 5 additions & 2 deletions src/librustc/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ use ty::{self, Region, Ty, TyCtxt, TypeFoldable, TypeVariants};
use ty::error::TypeError;
use syntax::ast::DUMMY_NODE_ID;
use syntax_pos::{Pos, Span};
use errors::{DiagnosticBuilder, DiagnosticStyledString};
use errors::{Applicability, DiagnosticBuilder, DiagnosticStyledString};

use rustc_data_structures::indexed_vec::Idx;

Expand Down Expand Up @@ -1097,7 +1097,10 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
if let Some((sp, has_lifetimes)) = type_param_span {
let tail = if has_lifetimes { " + " } else { "" };
let suggestion = format!("{}: {}{}", bound_kind, sub, tail);
err.span_suggestion_short(sp, consider, suggestion);
err.span_suggestion_short_with_applicability(
sp, consider, suggestion,
Applicability::MaybeIncorrect // Issue #41966
);
} else {
err.help(consider);
}
Expand Down
7 changes: 4 additions & 3 deletions src/librustc/lint/levels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use std::cmp;

use errors::DiagnosticBuilder;
use errors::{Applicability, DiagnosticBuilder};
use hir::HirId;
use ich::StableHashingContext;
use lint::builtin;
Expand Down Expand Up @@ -265,10 +265,11 @@ impl<'a> LintLevelsBuilder<'a> {
store.check_lint_name(&name_lower) {
db.emit();
} else {
db.span_suggestion(
db.span_suggestion_with_applicability(
li.span,
"lowercase the lint name",
name_lower
name_lower,
Applicability::MachineApplicable
).emit();
}
} else {
Expand Down
13 changes: 9 additions & 4 deletions src/librustc/middle/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ use self::VarKind::*;
use hir::def::*;
use ty::{self, TyCtxt};
use lint;
use errors::Applicability;
use util::nodemap::{NodeMap, NodeSet};

use std::collections::VecDeque;
Expand Down Expand Up @@ -1535,11 +1536,15 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
let mut err = self.ir.tcx
.struct_span_lint_node(lint::builtin::UNUSED_VARIABLES, id, sp, &msg);
if self.ir.variable_is_shorthand(var) {
err.span_suggestion(sp, "try ignoring the field",
format!("{}: _", name));
err.span_suggestion_with_applicability(sp, "try ignoring the field",
format!("{}: _", name),
Applicability::MachineApplicable);
} else {
err.span_suggestion_short(sp, &suggest_underscore_msg,
format!("_{}", name));
err.span_suggestion_short_with_applicability(
sp, &suggest_underscore_msg,
format!("_{}", name),
Applicability::MachineApplicable,
);
}
err.emit()
}
Expand Down
35 changes: 22 additions & 13 deletions src/librustc/traits/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use super::{
Overflow,
};

use errors::DiagnosticBuilder;
use errors::{Applicability, DiagnosticBuilder};
use hir;
use hir::def_id::DefId;
use infer::{self, InferCtxt};
Expand Down Expand Up @@ -852,9 +852,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
if let Some(ref expr) = local.init {
if let hir::ExprIndex(_, _) = expr.node {
if let Ok(snippet) = self.tcx.sess.codemap().span_to_snippet(expr.span) {
err.span_suggestion(expr.span,
"consider borrowing here",
format!("&{}", snippet));
err.span_suggestion_with_applicability(
expr.span,
"consider borrowing here",
format!("&{}", snippet),
Applicability::MachineApplicable
);
}
}
}
Expand Down Expand Up @@ -897,7 +900,9 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
let format_str = format!("consider removing {} leading `&`-references",
remove_refs);

err.span_suggestion_short(sp, &format_str, String::from(""));
err.span_suggestion_short_with_applicability(
sp, &format_str, String::from(""), Applicability::MachineApplicable
);
break;
}
} else {
Expand Down Expand Up @@ -1042,10 +1047,11 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
let sugg = fields.iter()
.map(|(name, _)| name.to_owned())
.collect::<Vec<String>>().join(", ");
err.span_suggestion(found_span,
"change the closure to take multiple arguments instead of \
a single tuple",
format!("|{}|", sugg));
err.span_suggestion_with_applicability(found_span,
"change the closure to take multiple \
arguments instead of a single tuple",
format!("|{}|", sugg),
Applicability::MachineApplicable);
}
}
if let &[ArgKind::Tuple(_, ref fields)] = &expected_args[..] {
Expand Down Expand Up @@ -1073,10 +1079,13 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
"".to_owned()
},
);
err.span_suggestion(found_span,
"change the closure to accept a tuple instead of \
individual arguments",
sugg);
err.span_suggestion_with_applicability(
found_span,
"change the closure to accept a tuple instead of \
individual arguments",
sugg,
Applicability::MachineApplicable
);
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/librustc_errors/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,23 @@ impl Diagnostic {
self
}

pub fn span_suggestion_short_with_applicability(
&mut self, sp: Span, msg: &str, suggestion: String, applicability: Applicability
) -> &mut Self {
self.suggestions.push(CodeSuggestion {
substitutions: vec![Substitution {
parts: vec![SubstitutionPart {
snippet: suggestion,
span: sp,
}],
}],
msg: msg.to_owned(),
show_code_when_inline: false,
applicability: applicability,
});
self
}

pub fn set_span<S: Into<MultiSpan>>(&mut self, sp: S) -> &mut Self {
self.span = sp.into();
self
Expand Down
6 changes: 6 additions & 0 deletions src/librustc_errors/diagnostic_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,12 @@ impl<'a> DiagnosticBuilder<'a> {
suggestions: Vec<String>,
applicability: Applicability)
-> &mut Self);
forward!(pub fn span_suggestion_short_with_applicability(&mut self,
sp: Span,
msg: &str,
suggestion: String,
applicability: Applicability)
-> &mut Self);
forward!(pub fn set_span<S: Into<MultiSpan>>(&mut self, sp: S) -> &mut Self);
forward!(pub fn code(&mut self, s: DiagnosticId) -> &mut Self);

Expand Down
20 changes: 13 additions & 7 deletions src/libsyntax/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use ast::{MetaItem, MetaItemKind, NestedMetaItem, NestedMetaItemKind};
use ast::{Lit, LitKind, Expr, ExprKind, Item, Local, Stmt, StmtKind};
use codemap::{BytePos, Spanned, respan, dummy_spanned};
use syntax_pos::Span;
use errors::Handler;
use errors::{Applicability, Handler};
use feature_gate::{Features, GatedCfg};
use parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration};
use parse::parser::Parser;
Expand Down Expand Up @@ -1067,14 +1067,20 @@ pub fn find_repr_attrs(diagnostic: &Handler, attr: &Attribute) -> Vec<ReprAttr>
"incorrect `repr(align)` attribute format");
match value.node {
ast::LitKind::Int(int, ast::LitIntType::Unsuffixed) => {
err.span_suggestion(item.span,
"use parentheses instead",
format!("align({})", int));
err.span_suggestion_with_applicability(
item.span,
"use parentheses instead",
format!("align({})", int),
Applicability::MachineApplicable
);
}
ast::LitKind::Str(s, _) => {
err.span_suggestion(item.span,
"use parentheses instead",
format!("align({})", s));
err.span_suggestion_with_applicability(
item.span,
"use parentheses instead",
format!("align({})", s),
Applicability::MachineApplicable
);
}
_ => {}
}
Expand Down
8 changes: 6 additions & 2 deletions src/libsyntax/ext/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use ast::{MacStmtStyle, StmtKind, ItemKind};
use attr::{self, HasAttrs};
use codemap::{ExpnInfo, NameAndSpan, MacroBang, MacroAttribute, dummy_spanned, respan};
use config::{is_test_or_bench, StripUnconfigured};
use errors::FatalError;
use errors::{Applicability, FatalError};
use ext::base::*;
use ext::derive::{add_derived_markers, collect_derives};
use ext::hygiene::{self, Mark, SyntaxContext};
Expand Down Expand Up @@ -331,7 +331,11 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
let trait_list = traits.iter()
.map(|t| format!("{}", t)).collect::<Vec<_>>();
let suggestion = format!("#[derive({})]", trait_list.join(", "));
err.span_suggestion(span, "try an outer attribute", suggestion);
err.span_suggestion_with_applicability(
span, "try an outer attribute", suggestion,
// We don't 𝑘𝑛𝑜𝑤 that the following item is an ADT
Applicability::MaybeIncorrect
);
}
err.emit();
}
Expand Down
13 changes: 7 additions & 6 deletions src/libsyntax/parse/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use ast::{self, Ident};
use syntax_pos::{self, BytePos, CharPos, Pos, Span, NO_EXPANSION};
use codemap::{CodeMap, FilePathMapping};
use errors::{FatalError, DiagnosticBuilder};
use errors::{Applicability, FatalError, DiagnosticBuilder};
use parse::{token, ParseSess};
use str::char_at;
use symbol::{Symbol, keywords};
Expand Down Expand Up @@ -1379,11 +1379,12 @@ impl<'a> StringReader<'a> {
self.sess.span_diagnostic
.struct_span_err(span,
"character literal may only contain one codepoint")
.span_suggestion(span,
"if you meant to write a `str` literal, \
use double quotes",
format!("\"{}\"", &self.src[start..end]))
.emit();
.span_suggestion_with_applicability(
span,
"if you meant to write a `str` literal, use double quotes",
format!("\"{}\"", &self.src[start..end]),
Applicability::MachineApplicable
).emit();
return Ok(token::Literal(token::Str_(Symbol::intern("??")), None))
}
if self.ch_is('\n') || self.is_eof() || self.ch_is('/') {
Expand Down
Loading

0 comments on commit 16cd84e

Please sign in to comment.