require types on definition statements

This commit is contained in:
gluax 2022-03-07 08:05:42 -08:00
parent 8398b3181e
commit 85efd764a6
5 changed files with 14 additions and 25 deletions

View File

@ -371,7 +371,7 @@ impl Canonicalizer {
}
Statement::Definition(definition) => {
let value = self.canonicalize_expression(&definition.value);
let type_ = self.canonicalize_self_type(definition.type_.as_ref());
let type_ = self.canonicalize_self_type(Some(&definition.type_)).unwrap();
Statement::Definition(DefinitionStatement {
declaration_type: definition.declaration_type.clone(),
@ -550,9 +550,7 @@ impl ReconstructingReducer for Canonicalizer {
for (index, character) in string.iter().enumerate() {
let col_start = span.col_start + index + 1 + col_adder; // account for open quote
let bytes = span.content.clone().into_bytes();
let col_stop: usize;
if bytes[col_start - 1] == b'\\' {
let col_stop = if bytes[col_start - 1] == b'\\' {
let mut width = 0;
match bytes[col_start] {
@ -569,10 +567,10 @@ impl ReconstructingReducer for Canonicalizer {
_ => width += 1,
}
col_adder += width;
col_stop = col_start + 1 + width;
col_start + 1 + width
} else {
col_stop = col_start + 1;
}
col_start + 1
};
elements.push(SpreadOrExpression::Expression(Expression::Value(
ValueExpression::Char(CharValue {
@ -620,11 +618,11 @@ impl ReconstructingReducer for Canonicalizer {
&mut self,
definition: &DefinitionStatement,
variable_names: Vec<VariableName>,
type_: Option<Type>,
type_: Type,
value: Expression,
) -> Result<DefinitionStatement> {
match &type_ {
Some(Type::Tuple(elements)) if elements.len() != 1 => {}
Type::Tuple(elements) if elements.len() != 1 => {}
_ if definition.parened => {
return Err(AstError::invalid_parens_around_single_variable(&definition.span).into());
}

View File

@ -309,11 +309,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
variable_names.push(self.reduce_variable_name(variable_name)?);
}
let type_ = definition
.type_
.as_ref()
.map(|type_| self.reduce_type(type_, &definition.span))
.transpose()?;
let type_ = self.reduce_type(&definition.type_, &definition.span)?;
let value = self.reduce_expression(&definition.value)?;

View File

@ -280,7 +280,7 @@ pub trait ReconstructingReducer {
&mut self,
definition: &DefinitionStatement,
variable_names: Vec<VariableName>,
type_: Option<Type>,
type_: Type,
value: Expression,
) -> Result<DefinitionStatement> {
Ok(DefinitionStatement {

View File

@ -36,7 +36,7 @@ pub struct DefinitionStatement {
/// Tracks whether the variable(s) are in parens.
pub parened: bool,
/// The types of the bindings, if specified, or inferred otherwise.
pub type_: Option<Type>,
pub type_: Type,
/// An initializer value for the bindings.
pub value: Expression,
/// The span excluding the semicolon.
@ -61,9 +61,7 @@ impl fmt::Display for DefinitionStatement {
write!(f, "({})", names)?;
}
if self.type_.is_some() {
write!(f, ": {}", self.type_.as_ref().unwrap())?;
}
write!(f, ": {}", self.type_)?;
write!(f, " = {};", self.value)
}
}

View File

@ -305,11 +305,8 @@ impl ParserContext<'_> {
(vec![self.parse_variable_name(&declare)?], false)
};
// Parse an optional type ascription.
let type_ = self
.eat(Token::Colon)
.map(|_| self.parse_type().map(|t| t.0))
.transpose()?;
self.expect(Token::Colon)?;
let type_ = self.parse_type()?;
self.expect(Token::Assign)?;
let expr = self.parse_expression()?;
@ -324,7 +321,7 @@ impl ParserContext<'_> {
},
variable_names,
parened,
type_,
type_: type_.0,
value: expr,
})
}