Skip to content

Commit

Permalink
Merge pull request #131 from sharkdp/fix-130
Browse files Browse the repository at this point in the history
Fix name clash with reserved identifiers
  • Loading branch information
sharkdp authored Jul 25, 2023
2 parents e33f85d + 2d0c815 commit eabca59
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions examples/name_resolution_error/reserved_identifier_1.nbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
unit _
1 change: 1 addition & 0 deletions examples/name_resolution_error/reserved_identifier_2.nbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
unit ans
1 change: 1 addition & 0 deletions examples/name_resolution_error/reserved_identifier_3.nbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let _ = 1
1 change: 1 addition & 0 deletions examples/name_resolution_error/reserved_identifier_4.nbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let ans = 1
3 changes: 2 additions & 1 deletion numbat-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,8 @@ impl Cli {
execution_mode.exit_status_in_case_of_error()
}
Err(NumbatError::NameResolutionError(
e @ NameResolutionError::IdentifierClash { .. },
e @ (NameResolutionError::IdentifierClash { .. }
| NameResolutionError::ReservedIdentifier(_)),
)) => {
self.print_diagnostic(e);
execution_mode.exit_status_in_case_of_error()
Expand Down
5 changes: 5 additions & 0 deletions numbat/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ impl ErrorDiagnostic for NameResolutionError {
.diagnostic_label(LabelStyle::Primary)
.with_message("identifier is already in use"),
]),
NameResolutionError::ReservedIdentifier(span) => Diagnostic::error()
.with_message("reserved identifier may not be used")
.with_labels(vec![span
.diagnostic_label(LabelStyle::Primary)
.with_message("reserved identifier")]),
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions numbat/src/name_resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ pub enum NameResolutionError {
conflict_span: Span,
original_span: Span,
},

#[error("Reserved identifier")]
ReservedIdentifier(Span),
}
7 changes: 7 additions & 0 deletions numbat/src/prefix_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ pub struct PrefixParser {
units_vec: Vec<(String, UnitInfo)>,

other_identifiers: HashMap<String, Span>,

reserved_identifiers: &'static [&'static str],
}

impl PrefixParser {
Expand All @@ -77,6 +79,7 @@ impl PrefixParser {
units: HashMap::new(),
units_vec: Vec::new(),
other_identifiers: HashMap::new(),
reserved_identifiers: &["_", "ans"],
}
}

Expand Down Expand Up @@ -138,6 +141,10 @@ impl PrefixParser {
}

fn ensure_name_is_available(&self, name: &str, conflict_span: Span) -> Result<()> {
if self.reserved_identifiers.contains(&name) {
return Err(NameResolutionError::ReservedIdentifier(conflict_span));
}

if let Some(original_span) = self.other_identifiers.get(name) {
return Err(self.identifier_clash_error(name, conflict_span, *original_span));
}
Expand Down

0 comments on commit eabca59

Please sign in to comment.