Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiple substitutions become multiple children in JSON output #58221

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 28 additions & 30 deletions src/libsyntax/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use source_map::{SourceMap, FilePathMapping};
use syntax_pos::{self, MacroBacktrace, Span, SpanLabel, MultiSpan};
use errors::registry::Registry;
use errors::{DiagnosticBuilder, SubDiagnostic, CodeSuggestion, SourceMapper};
use errors::{DiagnosticBuilder, SubDiagnostic, SourceMapper};
use errors::{DiagnosticId, Applicability};
use errors::emitter::{Emitter, EmitterWriter};

Expand Down Expand Up @@ -162,15 +162,32 @@ impl Diagnostic {
fn from_diagnostic_builder(db: &DiagnosticBuilder,
je: &JsonEmitter)
-> Diagnostic {
let sugg = db.suggestions.iter().map(|sugg| {
Diagnostic {
message: sugg.msg.clone(),
code: None,
level: "help",
spans: DiagnosticSpan::from_suggestion(sugg, je),
children: vec![],
rendered: None,
}
// In our internal representation, a `CodeSuggestion` has one
// or more `Substitutions`, each of which has one or more
// `SubstitutionParts`; here, each `Substitution` becomes an
// element in the `children` array
let substitution_children = db.suggestions.iter().flat_map(|suggestion| {
suggestion.substitutions.iter().map(move |substitution| {
Diagnostic {
message: suggestion.msg.clone(),
code: None,
level: "help",
spans: substitution.parts.iter().map(move |suggestion_inner| {
let span_label = SpanLabel {
span: suggestion_inner.span,
is_primary: true,
label: None,
};
DiagnosticSpan::from_span_label(
span_label,
Some((&suggestion_inner.snippet, suggestion.applicability)),
je
)
}).collect(),
children: Vec::new(),
rendered: None,
}
})
});

// generate regular command line output and store it in the json
Expand Down Expand Up @@ -201,7 +218,7 @@ impl Diagnostic {
spans: DiagnosticSpan::from_multispan(&db.span, je),
children: db.children.iter().map(|c| {
Diagnostic::from_sub_diagnostic(c, je)
}).chain(sugg).collect(),
}).chain(substitution_children).collect(),
rendered: Some(output),
}
}
Expand Down Expand Up @@ -308,25 +325,6 @@ impl DiagnosticSpan {
.collect()
}

fn from_suggestion(suggestion: &CodeSuggestion, je: &JsonEmitter)
-> Vec<DiagnosticSpan> {
suggestion.substitutions
.iter()
.flat_map(|substitution| {
substitution.parts.iter().map(move |suggestion_inner| {
let span_label = SpanLabel {
span: suggestion_inner.span,
is_primary: true,
label: None,
};
DiagnosticSpan::from_span_label(span_label,
Some((&suggestion_inner.snippet,
suggestion.applicability)),
je)
})
})
.collect()
}
}

impl DiagnosticSpanLine {
Expand Down
8 changes: 8 additions & 0 deletions src/test/ui/json_error_format/issue-53934-multiple-parts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// compile-flags: --error-format pretty-json -Zunstable-options

struct Point { x: isize, y: isize }

fn main() {
let p = Point { x: 1, y: 2 };
let Point { .., y, } = p;
}
126 changes: 126 additions & 0 deletions src/test/ui/json_error_format/issue-53934-multiple-parts.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
{
"message": "expected `}`, found `,`",
"code": null,
"level": "error",
"spans": [
{
"file_name": "$DIR/issue-53934-multiple-parts.rs",
"byte_start": 166,
"byte_end": 167,
"line_start": 7,
"line_end": 7,
"column_start": 19,
"column_end": 20,
"is_primary": true,
"text": [
{
"text": " let Point { .., y, } = p;",
"highlight_start": 19,
"highlight_end": 20
}
],
"label": "expected `}`",
"suggested_replacement": null,
"suggestion_applicability": null,
"expansion": null
},
{
"file_name": "$DIR/issue-53934-multiple-parts.rs",
"byte_start": 164,
"byte_end": 167,
"line_start": 7,
"line_end": 7,
"column_start": 17,
"column_end": 20,
"is_primary": false,
"text": [
{
"text": " let Point { .., y, } = p;",
"highlight_start": 17,
"highlight_end": 20
}
],
"label": "`..` must be at the end and cannot have a trailing comma",
"suggested_replacement": null,
"suggestion_applicability": null,
"expansion": null
}
],
"children": [
{
"message": "move the `..` to the end of the field list",
"code": null,
"level": "help",
"spans": [
{
"file_name": "$DIR/issue-53934-multiple-parts.rs",
"byte_start": 164,
"byte_end": 168,
"line_start": 7,
"line_end": 7,
"column_start": 17,
"column_end": 21,
"is_primary": true,
"text": [
{
"text": " let Point { .., y, } = p;",
"highlight_start": 17,
"highlight_end": 21
}
],
"label": null,
"suggested_replacement": "",
"suggestion_applicability": "MachineApplicable",
"expansion": null
},
{
"file_name": "$DIR/issue-53934-multiple-parts.rs",
"byte_start": 171,
"byte_end": 172,
"line_start": 7,
"line_end": 7,
"column_start": 24,
"column_end": 25,
"is_primary": true,
"text": [
{
"text": " let Point { .., y, } = p;",
"highlight_start": 24,
"highlight_end": 25
}
],
"label": null,
"suggested_replacement": ".. }",
"suggestion_applicability": "MachineApplicable",
"expansion": null
}
],
"children": [],
"rendered": null
}
],
"rendered": "error: expected `}`, found `,`
--> $DIR/issue-53934-multiple-parts.rs:7:19
|
LL | let Point { .., y, } = p;
| --^
| | |
| | expected `}`
| `..` must be at the end and cannot have a trailing comma
help: move the `..` to the end of the field list
|
LL | let Point { y, .. } = p;
| -- ^^^^

"
}
{
"message": "aborting due to previous error",
"code": null,
"level": "error",
"spans": [],
"children": [],
"rendered": "error: aborting due to previous error

"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// compile-flags: --error-format pretty-json -Zunstable-options

fn main() {
let xs = vec![String::from("foo")];
let d: &Display = &xs;
println!("{}", d);
}
Loading