feat(imports): return a nice error if a validator module is imported

This commit is contained in:
rvcas 2023-01-15 18:45:15 -05:00 committed by Lucas
parent f114905f7d
commit 1adac64585
2 changed files with 19 additions and 1 deletions

View File

@ -680,10 +680,17 @@ impl<'a> Environment<'a> {
.get(&name) .get(&name)
.ok_or_else(|| Error::UnknownModule { .ok_or_else(|| Error::UnknownModule {
location: *location, location: *location,
name, name: name.clone(),
imported_modules: self.imported_modules.keys().cloned().collect(), imported_modules: self.imported_modules.keys().cloned().collect(),
})?; })?;
if module_info.kind.is_validator() {
return Err(Error::ValidatorImported {
location: *location,
name,
});
}
// Determine local alias of imported module // Determine local alias of imported module
let module_name = as_name let module_name = as_name
.as_ref() .as_ref()

View File

@ -277,6 +277,9 @@ pub enum Error {
index: usize, index: usize,
size: usize, size: usize,
}, },
#[error("I discovered an attempt to import a validator module: '{}'\n", name.purple())]
ValidatorImported { location: Span, name: String },
} }
impl Error { impl Error {
@ -424,6 +427,7 @@ impl Diagnostic for Error {
Self::RecursiveType { .. } => Some(Box::new("recursive_type")), Self::RecursiveType { .. } => Some(Box::new("recursive_type")),
Self::NotATuple { .. } => Some(Box::new("not_a_tuple")), Self::NotATuple { .. } => Some(Box::new("not_a_tuple")),
Self::TupleIndexOutOfBound { .. } => Some(Box::new("tuple_index_out_of_bound")), Self::TupleIndexOutOfBound { .. } => Some(Box::new("tuple_index_out_of_bound")),
Self::ValidatorImported { .. } => Some(Box::new("validator_imported")),
} }
} }
@ -1088,6 +1092,8 @@ impl Diagnostic for Error {
})), })),
Self::TupleIndexOutOfBound { .. } => None, Self::TupleIndexOutOfBound { .. } => None,
Self::ValidatorImported { .. } => Some(Box::new(format!("If you are trying to share code defined in\na validator then move it to a module in {}", "lib/".purple())))
} }
} }
@ -1260,6 +1266,10 @@ impl Diagnostic for Error {
Self::TupleIndexOutOfBound { location, .. } => Some(Box::new( Self::TupleIndexOutOfBound { location, .. } => Some(Box::new(
vec![LabeledSpan::new_with_span(None, *location)].into_iter(), vec![LabeledSpan::new_with_span(None, *location)].into_iter(),
)), )),
Self::ValidatorImported { location, .. } => Some(Box::new(
vec![LabeledSpan::new_with_span(None, *location)].into_iter(),
)),
} }
} }
@ -1357,6 +1367,7 @@ impl Diagnostic for Error {
Self::TupleIndexOutOfBound { .. } => Some(Box::new( Self::TupleIndexOutOfBound { .. } => Some(Box::new(
"https://aiken-lang.org/language-tour/primitive-types#tuples" "https://aiken-lang.org/language-tour/primitive-types#tuples"
)), )),
Self::ValidatorImported { .. } => None
} }
} }
} }