-
Notifications
You must be signed in to change notification settings - Fork 505
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added
compiler_error!
inline macro. (#5357)
- Loading branch information
Showing
6 changed files
with
192 additions
and
29 deletions.
There are no files selected for viewing
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
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,46 @@ | ||
use cairo_lang_defs::extract_macro_single_unnamed_arg; | ||
use cairo_lang_defs::plugin::{MacroPlugin, MacroPluginMetadata, PluginDiagnostic, PluginResult}; | ||
use cairo_lang_defs::plugin_utils::PluginResultTrait; | ||
use cairo_lang_syntax::node::db::SyntaxGroup; | ||
use cairo_lang_syntax::node::{ast, Terminal, TypedStablePtr, TypedSyntaxNode}; | ||
|
||
/// Plugin that allows writing item level `compile_error!` causing a diagnostic. | ||
/// Useful for testing that `cfg` attributes are valid. | ||
#[derive(Debug, Default)] | ||
#[non_exhaustive] | ||
pub struct CompileErrorPlugin; | ||
|
||
impl MacroPlugin for CompileErrorPlugin { | ||
fn generate_code( | ||
&self, | ||
db: &dyn SyntaxGroup, | ||
item_ast: ast::ModuleItem, | ||
_metadata: &MacroPluginMetadata<'_>, | ||
) -> PluginResult { | ||
if let ast::ModuleItem::InlineMacro(inline_macro_ast) = item_ast { | ||
if inline_macro_ast.name(db).text(db) == "compile_error" { | ||
let compilation_error_arg = extract_macro_single_unnamed_arg!( | ||
db, | ||
&inline_macro_ast, | ||
ast::WrappedArgList::ParenthesizedArgList(_) | ||
); | ||
let ast::Expr::String(err_message) = compilation_error_arg else { | ||
return PluginResult::diagnostic_only(PluginDiagnostic::error( | ||
compilation_error_arg.stable_ptr().untyped(), | ||
"`compiler_error!` argument must be an unnamed string argument." | ||
.to_string(), | ||
)); | ||
}; | ||
return PluginResult::diagnostic_only(PluginDiagnostic::error( | ||
inline_macro_ast.stable_ptr().untyped(), | ||
err_message.text(db).to_string(), | ||
)); | ||
} | ||
} | ||
PluginResult { code: None, diagnostics: vec![], remove_original_item: false } | ||
} | ||
|
||
fn declared_attributes(&self) -> Vec<String> { | ||
vec![] | ||
} | ||
} |
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
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,61 @@ | ||
//! > Test diagnostics of compile error. | ||
|
||
//! > test_runner_name | ||
test_expand_plugin(expect_diagnostics: true) | ||
|
||
//! > cairo_code | ||
compile_error!(); | ||
|
||
compile_error!(1); | ||
|
||
compile_error!("message", "extra"); | ||
|
||
compile_error!("error message"); | ||
|
||
//! > expanded_cairo_code | ||
|
||
//! > expected_diagnostics | ||
error: Macro `compile_error` must have exactly 1 unnamed arguments. | ||
--> test_src/lib.cairo:1:1 | ||
compile_error!(); | ||
^***************^ | ||
|
||
error: `compiler_error!` argument must be an unnamed string argument. | ||
--> test_src/lib.cairo:3:16 | ||
compile_error!(1); | ||
^ | ||
|
||
error: Macro `compile_error` must have exactly 1 unnamed arguments. | ||
--> test_src/lib.cairo:5:1 | ||
compile_error!("message", "extra"); | ||
^*********************************^ | ||
|
||
error: "error message" | ||
--> test_src/lib.cairo:7:1 | ||
compile_error!("error message"); | ||
^******************************^ | ||
|
||
//! > ========================================================================== | ||
|
||
//! > Test usage of `compile_error!` with `cfg`. | ||
|
||
//! > test_runner_name | ||
test_expand_plugin(expect_diagnostics: true) | ||
|
||
//! > cfg | ||
["noignore"] | ||
|
||
//! > cairo_code | ||
#[cfg(noignore)] | ||
compile_error!("show"); | ||
|
||
#[cfg(ignore)] | ||
compile_error!("ignore"); | ||
|
||
//! > expanded_cairo_code | ||
|
||
//! > expected_diagnostics | ||
error: "show" | ||
--> test_src/lib.cairo:1:1 | ||
#[cfg(noignore)] | ||
^**************^ |