Browse Source

Proper source spans for variable type annotations

David Peter 2 years ago
parent
commit
c912d48794

+ 1 - 1
book/src/tutorial.md

@@ -2,7 +2,7 @@
 
 In this tutorial, you will use Numbat to calculate how many bananas you would need to power a
 house. This is based on [an article](https://what-if.xkcd.com/158/) in the great *what if?* series
-by the xkcd author.
+by the author of the xkcd comics.
 
 <p align="center"><img src="https://what-if.xkcd.com/imgs/a/158/hazard.png"></p>
 

+ 0 - 4
numbat/src/ast.rs

@@ -363,7 +363,6 @@ pub enum Statement {
         identifier_span: Span,
         identifier: String,
         expr: Expression,
-        type_annotation_span: Option<Span>,
         type_annotation: Option<DimensionExpression>,
     },
     DefineFunction {
@@ -451,7 +450,6 @@ impl PrettyPrint for Statement {
                 identifier_span: _,
                 identifier,
                 expr,
-                type_annotation_span: _,
                 type_annotation,
             } => {
                 m::keyword("let")
@@ -682,13 +680,11 @@ impl ReplaceSpans for Statement {
                 identifier_span: _,
                 identifier,
                 expr,
-                type_annotation_span,
                 type_annotation,
             } => Statement::DefineVariable {
                 identifier_span: Span::dummy(),
                 identifier: identifier.clone(),
                 expr: expr.replace_spans(),
-                type_annotation_span: type_annotation_span.map(|_| Span::dummy()),
                 type_annotation: type_annotation.as_ref().map(|t| t.replace_spans()),
             },
             Statement::DefineFunction {

+ 3 - 8
numbat/src/parser.rs

@@ -237,12 +237,10 @@ impl<'a> Parser<'a> {
             if let Some(identifier) = self.match_exact(TokenKind::Identifier) {
                 let identifier_span = self.last().unwrap().span;
 
-                let (type_annotation_span, dexpr) = if self.match_exact(TokenKind::Colon).is_some()
-                {
-                    let type_annotation = self.dimension_expression()?;
-                    (Some(self.last().unwrap().span), Some(type_annotation))
+                let dexpr = if self.match_exact(TokenKind::Colon).is_some() {
+                    Some(self.dimension_expression()?)
                 } else {
-                    (None, None)
+                    None
                 };
 
                 if self.match_exact(TokenKind::Equal).is_none() {
@@ -258,7 +256,6 @@ impl<'a> Parser<'a> {
                         identifier_span,
                         identifier: identifier.lexeme.clone(),
                         expr,
-                        type_annotation_span,
                         type_annotation: dexpr,
                     })
                 }
@@ -1355,7 +1352,6 @@ mod tests {
                 identifier_span: Span::dummy(),
                 identifier: "foo".into(),
                 expr: scalar!(1.0),
-                type_annotation_span: None,
                 type_annotation: None,
             },
         );
@@ -1366,7 +1362,6 @@ mod tests {
                 identifier_span: Span::dummy(),
                 identifier: "x".into(),
                 expr: binop!(scalar!(1.0), Mul, identifier!("meter")),
-                type_annotation_span: Some(Span::dummy()),
                 type_annotation: Some(DimensionExpression::Dimension(
                     Span::dummy(),
                     "Length".into(),

+ 0 - 2
numbat/src/prefix_transformer.rs

@@ -134,7 +134,6 @@ impl Transformer {
                 identifier_span,
                 identifier,
                 expr,
-                type_annotation_span,
                 type_annotation,
             } => {
                 self.variable_names.push(identifier.clone());
@@ -144,7 +143,6 @@ impl Transformer {
                     identifier_span,
                     identifier,
                     expr: self.transform_expression(expr),
-                    type_annotation_span,
                     type_annotation,
                 }
             }

+ 0 - 2
numbat/src/resolver.rs

@@ -212,7 +212,6 @@ mod tests {
                     identifier_span: Span::dummy(),
                     identifier: "a".into(),
                     expr: Expression::Scalar(Span::dummy(), Number::from_f64(1.0)),
-                    type_annotation_span: None,
                     type_annotation: None
                 },
                 Statement::Expression(Expression::Identifier(Span::dummy(), "a".into()))
@@ -242,7 +241,6 @@ mod tests {
                     identifier_span: Span::dummy(),
                     identifier: "a".into(),
                     expr: Expression::Scalar(Span::dummy(), Number::from_f64(1.0)),
-                    type_annotation_span: None,
                     type_annotation: None
                 },
                 Statement::Expression(Expression::Identifier(Span::dummy(), "a".into()))

+ 1 - 2
numbat/src/typechecker.rs

@@ -433,7 +433,6 @@ impl TypeChecker {
                 identifier_span,
                 identifier,
                 expr,
-                type_annotation_span,
                 type_annotation,
             } => {
                 let expr_checked = self.check_expression(expr)?;
@@ -448,7 +447,7 @@ impl TypeChecker {
                         return Err(TypeCheckError::IncompatibleDimensions {
                             span_operation: *identifier_span,
                             operation: "variable definition".into(),
-                            span_expected: type_annotation_span.unwrap(),
+                            span_expected: dexpr.full_span(),
                             expected_name: "specified dimension",
                             expected_type: type_specified,
                             span_actual: expr.full_span(),