Skip to content

Commit

Permalink
report error when enum variant cannot be evaluated
Browse files Browse the repository at this point in the history
  • Loading branch information
taichi-ishitani committed Aug 22, 2024
1 parent d31f797 commit 04b60ed
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
23 changes: 23 additions & 0 deletions crates/analyzer/src/analyzer_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,21 @@ pub enum AnalyzerError {
error_location: SourceSpan,
},

#[diagnostic(
severity(Error),
code(unevaluatable_enum_variant_value),
help(""),
url("")
)]
#[error("The implicit value of enum variant {identifier} cannot be evaluated")]
UnevaluatableEnumVariant {
identifier: String,
#[source_code]
input: NamedSource<String>,
#[label("Error location")]
error_location: SourceSpan,
},

#[diagnostic(
severity(Error),
code(too_large_number),
Expand Down Expand Up @@ -1334,6 +1349,14 @@ impl AnalyzerError {
}
}

pub fn unevaluatable_enum_variant(identifier: &str, source: &str, token: &TokenRange) -> Self {
AnalyzerError::UnevaluatableEnumVariant {
identifier: identifier.to_string(),
input: AnalyzerError::named_source(source, token),
error_location: token.into(),
}
}

pub fn too_large_number(width: usize, source: &str, token: &TokenRange) -> Self {
AnalyzerError::TooLargeNumber {
width,
Expand Down
8 changes: 7 additions & 1 deletion crates/analyzer/src/handlers/create_symbol_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,13 @@ impl<'a> VerylGrammarTrait for CreateSymbolTable<'a> {
if let Some(value) = evaluated {
value
} else {
todo!("report error")
let name = arg.identifier.identifier_token.to_string();
self.errors.push(AnalyzerError::unevaluatable_enum_variant(
&name,
self.text,
&arg.identifier.as_ref().into(),
));
return Ok(());
}
}
EnumMemberValue::ImplicitValue(value) => value,
Expand Down
30 changes: 30 additions & 0 deletions crates/analyzer/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1388,6 +1388,36 @@ fn too_much_enum_variant() {
));
}

#[test]
fn unevaluatable_enum_variant() {
let code = r#"
module ModuleA {
enum EnumA: logic<2> {
A = 2'b0x,
B = 2'b10,
}
}
"#;

let errors = analyze(code);
assert!(errors.is_empty());

let code = r#"
module ModuleB {
enum EnumA: logic<2> {
A = 2'b0x,
B,
}
}
"#;

let errors = analyze(code);
assert!(matches!(
errors[0],
AnalyzerError::UnevaluatableEnumVariant { .. }
))
}

#[test]
fn undefined_identifier() {
let code = r#"
Expand Down

0 comments on commit 04b60ed

Please sign in to comment.