diff options
| author | bors <bors@rust-lang.org> | 2018-02-01 04:47:46 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2018-02-01 04:47:46 +0000 |
| commit | 26792f0607bea2b03daa5195c2688fd53289042b (patch) | |
| tree | 35a3660501f3d9c285d4fbfcdf2efd01777574a5 /src/libsyntax | |
| parent | 8ccab7eed5f4fc93500fbf242e575073ca70d7cb (diff) | |
| parent | 540f95d9fad41a605e4c8b898d77f47374a76cbd (diff) | |
| download | rust-26792f0607bea2b03daa5195c2688fd53289042b.tar.gz rust-26792f0607bea2b03daa5195c2688fd53289042b.zip | |
Auto merge of #47540 - Manishearth:suggestion, r=nrc
Add approximate suggestions for rustfix This adds `span_approximate_suggestion()` that lets you emit a suggestion marked as "non-machine applicable" in the JSON output. UI users see no difference. This is for when rustc and clippy wish to emit suggestions which will make sense to the reader (e.g. they may have placeholders like `<type>`) but are not source-applicable, so that rustfix/etc can ignore these. fixes #39254
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/feature_gate.rs | 5 | ||||
| -rw-r--r-- | src/libsyntax/json.rs | 40 | ||||
| -rw-r--r-- | src/libsyntax/lib.rs | 1 |
3 files changed, 36 insertions, 10 deletions
diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index 9a2560b0458..3e523fca92a 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -788,6 +788,11 @@ pub const BUILTIN_ATTRIBUTES: &'static [(&'static str, AttributeType, AttributeG is just used for rustc unit tests \ and will never be stable", cfg_fn!(rustc_attrs))), + ("rustc_serialize_exclude_null", Normal, Gated(Stability::Unstable, + "rustc_attrs", + "the `#[rustc_serialize_exclude_null]` attribute \ + is an internal-only feature", + cfg_fn!(rustc_attrs))), ("rustc_synthetic", Whitelisted, Gated(Stability::Unstable, "rustc_attrs", "this attribute \ diff --git a/src/libsyntax/json.rs b/src/libsyntax/json.rs index 7635ec26b28..98d5fa8f797 100644 --- a/src/libsyntax/json.rs +++ b/src/libsyntax/json.rs @@ -38,34 +38,41 @@ pub struct JsonEmitter { registry: Option<Registry>, cm: Rc<CodeMapper + 'static>, pretty: bool, + /// Whether "approximate suggestions" are enabled in the config + approximate_suggestions: bool, } impl JsonEmitter { pub fn stderr(registry: Option<Registry>, code_map: Rc<CodeMap>, - pretty: bool) -> JsonEmitter { + pretty: bool, + approximate_suggestions: bool) -> JsonEmitter { JsonEmitter { dst: Box::new(io::stderr()), registry, cm: code_map, pretty, + approximate_suggestions, } } pub fn basic(pretty: bool) -> JsonEmitter { let file_path_mapping = FilePathMapping::empty(); - JsonEmitter::stderr(None, Rc::new(CodeMap::new(file_path_mapping)), pretty) + JsonEmitter::stderr(None, Rc::new(CodeMap::new(file_path_mapping)), + pretty, false) } pub fn new(dst: Box<Write + Send>, registry: Option<Registry>, code_map: Rc<CodeMap>, - pretty: bool) -> JsonEmitter { + pretty: bool, + approximate_suggestions: bool) -> JsonEmitter { JsonEmitter { dst, registry, cm: code_map, pretty, + approximate_suggestions, } } } @@ -101,6 +108,7 @@ struct Diagnostic { } #[derive(RustcEncodable)] +#[allow(unused_attributes)] struct DiagnosticSpan { file_name: String, byte_start: u32, @@ -121,6 +129,9 @@ struct DiagnosticSpan { /// If we are suggesting a replacement, this will contain text /// that should be sliced in atop this span. suggested_replacement: Option<String>, + /// If the suggestion is approximate + #[rustc_serialize_exclude_null] + suggestion_approximate: Option<bool>, /// Macro invocations that created the code at this span, if any. expansion: Option<Box<DiagnosticSpanMacroExpansion>>, } @@ -220,7 +231,7 @@ impl Diagnostic { impl DiagnosticSpan { fn from_span_label(span: SpanLabel, - suggestion: Option<&String>, + suggestion: Option<(&String, bool)>, je: &JsonEmitter) -> DiagnosticSpan { Self::from_span_etc(span.span, @@ -233,7 +244,7 @@ impl DiagnosticSpan { fn from_span_etc(span: Span, is_primary: bool, label: Option<String>, - suggestion: Option<&String>, + suggestion: Option<(&String, bool)>, je: &JsonEmitter) -> DiagnosticSpan { // obtain the full backtrace from the `macro_backtrace` @@ -253,7 +264,7 @@ impl DiagnosticSpan { fn from_span_full(span: Span, is_primary: bool, label: Option<String>, - suggestion: Option<&String>, + suggestion: Option<(&String, bool)>, mut backtrace: vec::IntoIter<MacroBacktrace>, je: &JsonEmitter) -> DiagnosticSpan { @@ -281,6 +292,13 @@ impl DiagnosticSpan { def_site_span, }) }); + + let suggestion_approximate = if je.approximate_suggestions { + suggestion.map(|x| x.1) + } else { + None + }; + DiagnosticSpan { file_name: start.file.name.to_string(), byte_start: span.lo().0 - start.file.start_pos.0, @@ -291,7 +309,8 @@ impl DiagnosticSpan { column_end: end.col.0 + 1, is_primary, text: DiagnosticSpanLine::from_span(span, je), - suggested_replacement: suggestion.cloned(), + suggested_replacement: suggestion.map(|x| x.0.clone()), + suggestion_approximate, expansion: backtrace_step, label, } @@ -309,14 +328,15 @@ impl DiagnosticSpan { suggestion.substitutions .iter() .flat_map(|substitution| { - substitution.parts.iter().map(move |suggestion| { + substitution.parts.iter().map(move |suggestion_inner| { let span_label = SpanLabel { - span: suggestion.span, + span: suggestion_inner.span, is_primary: true, label: None, }; DiagnosticSpan::from_span_label(span_label, - Some(&suggestion.snippet), + Some((&suggestion_inner.snippet, + suggestion.approximate)), je) }) }) diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index 3b4c5da10f2..9181cca215c 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -25,6 +25,7 @@ #![feature(match_default_bindings)] #![feature(i128_type)] #![feature(const_atomic_usize_new)] +#![feature(rustc_attrs)] // See librustc_cratesio_shim/Cargo.toml for a comment explaining this. #[allow(unused_extern_crates)] |
