Return error if there is a String data type definition

This commit is contained in:
imaqtkatt 2024-01-15 14:59:49 -03:00
parent 89b73cdb02
commit ab9e71786e
5 changed files with 18 additions and 4 deletions

View File

@ -39,7 +39,7 @@ pub fn compile_book(book: &mut Book, opt_level: OptimizationLevel) -> Result<Com
pub fn desugar_book(book: &mut Book, opt_level: OptimizationLevel) -> Result<DefId, String> {
let main = book.check_has_main()?;
book.check_shared_names()?;
book.encode_strs();
book.encode_strs()?;
book.generate_scott_adts();
book.resolve_refs();
encode_pattern_matching(book)?;

View File

@ -10,7 +10,11 @@ const HEAD: &str = "head";
const TAIL: &str = "tail";
impl Book {
pub fn encode_strs(&mut self) {
pub fn encode_strs(&mut self) -> Result<(), String> {
if self.adts.contains_key(&Name::new(STRING)) {
return Err(format!("String is a built-in data type and should not be overridden."));
}
self.ctrs.insert(Name::new(SNIL), Name::new(STRING));
self.ctrs.insert(Name::new(SCONS), Name::new(STRING));
@ -33,6 +37,8 @@ impl Book {
self.ctrs.remove(&Name::new(SCONS));
self.adts.remove(&Name::new(STRING));
}
Ok(())
}
}
@ -64,7 +70,7 @@ impl Term {
let fst_uses = fst.encode_str();
let snd_uses = snd.encode_str();
fst_uses || snd_uses
},
}
Term::Match { arms, .. } => {
let mut used = false;
for arm in arms {

View File

@ -160,7 +160,7 @@ fn encode_pattern_match() {
run_golden_test_dir(function_name!(), &|code| {
let mut book = do_parse_book(code)?;
book.check_shared_names()?;
book.encode_strs();
book.encode_strs()?;
book.generate_scott_adts();
book.resolve_refs();
encode_pattern_matching(&mut book)?;

View File

@ -0,0 +1,3 @@
data String = S
main = S

View File

@ -0,0 +1,5 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/compile_file/adt_string.hvm
---
String is a built-in data type and should not be overridden.