call span_suggestion with applicability

This commit is contained in:
Joseph Post 2018-08-25 22:47:46 -05:00
parent d95f078f0a
commit 1f421d6456
3 changed files with 60 additions and 27 deletions

View file

@ -69,7 +69,7 @@ use syntax::feature_gate::{feature_err, GateIssue};
use syntax::ptr::P; use syntax::ptr::P;
use syntax_pos::{Span, DUMMY_SP, MultiSpan}; use syntax_pos::{Span, DUMMY_SP, MultiSpan};
use errors::{DiagnosticBuilder, DiagnosticId}; use errors::{Applicability, DiagnosticBuilder, DiagnosticId};
use std::cell::{Cell, RefCell}; use std::cell::{Cell, RefCell};
use std::cmp; use std::cmp;
@ -220,9 +220,12 @@ fn resolve_struct_error<'sess, 'a>(resolver: &'sess Resolver,
let sugg_msg = "try using a local type parameter instead"; let sugg_msg = "try using a local type parameter instead";
if let Some((sugg_span, new_snippet)) = cm.generate_local_type_param_snippet(span) { if let Some((sugg_span, new_snippet)) = cm.generate_local_type_param_snippet(span) {
// Suggest the modification to the user // Suggest the modification to the user
err.span_suggestion(sugg_span, err.span_suggestion_with_applicability(
sugg_msg, sugg_span,
new_snippet); sugg_msg,
new_snippet,
Applicability::MachineApplicable,
);
} else if let Some(sp) = cm.generate_fn_name_span(span) { } else if let Some(sp) = cm.generate_fn_name_span(span) {
err.span_label(sp, "try adding a local type parameter in this method instead"); err.span_label(sp, "try adding a local type parameter in this method instead");
} else { } else {
@ -2998,8 +3001,12 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
enum_path); enum_path);
err.help(&msg); err.help(&msg);
} else { } else {
err.span_suggestion(span, "you can try using the variant's enum", err.span_suggestion_with_applicability(
enum_path); span,
"you can try using the variant's enum",
enum_path,
Applicability::MachineApplicable,
);
} }
} }
} }
@ -3008,20 +3015,32 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
let self_is_available = this.self_value_is_available(path[0].span, span); let self_is_available = this.self_value_is_available(path[0].span, span);
match candidate { match candidate {
AssocSuggestion::Field => { AssocSuggestion::Field => {
err.span_suggestion(span, "try", err.span_suggestion_with_applicability(
format!("self.{}", path_str)); span,
"try",
format!("self.{}", path_str),
Applicability::MachineApplicable,
);
if !self_is_available { if !self_is_available {
err.span_label(span, format!("`self` value is only available in \ err.span_label(span, format!("`self` value is only available in \
methods with `self` parameter")); methods with `self` parameter"));
} }
} }
AssocSuggestion::MethodWithSelf if self_is_available => { AssocSuggestion::MethodWithSelf if self_is_available => {
err.span_suggestion(span, "try", err.span_suggestion_with_applicability(
format!("self.{}", path_str)); span,
"try",
format!("self.{}", path_str),
Applicability::MachineApplicable,
);
} }
AssocSuggestion::MethodWithSelf | AssocSuggestion::AssocItem => { AssocSuggestion::MethodWithSelf | AssocSuggestion::AssocItem => {
err.span_suggestion(span, "try", err.span_suggestion_with_applicability(
format!("Self::{}", path_str)); span,
"try",
format!("Self::{}", path_str),
Applicability::MachineApplicable,
);
} }
} }
return (err, candidates); return (err, candidates);
@ -4617,15 +4636,16 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
format!("other_{}", name) format!("other_{}", name)
}; };
err.span_suggestion(binding.span, err.span_suggestion_with_applicability(
rename_msg, binding.span,
if snippet.ends_with(';') { rename_msg,
format!("{} as {};", if snippet.ends_with(';') {
&snippet[..snippet.len()-1], format!("{} as {};", &snippet[..snippet.len() - 1], suggested_name)
suggested_name) } else {
} else { format!("{} as {}", snippet, suggested_name)
format!("{} as {}", snippet, suggested_name) },
}); Applicability::MachineApplicable,
);
} else { } else {
err.span_label(binding.span, rename_msg); err.span_label(binding.span, rename_msg);
} }

View file

@ -10,7 +10,7 @@
use rustc::session::Session; use rustc::session::Session;
use syntax_pos::Span; use syntax_pos::Span;
use errors::{DiagnosticId, DiagnosticBuilder}; use errors::{Applicability, DiagnosticId, DiagnosticBuilder};
use rustc::ty::{Ty, TypeFoldable}; use rustc::ty::{Ty, TypeFoldable};
pub trait StructuredDiagnostic<'tcx> { pub trait StructuredDiagnostic<'tcx> {
@ -73,9 +73,12 @@ impl<'tcx> StructuredDiagnostic<'tcx> for VariadicError<'tcx> {
) )
}; };
if let Ok(snippet) = self.sess.source_map().span_to_snippet(self.span) { if let Ok(snippet) = self.sess.source_map().span_to_snippet(self.span) {
err.span_suggestion(self.span, err.span_suggestion_with_applicability(
&format!("cast the value to `{}`", self.cast_ty), self.span,
format!("{} as {}", snippet, self.cast_ty)); &format!("cast the value to `{}`", self.cast_ty),
format!("{} as {}", snippet, self.cast_ty),
Applicability::MachineApplicable,
);
} else { } else {
err.help(&format!("cast the value to `{}`", self.cast_ty)); err.help(&format!("cast the value to `{}`", self.cast_ty));
} }

View file

@ -786,7 +786,12 @@ impl<'a> Parser<'a> {
} else { } else {
err.span_label(self.span, "expected identifier"); err.span_label(self.span, "expected identifier");
if self.token == token::Comma && self.look_ahead(1, |t| t.is_ident()) { if self.token == token::Comma && self.look_ahead(1, |t| t.is_ident()) {
err.span_suggestion(self.span, "remove this comma", String::new()); err.span_suggestion_with_applicability(
self.span,
"remove this comma",
String::new(),
Applicability::MachineApplicable,
);
} }
} }
err err
@ -6077,7 +6082,12 @@ impl<'a> Parser<'a> {
self.this_token_to_string())); self.this_token_to_string()));
if self.token.is_ident() { if self.token.is_ident() {
// This is likely another field; emit the diagnostic and keep going // This is likely another field; emit the diagnostic and keep going
err.span_suggestion(sp, "try adding a comma", ",".into()); err.span_suggestion_with_applicability(
sp,
"try adding a comma",
",".into(),
Applicability::MachineApplicable,
);
err.emit(); err.emit();
} else { } else {
return Err(err) return Err(err)