Skip to content

Commit

Permalink
Fix User-Defined Type error messages (#2774)
Browse files Browse the repository at this point in the history
we plan to refer `Type` as a special function in our documentation and
hence changing all references of Type literal in error messages and some
cosmetic changes
  • Loading branch information
adithyaselv authored Dec 16, 2024
1 parent 040e97b commit 6181382
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 74 deletions.
4 changes: 2 additions & 2 deletions src/libraries/Microsoft.PowerFx.Core/Binding/Binder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2566,7 +2566,7 @@ public override void Visit(TypeLiteralNode node)
Contracts.AssertValue(node);

_txb.SetType(node, DType.Error);
_txb.ErrorContainer.Error(node, TexlStrings.ErrTypeLiteral_UnsupportedUsage);
_txb.ErrorContainer.Error(node, TexlStrings.ErrTypeFunction_UnsupportedUsage);
}

// Method to bind TypeLiteralNode from valid context where a type is expected.
Expand All @@ -2592,7 +2592,7 @@ private void VisitType(TypeLiteralNode node)
else
{
_txb.SetType(node, DType.Error);
_txb.ErrorContainer.Error(node, TexlStrings.ErrTypeLiteral_InvalidTypeDefinition, node.ToString());
_txb.ErrorContainer.Error(node, TexlStrings.ErrTypeFunction_InvalidTypeExpression, node.ToString());
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/libraries/Microsoft.PowerFx.Core/Localization/Strings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ internal static class TexlStrings

public static ErrorResourceKey ErrNamedFormula_MissingSemicolon = new ErrorResourceKey("ErrNamedFormula_MissingSemicolon");
public static ErrorResourceKey ErrNamedFormula_MissingValue = new ErrorResourceKey("ErrNamedFormula_MissingValue");
public static ErrorResourceKey ErrNamedType_MissingTypeLiteral = new ErrorResourceKey("ErrNamedType_MissingTypeLiteral");
public static ErrorResourceKey ErrNamedType_MissingTypeExpression = new ErrorResourceKey("ErrNamedType_MissingTypeExpression");
public static ErrorResourceKey ErrUDF_MissingFunctionBody = new ErrorResourceKey("ErrUDF_MissingFunctionBody");
public static ErrorResourceKey ErrNamedFormula_AlreadyDefined = new ErrorResourceKey("ErrNamedFormula_AlreadyDefined");
public static ErrorResourceKey ErrorResource_NameConflict = new ErrorResourceKey("ErrorResource_NameConflict");
Expand All @@ -790,10 +790,10 @@ internal static class TexlStrings
public static ErrorResourceKey ErrUDF_InvalidParamType = new ErrorResourceKey("ErrUDF_InvalidParamType");
public static ErrorResourceKey WrnUDF_ShadowingBuiltInFunction = new ErrorResourceKey("WrnUDF_ShadowingBuiltInFunction");

public static ErrorResourceKey ErrTypeLiteral_InvalidTypeDefinition = new ErrorResourceKey("ErrTypeLiteral_InvalidTypeDefinition");
public static ErrorResourceKey ErrTypeLiteral_UnsupportedUsage = new ErrorResourceKey("ErrTypeLiteral_UnsupportedUsage");
public static ErrorResourceKey ErrTypeFunction_InvalidTypeExpression = new ErrorResourceKey("ErrTypeFunction_InvalidTypeExpression");
public static ErrorResourceKey ErrTypeFunction_UnsupportedUsage = new ErrorResourceKey("ErrTypeFunction_UnsupportedUsage");
public static ErrorResourceKey ErrNamedType_InvalidCycles = new ErrorResourceKey("ErrNamedType_InvalidCycles");
public static ErrorResourceKey ErrNamedType_InvalidTypeDefinition = new ErrorResourceKey("ErrNamedType_InvalidTypeDefinition");
public static ErrorResourceKey ErrNamedType_InvalidTypeDeclaration = new ErrorResourceKey("ErrNamedType_InvalidTypeDeclaration");
public static ErrorResourceKey ErrNamedType_InvalidTypeName = new ErrorResourceKey("ErrNamedType_InvalidTypeName");
public static ErrorResourceKey ErrNamedType_TypeAlreadyDefined = new ErrorResourceKey("ErrNamedType_TypeAlreadyDefined");
public static ErrorResourceKey ErrRecordContainsInvalidFields_Arg = new ErrorResourceKey("ErrRecordContainsInvalidFields_Arg");
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/Microsoft.PowerFx.Core/Parser/TexlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ private ParseUserDefinitionResult ParseUDFsAndNamedFormulas(string script, Parse

if (_curs.TidCur == TokKind.Semicolon)
{
CreateError(thisIdentifier, TexlStrings.ErrNamedType_MissingTypeLiteral);
CreateError(thisIdentifier, TexlStrings.ErrNamedType_MissingTypeExpression);
}

// Extract expression
Expand Down Expand Up @@ -343,7 +343,7 @@ private ParseUserDefinitionResult ParseUDFsAndNamedFormulas(string script, Parse
}
else
{
CreateError(_curs.TokCur, TexlStrings.ErrNamedType_MissingTypeLiteral);
CreateError(_curs.TokCur, TexlStrings.ErrNamedType_MissingTypeExpression);
}

// If the result was an error, keep moving cursor until end of named type expression
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public override bool PreVisit(TableNode node)
{
if (node.ChildNodes.Count > 1)
{
_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeLiteral_InvalidTypeDefinition, node.ToString()));
_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeFunction_InvalidTypeExpression, node.ToString()));
return false;
}

Expand All @@ -121,7 +121,7 @@ public override bool PreVisit(CallNode node)
return true;
}

_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeLiteral_InvalidTypeDefinition, node.ToString()));
_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeFunction_InvalidTypeExpression, node.ToString()));
return false;
}

Expand All @@ -132,89 +132,89 @@ public override bool PreVisit(ListNode node)
return true;
}

_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeLiteral_InvalidTypeDefinition, node.ToString()));
_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeFunction_InvalidTypeExpression, node.ToString()));
return false;
}

// Invalid nodes
public override void Visit(ErrorNode node)
{
_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeLiteral_InvalidTypeDefinition, node.ToString()));
_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeFunction_InvalidTypeExpression, node.ToString()));
}

public override void Visit(BlankNode node)
{
_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeLiteral_InvalidTypeDefinition, node.ToString()));
_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeFunction_InvalidTypeExpression, node.ToString()));
}

public override void Visit(BoolLitNode node)
{
_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeLiteral_InvalidTypeDefinition, node.ToString()));
_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeFunction_InvalidTypeExpression, node.ToString()));
}

public override void Visit(StrLitNode node)
{
_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeLiteral_InvalidTypeDefinition, node.ToString()));
_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeFunction_InvalidTypeExpression, node.ToString()));
}

public override void Visit(NumLitNode node)
{
_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeLiteral_InvalidTypeDefinition, node.ToString()));
_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeFunction_InvalidTypeExpression, node.ToString()));
}

public override void Visit(DecLitNode node)
{
_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeLiteral_InvalidTypeDefinition, node.ToString()));
_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeFunction_InvalidTypeExpression, node.ToString()));
}

public override void Visit(ParentNode node)
{
_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeLiteral_InvalidTypeDefinition, node.ToString()));
_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeFunction_InvalidTypeExpression, node.ToString()));
}

public override void Visit(SelfNode node)
{
_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeLiteral_InvalidTypeDefinition, node.ToString()));
_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeFunction_InvalidTypeExpression, node.ToString()));
}

public override void Visit(TypeLiteralNode node)
{
_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeLiteral_InvalidTypeDefinition, node.ToString()));
_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeFunction_InvalidTypeExpression, node.ToString()));
}

public override bool PreVisit(StrInterpNode node)
{
_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeLiteral_InvalidTypeDefinition, node.ToString()));
_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeFunction_InvalidTypeExpression, node.ToString()));
return false;
}

public override bool PreVisit(DottedNameNode node)
{
_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeLiteral_InvalidTypeDefinition, node.ToString()));
_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeFunction_InvalidTypeExpression, node.ToString()));
return false;
}

public override bool PreVisit(UnaryOpNode node)
{
_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeLiteral_InvalidTypeDefinition, node.ToString()));
_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeFunction_InvalidTypeExpression, node.ToString()));
return false;
}

public override bool PreVisit(BinaryOpNode node)
{
_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeLiteral_InvalidTypeDefinition, node.ToString()));
_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeFunction_InvalidTypeExpression, node.ToString()));
return false;
}

public override bool PreVisit(VariadicOpNode node)
{
_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeLiteral_InvalidTypeDefinition, node.ToString()));
_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeFunction_InvalidTypeExpression, node.ToString()));
return false;
}

public override bool PreVisit(AsNode node)
{
_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeLiteral_InvalidTypeDefinition, node.ToString()));
_errors.Add(new TexlError(node, DocumentErrorSeverity.Severe, TexlStrings.ErrTypeFunction_InvalidTypeExpression, node.ToString()));
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private IReadOnlyDictionary<DName, FormulaType> ResolveTypes()

if (resolvedType == DType.Invalid)
{
_errors.Add(new TexlError(currentType.Type.TypeRoot, DocumentErrorSeverity.Severe, TexlStrings.ErrNamedType_InvalidTypeDefinition, currentType.Ident.Name));
_errors.Add(new TexlError(currentType.Type.TypeRoot, DocumentErrorSeverity.Severe, TexlStrings.ErrNamedType_InvalidTypeDeclaration, currentType.Ident.Name));
continue;
}

Expand Down
34 changes: 17 additions & 17 deletions src/strings/PowerFxResources.en-US.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1817,7 +1817,7 @@
</data>
<data name="IsTypeUOArg2" xml:space="preserve">
<value>type</value>
<comment>function_parameter - Second argument of the IsTypeUO function - Type value - either a typeliteral or global named type.</comment>
<comment>function_parameter - Second argument of the IsTypeUO function - Type value - either a inline type expression or a type name.</comment>
</data>
<data name="AboutIsType_untypedValue" xml:space="preserve">
<value>The untyped value to check if it can be casted as specified type.</value>
Expand Down Expand Up @@ -4185,10 +4185,10 @@
<value>Named formula must be an expression.</value>
<comment>This error message shows up when Named formula is not an expression. For example, a = ;</comment>
</data>
<data name="ErrNamedType_MissingTypeLiteral" xml:space="preserve">
<value>Named type must be a type literal.</value>
<data name="ErrNamedType_MissingTypeExpression" xml:space="preserve">
<value>A named type declaration must be a valid type expression.</value>
<comment>
This error message shows up when Named type is not a type literal. A valid type literal expression is of syntax "Type(Expression)".
This error message shows up when a named type declaration is not valid a type expression. A valid type expression is of syntax "Type(Expression)".
Some examples for valid named type declarations - "Point := Type({x: Number, y: Number});" , "T1 := Type(Number);" , "T2 := Type([Boolean]);".
Some examples for invalid named type declarations - "T1 := 5;" , "T2 := ;" , "T3 := [1, 2, 3];".
</comment>
Expand Down Expand Up @@ -4560,21 +4560,21 @@
<value>Set a variable with an online datasource value (table or record) can cause multiple requests to the server. Soon this will be blocked.</value>
<comment>Warning for makers that a certain capability should be avoided.</comment>
</data>
<data name="ErrTypeLiteral_InvalidTypeDefinition" xml:space="preserve">
<value>Type literal declaration is invalid. The expression '{0}' cannot be used in a type definition.</value>
<comment>Error message when validation of type literal fails.</comment>
<data name="ErrTypeFunction_InvalidTypeExpression" xml:space="preserve">
<value>Type expression is invalid. '{0}' cannot be used in a type expression.</value>
<comment>Error message when validation of type expression fails.</comment>
</data>
<data name="ErrTypeLiteral_UnsupportedUsage" xml:space="preserve">
<value>Unsupported usage: type literals can only be used in type arguments and type definitions.</value>
<comment>Error message shown when a type literal is used in an unsupported expression.</comment>
<data name="ErrTypeFunction_UnsupportedUsage" xml:space="preserve">
<value>Unsupported usage: Type function can only be used in type arguments and type declarations.</value>
<comment>{Locked=Type}Error message shown when Type function is used in an unsupported expression.</comment>
</data>
<data name="ErrNamedType_InvalidTypeDefinition" xml:space="preserve">
<value>Definition of type {0} is invalid.</value>
<comment>Error message when type binding is invalid.</comment>
<data name="ErrNamedType_InvalidTypeDeclaration" xml:space="preserve">
<value>Declaration of type {0} is invalid.</value>
<comment>Error message when type declaration is invalid.</comment>
</data>
<data name="ErrNamedType_InvalidCycles" xml:space="preserve">
<value>Definition of type {0} is invalid and may contain cycles.</value>
<comment>Error message when type binding may have cycles.</comment>
<value>Declaration of type {0} is invalid and may contain cycles.</value>
<comment>Error message when type declaration may contain cycles.</comment>
</data>
<data name="ErrNamedType_InvalidTypeName" xml:space="preserve">
<value>Restricted type name {0} cannot be used.</value>
Expand Down Expand Up @@ -4879,8 +4879,8 @@
<comment>Error Message.</comment>
</data>
<data name="ErrInvalidArgumentExpectedType" xml:space="preserve">
<value>Invalid argument '{0}'. Expected valid type name or type literal.</value>
<comment>Error Message shown to user when a value other name or type literal is passed into AsType, IsType and ParseJSON functions.</comment>
<value>Invalid argument '{0}'. Expected valid type name or inline type expression.</value>
<comment>Error Message shown to user when a value other name or inline type expression is passed into AsType, IsType and ParseJSON functions.</comment>
</data>
<data name="ErrUnsupportedTypeInTypeArgument" xml:space="preserve">
<value>Unsupported type '{0}' in type argument.</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ Error({Kind:ErrorKind.InvalidArgument})
Error({Kind:ErrorKind.InvalidArgument})

>> AsType(ParseJSON("1"), 1)
Errors: Error 23-24: Invalid argument '1'. Expected valid type name or type literal.
Errors: Error 23-24: Invalid argument '1'. Expected valid type name or inline type expression.

>> AsType(ParseJSON("5"), Type(5))
Errors: Error 28-29: Type literal declaration is invalid. The expression '5' cannot be used in a type definition.|Error 27-28: Type literal declaration is invalid. The expression 'Type(5)' cannot be used in a type definition.
Errors: Error 28-29: Type expression is invalid. '5' cannot be used in a type expression.|Error 27-28: Type expression is invalid. 'Type(5)' cannot be used in a type expression.

>> AsType(ParseJSON("true"), UnKnown)
Errors: Error 26-33: Name isn't valid. 'UnKnown' isn't recognized.|Error 0-34: Invalid argument 'UnKnown'. Expected valid type name or type literal.
Errors: Error 26-33: Name isn't valid. 'UnKnown' isn't recognized.|Error 0-34: Invalid argument 'UnKnown'. Expected valid type name or inline type expression.

>> AsType(ParseJSON("fasle"), Boolean)
Error({Kind:ErrorKind.InvalidJSON})
Expand Down Expand Up @@ -134,7 +134,7 @@ Error({Kind:ErrorKind.Div0})
Blank()

>> AsType(ParseJSON("42"), Blank())
Errors: Error 24-31: Invalid argument 'Blank()'. Expected valid type name or type literal.
Errors: Error 24-31: Invalid argument 'Blank()'. Expected valid type name or inline type expression.

>> AsType(ParseJSON("42"), 1/0)
Errors: Error 25-26: Invalid argument '1 / 0'. Expected valid type name or type literal.
Errors: Error 25-26: Invalid argument '1 / 0'. Expected valid type name or inline type expression.
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,4 @@ Error({Kind:ErrorKind.InvalidArgument})

// Type literal should cause error
>> If(Type(Boolean), 1, 2)
Errors: Error 7-8: Unsupported usage: type literals can only be used in type arguments and type definitions.|Error 0-2: The function 'If' has some invalid arguments.
Errors: Error 7-8: Unsupported usage: Type function can only be used in type arguments and type declarations.|Error 0-2: The function 'If' has some invalid arguments.
Loading

0 comments on commit 6181382

Please sign in to comment.