feat: validator fns no longer need to be public
If the function doesn't match a script purpose and is unused then it will till present as a warning.
This commit is contained in:
parent
63bfb4e05d
commit
c66d07a54c
|
@ -30,5 +30,11 @@ impl IdGenerator {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub const SPEND: &str = "spend";
|
||||||
|
pub const CERT: &str = "cert";
|
||||||
|
pub const MINT: &str = "mint";
|
||||||
|
pub const WITHDRAW: &str = "withdraw";
|
||||||
|
pub const VALIDATOR_NAMES: [&str; 4] = [SPEND, CERT, MINT, WITHDRAW];
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
|
|
|
@ -8,13 +8,13 @@ use itertools::Itertools;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ast::{
|
ast::{
|
||||||
Annotation, CallArg, DataType, Definition, Function, ModuleConstant, Pattern,
|
Annotation, CallArg, DataType, Definition, Function, ModuleConstant, ModuleKind, Pattern,
|
||||||
RecordConstructor, RecordConstructorArg, Span, TypeAlias, TypedDefinition,
|
RecordConstructor, RecordConstructorArg, Span, TypeAlias, TypedDefinition,
|
||||||
UnqualifiedImport, UntypedDefinition, Use, PIPE_VARIABLE,
|
UnqualifiedImport, UntypedDefinition, Use, PIPE_VARIABLE,
|
||||||
},
|
},
|
||||||
builtins::{self, function, generic_var, tuple, unbound_var},
|
builtins::{self, function, generic_var, tuple, unbound_var},
|
||||||
tipo::fields::FieldMap,
|
tipo::fields::FieldMap,
|
||||||
IdGenerator,
|
IdGenerator, VALIDATOR_NAMES,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
|
@ -993,6 +993,7 @@ impl<'a> Environment<'a> {
|
||||||
module_name: &String,
|
module_name: &String,
|
||||||
hydrators: &mut HashMap<String, Hydrator>,
|
hydrators: &mut HashMap<String, Hydrator>,
|
||||||
names: &mut HashMap<&'a str, &'a Span>,
|
names: &mut HashMap<&'a str, &'a Span>,
|
||||||
|
kind: ModuleKind,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
match def {
|
match def {
|
||||||
Definition::Fn(Function {
|
Definition::Fn(Function {
|
||||||
|
@ -1051,7 +1052,7 @@ impl<'a> Environment<'a> {
|
||||||
tipo,
|
tipo,
|
||||||
);
|
);
|
||||||
|
|
||||||
if !public {
|
if !public && (kind.is_lib() || !VALIDATOR_NAMES.contains(&name.as_str())) {
|
||||||
self.init_usage(name.clone(), EntityKind::PrivateFunction, *location);
|
self.init_usage(name.clone(), EntityKind::PrivateFunction, *location);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1389,6 +1389,12 @@ pub enum Warning {
|
||||||
location: Span,
|
location: Span,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
#[error("I found a public definition in a validator module.\nDefinitions in validator modules do not need to be public.\n")]
|
||||||
|
PubInValidatorModule {
|
||||||
|
#[label]
|
||||||
|
location: Span,
|
||||||
|
},
|
||||||
|
|
||||||
#[error("I found a record update with no fields; effectively updating nothing.\n")]
|
#[error("I found a record update with no fields; effectively updating nothing.\n")]
|
||||||
#[diagnostic(url("https://aiken-lang.org/language-tour/custom-types#record-updates"))]
|
#[diagnostic(url("https://aiken-lang.org/language-tour/custom-types#record-updates"))]
|
||||||
NoFieldsRecordUpdate {
|
NoFieldsRecordUpdate {
|
||||||
|
|
|
@ -3,7 +3,7 @@ use std::collections::HashMap;
|
||||||
use crate::{
|
use crate::{
|
||||||
ast::{
|
ast::{
|
||||||
DataType, Definition, Function, Layer, ModuleConstant, ModuleKind, RecordConstructor,
|
DataType, Definition, Function, Layer, ModuleConstant, ModuleKind, RecordConstructor,
|
||||||
RecordConstructorArg, TypeAlias, TypedDefinition, TypedModule, UntypedDefinition,
|
RecordConstructorArg, Span, TypeAlias, TypedDefinition, TypedModule, UntypedDefinition,
|
||||||
UntypedModule, Use,
|
UntypedModule, Use,
|
||||||
},
|
},
|
||||||
builtins,
|
builtins,
|
||||||
|
@ -57,7 +57,7 @@ impl UntypedModule {
|
||||||
|
|
||||||
// Register values so they can be used in functions earlier in the module.
|
// Register values so they can be used in functions earlier in the module.
|
||||||
for def in self.definitions() {
|
for def in self.definitions() {
|
||||||
environment.register_values(def, &name, &mut hydrators, &mut value_names)?;
|
environment.register_values(def, &name, &mut hydrators, &mut value_names, kind)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Infer the types of each definition in the module
|
// Infer the types of each definition in the module
|
||||||
|
@ -79,7 +79,7 @@ impl UntypedModule {
|
||||||
}
|
}
|
||||||
|
|
||||||
for def in consts.into_iter().chain(not_consts) {
|
for def in consts.into_iter().chain(not_consts) {
|
||||||
let definition = infer_definition(def, &name, &mut hydrators, &mut environment)?;
|
let definition = infer_definition(def, &name, &mut hydrators, &mut environment, kind)?;
|
||||||
|
|
||||||
definitions.push(definition);
|
definitions.push(definition);
|
||||||
}
|
}
|
||||||
|
@ -145,6 +145,7 @@ fn infer_definition(
|
||||||
module_name: &String,
|
module_name: &String,
|
||||||
hydrators: &mut HashMap<String, Hydrator>,
|
hydrators: &mut HashMap<String, Hydrator>,
|
||||||
environment: &mut Environment<'_>,
|
environment: &mut Environment<'_>,
|
||||||
|
kind: ModuleKind,
|
||||||
) -> Result<TypedDefinition, Error> {
|
) -> Result<TypedDefinition, Error> {
|
||||||
match def {
|
match def {
|
||||||
Definition::Fn(Function {
|
Definition::Fn(Function {
|
||||||
|
@ -158,6 +159,15 @@ fn infer_definition(
|
||||||
end_position,
|
end_position,
|
||||||
..
|
..
|
||||||
}) => {
|
}) => {
|
||||||
|
if public && kind.is_validator() {
|
||||||
|
environment.warnings.push(Warning::PubInValidatorModule {
|
||||||
|
location: Span {
|
||||||
|
start: location.start,
|
||||||
|
end: location.start + 3,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
let preregistered_fn = environment
|
let preregistered_fn = environment
|
||||||
.get_variable(&name)
|
.get_variable(&name)
|
||||||
.expect("Could not find preregistered type for function");
|
.expect("Could not find preregistered type for function");
|
||||||
|
@ -239,7 +249,7 @@ fn infer_definition(
|
||||||
|
|
||||||
Definition::Test(f) => {
|
Definition::Test(f) => {
|
||||||
if let Definition::Fn(f) =
|
if let Definition::Fn(f) =
|
||||||
infer_definition(Definition::Fn(f), module_name, hydrators, environment)?
|
infer_definition(Definition::Fn(f), module_name, hydrators, environment, kind)?
|
||||||
{
|
{
|
||||||
environment.unify(f.return_type.clone(), builtins::bool(), f.location)?;
|
environment.unify(f.return_type.clone(), builtins::bool(), f.location)?;
|
||||||
Ok(Definition::Test(f))
|
Ok(Definition::Test(f))
|
||||||
|
@ -257,6 +267,15 @@ fn infer_definition(
|
||||||
annotation,
|
annotation,
|
||||||
..
|
..
|
||||||
}) => {
|
}) => {
|
||||||
|
if public && kind.is_validator() {
|
||||||
|
environment.warnings.push(Warning::PubInValidatorModule {
|
||||||
|
location: Span {
|
||||||
|
start: location.start,
|
||||||
|
end: location.start + 3,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
let tipo = environment
|
let tipo = environment
|
||||||
.get_type_constructor(&None, &alias, location)
|
.get_type_constructor(&None, &alias, location)
|
||||||
.expect("Could not find existing type for type alias")
|
.expect("Could not find existing type for type alias")
|
||||||
|
@ -284,6 +303,15 @@ fn infer_definition(
|
||||||
constructors: untyped_constructors,
|
constructors: untyped_constructors,
|
||||||
..
|
..
|
||||||
}) => {
|
}) => {
|
||||||
|
if public && kind.is_validator() {
|
||||||
|
environment.warnings.push(Warning::PubInValidatorModule {
|
||||||
|
location: Span {
|
||||||
|
start: location.start,
|
||||||
|
end: location.start + 3,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
let constructors = untyped_constructors
|
let constructors = untyped_constructors
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(
|
.map(
|
||||||
|
@ -417,6 +445,15 @@ fn infer_definition(
|
||||||
value,
|
value,
|
||||||
..
|
..
|
||||||
}) => {
|
}) => {
|
||||||
|
if public && kind.is_validator() {
|
||||||
|
environment.warnings.push(Warning::PubInValidatorModule {
|
||||||
|
location: Span {
|
||||||
|
start: location.start,
|
||||||
|
end: location.start + 3,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
let typed_expr = ExprTyper::new(environment).infer_const(&annotation, *value)?;
|
let typed_expr = ExprTyper::new(environment).infer_const(&annotation, *value)?;
|
||||||
|
|
||||||
let tipo = typed_expr.tipo();
|
let tipo = typed_expr.tipo();
|
||||||
|
|
|
@ -17,7 +17,7 @@ use aiken_lang::{
|
||||||
builtins::{self, generic_var},
|
builtins::{self, generic_var},
|
||||||
tipo::TypeInfo,
|
tipo::TypeInfo,
|
||||||
uplc::CodeGenerator,
|
uplc::CodeGenerator,
|
||||||
IdGenerator,
|
IdGenerator, CERT, MINT, SPEND, VALIDATOR_NAMES, WITHDRAW,
|
||||||
};
|
};
|
||||||
use deps::UseManifest;
|
use deps::UseManifest;
|
||||||
use miette::NamedSource;
|
use miette::NamedSource;
|
||||||
|
@ -44,10 +44,7 @@ use uplc::{
|
||||||
use crate::{
|
use crate::{
|
||||||
config::Config,
|
config::Config,
|
||||||
error::{Error, Warning},
|
error::{Error, Warning},
|
||||||
module::{
|
module::{CheckedModule, CheckedModules, ParsedModule, ParsedModules},
|
||||||
CheckedModule, CheckedModules, ParsedModule, ParsedModules, CERT, MINT, SPEND,
|
|
||||||
VALIDATOR_NAMES, WITHDRAW,
|
|
||||||
},
|
|
||||||
telemetry::Event,
|
telemetry::Event,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -207,12 +207,6 @@ fn find_cycle(
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const SPEND: &str = "spend";
|
|
||||||
pub const CERT: &str = "cert";
|
|
||||||
pub const MINT: &str = "mint";
|
|
||||||
pub const WITHDRAW: &str = "withdraw";
|
|
||||||
pub const VALIDATOR_NAMES: [&str; 4] = [SPEND, CERT, MINT, WITHDRAW];
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct CheckedModule {
|
pub struct CheckedModule {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
|
Loading…
Reference in New Issue