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

Fix unexpected enum variant resolution #1134

Merged
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
25 changes: 22 additions & 3 deletions crates/analyzer/src/handlers/check_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,28 @@ impl VerylGrammarTrait for CheckType<'_> {

fn scoped_identifier(&mut self, arg: &ScopedIdentifier) -> Result<(), ParolError> {
if let HandlerPoint::Before = self.point {
// Check variable type
if !self.in_user_defined_type.is_empty() && self.in_generic_argument.is_empty() {
if let Ok(symbol) = symbol_table::resolve(arg) {
if let Ok(symbol) = symbol_table::resolve(arg) {
// Check enum variant should be scoped by enum
// refs: https://github.com/veryl-lang/veryl/issues/1114
if matches!(symbol.found.kind, SymbolKind::EnumMember(_)) {
let preceed_id = symbol.full_path.get(symbol.full_path.len() - 2).unwrap();
let preceed_symbol = symbol_table::get(*preceed_id).unwrap();
if !matches!(
preceed_symbol.kind,
SymbolKind::Enum(_) | SymbolKind::TypeDef(_)
) {
self.errors.push(AnalyzerError::mismatch_type(
&preceed_symbol.token.to_string(),
"enum",
&preceed_symbol.kind.to_kind_name(),
self.text,
&preceed_symbol.token.into(),
));
}
}

// Check variable type
if !self.in_user_defined_type.is_empty() && self.in_generic_argument.is_empty() {
if self.in_modport {
if !matches!(symbol.found.kind, SymbolKind::Modport(_)) {
self.errors.push(AnalyzerError::mismatch_type(
Expand Down
29 changes: 29 additions & 0 deletions crates/analyzer/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,35 @@ fn mismatch_type() {

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

let code = r#"
module ModuleA {
enum EnumA {
B,
}
let _a: EnumA = EnumA::B;
let _b: EnumA = _a::B;
}
"#;

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

let code = r#"
module ModuleA {
enum EnumA {
A,
B,
}
type EnumB = EnumA;

let _a: EnumA = EnumA::A;
let _b: EnumB = EnumB::B;
}
"#;

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

#[test]
Expand Down
Loading