-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CLI] Add
compiler-message-format-json
experiment value to `aptos m…
…ove compile` and `aptos move lint` (#15540)
- Loading branch information
Showing
18 changed files
with
253 additions
and
46 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
third_party/move/move-compiler-v2/src/diagnostics/human.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright © Aptos Foundation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use crate::diagnostics::Emitter; | ||
use codespan::{FileId, Files}; | ||
use codespan_reporting::{ | ||
diagnostic::Diagnostic, | ||
term::{emit, termcolor::WriteColor, Config}, | ||
}; | ||
|
||
/// It's used in the native aptos-cli output to show error messages. | ||
/// Wraps the `codespan_reporting::term::emit()` method. | ||
pub struct HumanEmitter<'w, W: WriteColor> { | ||
writer: &'w mut W, | ||
} | ||
|
||
impl<'w, W> HumanEmitter<'w, W> | ||
where | ||
W: WriteColor, | ||
{ | ||
pub fn new(writer: &'w mut W) -> Self { | ||
HumanEmitter { writer } | ||
} | ||
} | ||
|
||
impl<'w, W> Emitter for HumanEmitter<'w, W> | ||
where | ||
W: WriteColor, | ||
{ | ||
fn emit(&mut self, source_files: &Files<String>, diag: &Diagnostic<FileId>) { | ||
emit(&mut self.writer, &Config::default(), source_files, diag).expect("emit must not fail") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright © Aptos Foundation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use crate::diagnostics::Emitter; | ||
use codespan::{FileId, Files}; | ||
use codespan_reporting::diagnostic::{Diagnostic, Label}; | ||
use std::io::Write; | ||
|
||
/// Shows compiler errors as a structured JSON output. | ||
/// Exists to support various tools external to the aptos-cli, i.e. IDEs. | ||
pub struct JsonEmitter<'w, W: Write> { | ||
writer: &'w mut W, | ||
} | ||
|
||
impl<'w, W: Write> JsonEmitter<'w, W> { | ||
pub fn new(writer: &'w mut W) -> Self { | ||
JsonEmitter { writer } | ||
} | ||
} | ||
|
||
impl<'w, W: Write> Emitter for JsonEmitter<'w, W> { | ||
fn emit(&mut self, source_files: &Files<String>, diag: &Diagnostic<FileId>) { | ||
let fpath_labels = diag | ||
.labels | ||
.iter() | ||
.map(|label| { | ||
let fpath = codespan_reporting::files::Files::name(source_files, label.file_id) | ||
.expect("always Ok() in the impl") | ||
.to_string(); | ||
Label::new(label.style, fpath, label.range.clone()) | ||
}) | ||
.collect(); | ||
let mut json_diag = Diagnostic::new(diag.severity) | ||
.with_message(diag.message.clone()) | ||
.with_labels(fpath_labels) | ||
.with_notes(diag.notes.clone()); | ||
if let Some(code) = &diag.code { | ||
json_diag = json_diag.with_code(code) | ||
} | ||
serde_json::to_writer(&mut self.writer, &json_diag).expect("it should be serializable"); | ||
writeln!(&mut self.writer) | ||
.expect("dest is stderr / in-memory buffer, it should always be available"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright © Aptos Foundation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use crate::{ | ||
diagnostics::{human::HumanEmitter, json::JsonEmitter}, | ||
options, Experiment, | ||
}; | ||
use anyhow::bail; | ||
use codespan::{FileId, Files}; | ||
use codespan_reporting::{ | ||
diagnostic::{Diagnostic, Severity}, | ||
term::termcolor::WriteColor, | ||
}; | ||
use move_model::model::GlobalEnv; | ||
|
||
pub mod human; | ||
pub mod json; | ||
|
||
impl options::Options { | ||
pub fn error_emitter<'w, W>(&self, dest: &'w mut W) -> Box<dyn Emitter + 'w> | ||
where | ||
W: WriteColor, | ||
{ | ||
if self.experiment_on(Experiment::MESSAGE_FORMAT_JSON) { | ||
Box::new(JsonEmitter::new(dest)) | ||
} else { | ||
Box::new(HumanEmitter::new(dest)) | ||
} | ||
} | ||
} | ||
|
||
pub trait Emitter { | ||
fn emit(&mut self, source_files: &Files<String>, diag: &Diagnostic<FileId>); | ||
|
||
/// Writes accumulated diagnostics of given or higher severity. | ||
fn report_diag(&mut self, global_env: &GlobalEnv, severity: Severity) { | ||
global_env.report_diag_with_filter( | ||
|files, diag| self.emit(files, diag), | ||
|d| d.severity >= severity, | ||
); | ||
} | ||
|
||
/// Helper function to report diagnostics, check for errors, and fail with a message on | ||
/// errors. This function is idempotent and will not report the same diagnostics again. | ||
fn check_diag( | ||
&mut self, | ||
global_env: &GlobalEnv, | ||
report_severity: Severity, | ||
msg: &str, | ||
) -> anyhow::Result<()> { | ||
self.report_diag(global_env, report_severity); | ||
if global_env.has_errors() { | ||
bail!("exiting with {}", msg); | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
third_party/move/move-compiler-v2/tests/compiler-message-format-json/errors.exp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
Diagnostics: | ||
{"severity":"Error","code":null,"message":"cannot use `bool` with an operator which expects a value of type `integer`","labels":[{"style":"Primary","file_id":"tests/compiler-message-format-json/errors.move","range":{"start":51,"end":55},"message":""}],"notes":[]} | ||
{"severity":"Error","code":null,"message":"cannot use `bool` with an operator which expects a value of type `integer`","labels":[{"style":"Primary","file_id":"tests/compiler-message-format-json/errors.move","range":{"start":69,"end":73},"message":""}],"notes":[]} |
6 changes: 6 additions & 0 deletions
6
third_party/move/move-compiler-v2/tests/compiler-message-format-json/errors.move
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module 0x42::errors { | ||
fun main() { | ||
1 + true; | ||
2 + true; | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
third_party/move/move-compiler-v2/tests/compiler-message-format-json/warnings.exp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
Diagnostics: | ||
{"severity":"Warning","code":null,"message":"Unused local variable `a`. Consider removing or prefixing with an underscore: `_a`","labels":[{"style":"Primary","file_id":"tests/compiler-message-format-json/warnings.move","range":{"start":53,"end":54},"message":""}],"notes":[]} | ||
{"severity":"Warning","code":null,"message":"Unused local variable `b`. Consider removing or prefixing with an underscore: `_b`","labels":[{"style":"Primary","file_id":"tests/compiler-message-format-json/warnings.move","range":{"start":72,"end":73},"message":""}],"notes":[]} |
6 changes: 6 additions & 0 deletions
6
third_party/move/move-compiler-v2/tests/compiler-message-format-json/warnings.move
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module 0x42::warnings { | ||
fun main() { | ||
let a = 1; | ||
let b = 2; | ||
} | ||
} |
Oops, something went wrong.