From d13ec50303ce6af30b71360ec62734748abba971 Mon Sep 17 00:00:00 2001 From: Daniel Sainati Date: Wed, 20 Dec 2023 11:13:05 -0500 Subject: [PATCH 1/5] report bespoke error for restricted types parsed as type arguments --- runtime/parser/expression.go | 8 +++++++- runtime/parser/expression_test.go | 16 ++++++++++++++++ runtime/parser/parser_test.go | 8 ++++---- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/runtime/parser/expression.go b/runtime/parser/expression.go index 9d06d9def6..b28f6652fe 100644 --- a/runtime/parser/expression.go +++ b/runtime/parser/expression.go @@ -601,7 +601,7 @@ func defineLessThanOrTypeArgumentsExpression() { return nil }() - // `err` is nil means the expression is an invocation + // `err` is nil means the expression is an invocation, if err == nil { // The expression was determined to be an invocation. @@ -640,6 +640,12 @@ func defineLessThanOrTypeArgumentsExpression() { } else { + // if the error specifically occurred during parsing restricted types, + // this is still an invocation and we should report that error instead + if strings.Contains(err.Error(), "restricted type") { + return nil, err, true + } + // The previous attempt to parse an invocation failed, // replay the buffered tokens. diff --git a/runtime/parser/expression_test.go b/runtime/parser/expression_test.go index b3c27caa96..76489256dd 100644 --- a/runtime/parser/expression_test.go +++ b/runtime/parser/expression_test.go @@ -4474,6 +4474,22 @@ func TestParseLessThanOrTypeArguments(t *testing.T) { result, ) }) + + t.Run("restricted type argument", func(t *testing.T) { + + t.Parallel() + + _, errs := testParseExpression("foo") + utils.AssertEqualWithDiff(t, + []error{ + &SyntaxError{ + Message: "restricted types have been removed; replace with the concrete type or an equivalent intersection type", + Pos: ast.Position{Offset: 6, Line: 1, Column: 6}, + }, + }, + errs, + ) + }) } func TestParseBoolExpression(t *testing.T) { diff --git a/runtime/parser/parser_test.go b/runtime/parser/parser_test.go index 44241e5124..5a0a762cab 100644 --- a/runtime/parser/parser_test.go +++ b/runtime/parser/parser_test.go @@ -506,7 +506,7 @@ func TestParseBuffering(t *testing.T) { fun isneg(x: SignedFixedPoint): Bool { /* I kinda forget what this is all about */ return x /* but we probably need to figure it out */ - < /* ************/((TODO?{/*))************ *// + < /* ************/((TODO?/*))************ *// -x /* maybe it says NaNs are not negative? */ } ` @@ -515,7 +515,7 @@ func TestParseBuffering(t *testing.T) { []error{ &SyntaxError{ Message: "expected token identifier", - Pos: ast.Position{Offset: 399, Line: 9, Column: 95}, + Pos: ast.Position{Offset: 398, Line: 9, Column: 94}, }, }, err.(Error).Errors, @@ -539,8 +539,8 @@ func TestParseBuffering(t *testing.T) { utils.AssertEqualWithDiff(t, []error{ &SyntaxError{ - Message: "expected token identifier", - Pos: ast.Position{Offset: 146, Line: 4, Column: 63}, + Message: "restricted types have been removed; replace with the concrete type or an equivalent intersection type", + Pos: ast.Position{Offset: 138, Line: 4, Column: 55}, }, }, err.(Error).Errors, From d2c0e18a60b712ebf6eb14319cf4ff6655d0874f Mon Sep 17 00:00:00 2001 From: Daniel Sainati Date: Wed, 20 Dec 2023 13:30:50 -0500 Subject: [PATCH 2/5] Update runtime/parser/expression.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Müller --- runtime/parser/expression.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/parser/expression.go b/runtime/parser/expression.go index b28f6652fe..9148d1dda2 100644 --- a/runtime/parser/expression.go +++ b/runtime/parser/expression.go @@ -601,7 +601,7 @@ func defineLessThanOrTypeArgumentsExpression() { return nil }() - // `err` is nil means the expression is an invocation, + // `err` is nil means the expression is an invocation if err == nil { // The expression was determined to be an invocation. From 764a17c7fc7ee6cbd51a075fc3caedbaa750a2cb Mon Sep 17 00:00:00 2001 From: Daniel Sainati Date: Wed, 20 Dec 2023 13:38:22 -0500 Subject: [PATCH 3/5] create new error type for restricted type errors --- runtime/parser/errors.go | 25 +++++++++++++++++++++++++ runtime/parser/expression_test.go | 5 ++--- runtime/parser/parser_test.go | 5 ++--- runtime/parser/type.go | 2 +- runtime/parser/type_test.go | 15 ++++++--------- 5 files changed, 36 insertions(+), 16 deletions(-) diff --git a/runtime/parser/errors.go b/runtime/parser/errors.go index 977b6369c8..b3118e75a1 100644 --- a/runtime/parser/errors.go +++ b/runtime/parser/errors.go @@ -318,3 +318,28 @@ func (e *CustomDestructorError) Error() string { func (e *CustomDestructorError) SecondaryError() string { return "remove the destructor definition" } + +// RestrictedTypeError + +type RestrictedTypeError struct { + Pos ast.Position +} + +var _ ParseError = &CustomDestructorError{} +var _ errors.UserError = &CustomDestructorError{} + +func (*RestrictedTypeError) isParseError() {} + +func (*RestrictedTypeError) IsUserError() {} + +func (e *RestrictedTypeError) StartPosition() ast.Position { + return e.Pos +} + +func (e *RestrictedTypeError) EndPosition(_ common.MemoryGauge) ast.Position { + return e.Pos +} + +func (e *RestrictedTypeError) Error() string { + return "restricted types have been removed; replace with the concrete type or an equivalent intersection type" +} diff --git a/runtime/parser/expression_test.go b/runtime/parser/expression_test.go index 76489256dd..faf6285484 100644 --- a/runtime/parser/expression_test.go +++ b/runtime/parser/expression_test.go @@ -4482,9 +4482,8 @@ func TestParseLessThanOrTypeArguments(t *testing.T) { _, errs := testParseExpression("foo") utils.AssertEqualWithDiff(t, []error{ - &SyntaxError{ - Message: "restricted types have been removed; replace with the concrete type or an equivalent intersection type", - Pos: ast.Position{Offset: 6, Line: 1, Column: 6}, + &RestrictedTypeError{ + Pos: ast.Position{Offset: 6, Line: 1, Column: 6}, }, }, errs, diff --git a/runtime/parser/parser_test.go b/runtime/parser/parser_test.go index 5a0a762cab..9b9ea77676 100644 --- a/runtime/parser/parser_test.go +++ b/runtime/parser/parser_test.go @@ -538,9 +538,8 @@ func TestParseBuffering(t *testing.T) { utils.AssertEqualWithDiff(t, []error{ - &SyntaxError{ - Message: "restricted types have been removed; replace with the concrete type or an equivalent intersection type", - Pos: ast.Position{Offset: 138, Line: 4, Column: 55}, + &RestrictedTypeError{ + Pos: ast.Position{Offset: 138, Line: 4, Column: 55}, }, }, err.(Error).Errors, diff --git a/runtime/parser/type.go b/runtime/parser/type.go index feace57cbc..6a124750a7 100644 --- a/runtime/parser/type.go +++ b/runtime/parser/type.go @@ -503,7 +503,7 @@ func defineIntersectionOrDictionaryType() { return left, nil, true } - return nil, p.syntaxError("restricted types have been removed; replace with the concrete type or an equivalent intersection type"), true + return nil, &RestrictedTypeError{Pos: p.current.StartPos}, true }, ) } diff --git a/runtime/parser/type_test.go b/runtime/parser/type_test.go index 4ea45cc415..d4905d1261 100644 --- a/runtime/parser/type_test.go +++ b/runtime/parser/type_test.go @@ -555,9 +555,8 @@ func TestParseIntersectionType(t *testing.T) { utils.AssertEqualWithDiff(t, []error{ - &SyntaxError{ - Message: "restricted types have been removed; replace with the concrete type or an equivalent intersection type", - Pos: ast.Position{Offset: 2, Line: 1, Column: 2}, + &RestrictedTypeError{ + Pos: ast.Position{Offset: 2, Line: 1, Column: 2}, }, }, errs, @@ -571,9 +570,8 @@ func TestParseIntersectionType(t *testing.T) { _, errs := testParseType("T{U}") utils.AssertEqualWithDiff(t, []error{ - &SyntaxError{ - Message: "restricted types have been removed; replace with the concrete type or an equivalent intersection type", - Pos: ast.Position{Offset: 2, Line: 1, Column: 2}, + &RestrictedTypeError{ + Pos: ast.Position{Offset: 2, Line: 1, Column: 2}, }, }, errs, @@ -587,9 +585,8 @@ func TestParseIntersectionType(t *testing.T) { _, errs := testParseType("T{U , V }") utils.AssertEqualWithDiff(t, []error{ - &SyntaxError{ - Message: "restricted types have been removed; replace with the concrete type or an equivalent intersection type", - Pos: ast.Position{Offset: 2, Line: 1, Column: 2}, + &RestrictedTypeError{ + Pos: ast.Position{Offset: 2, Line: 1, Column: 2}, }, }, errs, From 830154a7c88fab7ff05fbd5045097a198f1fd98f Mon Sep 17 00:00:00 2001 From: Daniel Sainati Date: Wed, 20 Dec 2023 13:40:21 -0500 Subject: [PATCH 4/5] use error type --- runtime/parser/expression.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/parser/expression.go b/runtime/parser/expression.go index 9148d1dda2..16b6209a85 100644 --- a/runtime/parser/expression.go +++ b/runtime/parser/expression.go @@ -642,7 +642,7 @@ func defineLessThanOrTypeArgumentsExpression() { // if the error specifically occurred during parsing restricted types, // this is still an invocation and we should report that error instead - if strings.Contains(err.Error(), "restricted type") { + if _, isRestrictedTypeErr := err.(*RestrictedTypeError); isRestrictedTypeErr { return nil, err, true } From 7c9209c4f7650c3e40b3d887415423fcc64a821c Mon Sep 17 00:00:00 2001 From: Daniel Sainati Date: Wed, 20 Dec 2023 13:48:15 -0500 Subject: [PATCH 5/5] use range --- runtime/parser/errors.go | 10 +--------- runtime/parser/expression_test.go | 5 ++++- runtime/parser/parser_test.go | 5 ++++- runtime/parser/type.go | 2 +- runtime/parser/type_test.go | 15 ++++++++++++--- 5 files changed, 22 insertions(+), 15 deletions(-) diff --git a/runtime/parser/errors.go b/runtime/parser/errors.go index b3118e75a1..ca8ff4823a 100644 --- a/runtime/parser/errors.go +++ b/runtime/parser/errors.go @@ -322,7 +322,7 @@ func (e *CustomDestructorError) SecondaryError() string { // RestrictedTypeError type RestrictedTypeError struct { - Pos ast.Position + ast.Range } var _ ParseError = &CustomDestructorError{} @@ -332,14 +332,6 @@ func (*RestrictedTypeError) isParseError() {} func (*RestrictedTypeError) IsUserError() {} -func (e *RestrictedTypeError) StartPosition() ast.Position { - return e.Pos -} - -func (e *RestrictedTypeError) EndPosition(_ common.MemoryGauge) ast.Position { - return e.Pos -} - func (e *RestrictedTypeError) Error() string { return "restricted types have been removed; replace with the concrete type or an equivalent intersection type" } diff --git a/runtime/parser/expression_test.go b/runtime/parser/expression_test.go index faf6285484..792df7fa23 100644 --- a/runtime/parser/expression_test.go +++ b/runtime/parser/expression_test.go @@ -4483,7 +4483,10 @@ func TestParseLessThanOrTypeArguments(t *testing.T) { utils.AssertEqualWithDiff(t, []error{ &RestrictedTypeError{ - Pos: ast.Position{Offset: 6, Line: 1, Column: 6}, + Range: ast.Range{ + StartPos: ast.Position{Offset: 6, Line: 1, Column: 6}, + EndPos: ast.Position{Offset: 6, Line: 1, Column: 6}, + }, }, }, errs, diff --git a/runtime/parser/parser_test.go b/runtime/parser/parser_test.go index 9b9ea77676..029dc465b6 100644 --- a/runtime/parser/parser_test.go +++ b/runtime/parser/parser_test.go @@ -539,7 +539,10 @@ func TestParseBuffering(t *testing.T) { utils.AssertEqualWithDiff(t, []error{ &RestrictedTypeError{ - Pos: ast.Position{Offset: 138, Line: 4, Column: 55}, + Range: ast.Range{ + StartPos: ast.Position{Offset: 138, Line: 4, Column: 55}, + EndPos: ast.Position{Offset: 139, Line: 4, Column: 56}, + }, }, }, err.(Error).Errors, diff --git a/runtime/parser/type.go b/runtime/parser/type.go index 6a124750a7..412913a2c6 100644 --- a/runtime/parser/type.go +++ b/runtime/parser/type.go @@ -503,7 +503,7 @@ func defineIntersectionOrDictionaryType() { return left, nil, true } - return nil, &RestrictedTypeError{Pos: p.current.StartPos}, true + return nil, &RestrictedTypeError{Range: p.current.Range}, true }, ) } diff --git a/runtime/parser/type_test.go b/runtime/parser/type_test.go index d4905d1261..33d4264188 100644 --- a/runtime/parser/type_test.go +++ b/runtime/parser/type_test.go @@ -556,7 +556,10 @@ func TestParseIntersectionType(t *testing.T) { utils.AssertEqualWithDiff(t, []error{ &RestrictedTypeError{ - Pos: ast.Position{Offset: 2, Line: 1, Column: 2}, + Range: ast.Range{ + StartPos: ast.Position{Offset: 2, Line: 1, Column: 2}, + EndPos: ast.Position{Offset: 2, Line: 1, Column: 2}, + }, }, }, errs, @@ -571,7 +574,10 @@ func TestParseIntersectionType(t *testing.T) { utils.AssertEqualWithDiff(t, []error{ &RestrictedTypeError{ - Pos: ast.Position{Offset: 2, Line: 1, Column: 2}, + Range: ast.Range{ + StartPos: ast.Position{Offset: 2, Line: 1, Column: 2}, + EndPos: ast.Position{Offset: 2, Line: 1, Column: 2}, + }, }, }, errs, @@ -586,7 +592,10 @@ func TestParseIntersectionType(t *testing.T) { utils.AssertEqualWithDiff(t, []error{ &RestrictedTypeError{ - Pos: ast.Position{Offset: 2, Line: 1, Column: 2}, + Range: ast.Range{ + StartPos: ast.Position{Offset: 2, Line: 1, Column: 2}, + EndPos: ast.Position{Offset: 2, Line: 1, Column: 2}, + }, }, }, errs,