From bc2b84130485b76129fbf3765c3f977c01f59de8 Mon Sep 17 00:00:00 2001 From: Adithya Selvaprithiviraj Date: Tue, 29 Oct 2024 11:41:12 -0700 Subject: [PATCH] Error when '=' syntax is used to define UDT (#2721) Today we allow something like `T = Type(Number)` which is incorrect and should not be allowed . this PR adds error to that scenario --------- --- .../Microsoft.PowerFx.Core/Localization/Strings.cs | 1 + .../Microsoft.PowerFx.Core/Parser/TexlParser.cs | 8 ++++++++ src/strings/PowerFxResources.en-US.resx | 7 +++++++ .../ParseTests.cs | 12 ++++++++++++ 4 files changed, 28 insertions(+) diff --git a/src/libraries/Microsoft.PowerFx.Core/Localization/Strings.cs b/src/libraries/Microsoft.PowerFx.Core/Localization/Strings.cs index 4b399d644..f6dc74da5 100644 --- a/src/libraries/Microsoft.PowerFx.Core/Localization/Strings.cs +++ b/src/libraries/Microsoft.PowerFx.Core/Localization/Strings.cs @@ -853,5 +853,6 @@ namespace Microsoft.PowerFx.Core.Localization public static ErrorResourceKey ErrReachedMaxJsonLength = new ErrorResourceKey("ErrReachedMaxJsonLength"); public static ErrorResourceKey ErrUserDefinedTypesDisabled = new ErrorResourceKey("ErrUserDefinedTypesDisabled"); + public static ErrorResourceKey ErrUserDefinedTypeIncorrectSyntax = new ErrorResourceKey("ErrUserDefinedTypeIncorrectSyntax"); } } diff --git a/src/libraries/Microsoft.PowerFx.Core/Parser/TexlParser.cs b/src/libraries/Microsoft.PowerFx.Core/Parser/TexlParser.cs index b6733382e..9774942c8 100644 --- a/src/libraries/Microsoft.PowerFx.Core/Parser/TexlParser.cs +++ b/src/libraries/Microsoft.PowerFx.Core/Parser/TexlParser.cs @@ -362,6 +362,8 @@ namespace Microsoft.PowerFx.Core.Parser } else if (_curs.TidCur == TokKind.Equ) { + var equalToken = _curs.TokCur; + var declaration = script.Substring(declarationStart, _curs.TokCur.Span.Min - declarationStart); _curs.TokMove(); definitionBeforeTrivia.Add(ParseTrivia()); @@ -385,6 +387,12 @@ namespace Microsoft.PowerFx.Core.Parser definitionBeforeTrivia.Add(ParseTrivia()); var result = ParseExpr(Precedence.None); + if (result is TypeLiteralNode _) + { + CreateError(equalToken, TexlStrings.ErrUserDefinedTypeIncorrectSyntax); + continue; + } + namedFormulas.Add(new NamedFormula(thisIdentifier.As(), new Formula(result.GetCompleteSpan().GetFragment(script), result), _startingIndex, attribute)); userDefinitionSourceInfos.Add(new UserDefinitionSourceInfo(index++, UserDefinitionType.NamedFormula, thisIdentifier.As(), declaration, new SourceList(definitionBeforeTrivia), GetExtraTriviaSourceList())); definitionBeforeTrivia = new List(); diff --git a/src/strings/PowerFxResources.en-US.resx b/src/strings/PowerFxResources.en-US.resx index a745ad5ab..bcc81eff3 100644 --- a/src/strings/PowerFxResources.en-US.resx +++ b/src/strings/PowerFxResources.en-US.resx @@ -4846,4 +4846,11 @@ The 'User-defined types' experimental feature is not enabled. Error message returned when user uses User-defined types with the User-defined types feature flag turned off. + + Incorrect syntax: '=' cannot be used to declare user-defined types. Use ':=' instead. + + Error message returned when user uses '=' syntax to define User-defined type. + Some examples for valid named type declarations - "Point := Type({x: Number, y: Number});" , "T1 := Type(Number);" , "T2 := Type([Boolean]);". + + \ No newline at end of file diff --git a/src/tests/Microsoft.PowerFx.Core.Tests.Shared/ParseTests.cs b/src/tests/Microsoft.PowerFx.Core.Tests.Shared/ParseTests.cs index d55678b9b..1695c2f57 100644 --- a/src/tests/Microsoft.PowerFx.Core.Tests.Shared/ParseTests.cs +++ b/src/tests/Microsoft.PowerFx.Core.Tests.Shared/ParseTests.cs @@ -1014,5 +1014,17 @@ namespace Microsoft.PowerFx.Core.Tests Assert.Equal(expectErrors, parseResult.HasErrors); Assert.Equal(isImperative, parseResult.UDFs.First().IsImperative); } + + [Theory] + [InlineData("A = Type(Number);", 0)] + [InlineData("A = \"hello\";B = Type([Boolean]); C = 5;", 2)] + public void TestTypeLiteralInNamedFormula(string script, int namedFormulaCount) + { + var parserOptions = new ParserOptions(); + var parseResult = UserDefinitions.Parse(script, parserOptions, Features.PowerFxV1); + Assert.True(parseResult.HasErrors); + Assert.Equal(namedFormulaCount, parseResult.NamedFormulas.Count()); + Assert.Contains(parseResult.Errors, e => e.MessageKey.Contains("ErrUserDefinedTypeIncorrectSyntax")); + } } }