Skip to content

Commit

Permalink
Reserve identifiers starting with __ for compiler usage
Browse files Browse the repository at this point in the history
  • Loading branch information
dalance committed Apr 10, 2024
1 parent 46f62a6 commit 374878b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
25 changes: 25 additions & 0 deletions crates/analyzer/src/analyzer_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,23 @@ pub enum AnalyzerError {
#[label("Uncovered")]
uncovered: SourceSpan,
},

#[diagnostic(
severity(Error),
code(reserved_identifier),
help("prefix `__` can't be used"),
url(
"https://doc.veryl-lang.org/book/06_appendix/02_semantic_error.html#reserved_identifier"
)
)]
#[error("{identifier} is reverved for compiler usage")]
ReservedIdentifier {
identifier: String,
#[source_code]
input: NamedSource,
#[label("Error location")]
error_location: SourceSpan,
},
}

impl AnalyzerError {
Expand Down Expand Up @@ -906,4 +923,12 @@ impl AnalyzerError {
uncovered: uncovered.into(),
}
}

pub fn reserved_identifier(identifier: &str, source: &str, token: &Token) -> Self {
AnalyzerError::ReservedIdentifier {
identifier: identifier.to_string(),
input: AnalyzerError::named_source(source, token),
error_location: token.into(),
}
}
}
9 changes: 9 additions & 0 deletions crates/analyzer/src/handlers/check_identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ impl<'a> CheckIdentifier<'a> {
};

let identifier = token.to_string();

if identifier.starts_with("__") {
self.errors.push(AnalyzerError::reserved_identifier(
&identifier,
self.text,
token,
));
}

if let Some(prefix) = prefix {
if !identifier.starts_with(prefix) {
self.errors.push(AnalyzerError::invalid_identifier(
Expand Down
14 changes: 14 additions & 0 deletions crates/analyzer/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,3 +600,17 @@ fn uncovered_branch() {
let errors = analyze(code);
assert!(matches!(errors[0], AnalyzerError::UncoveredBranch { .. }));
}

#[test]
fn reserved_identifier() {
let code = r#"
module __ModuleA {
}
"#;

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

0 comments on commit 374878b

Please sign in to comment.