Skip to content

[cfe] ConstantExpression.getStaticType() returns DynamicType despite the constant having more precise type #60368

@mkustermann

Description

@mkustermann

Backends often use expr.getStaticType() to get the static type of an expression.

The ConstantExpression implements this by just returning ConstantExpression.type which is often Dynamic as that's the default value. Though instead of this it could rather make it more precise via

@@ -4650,7 +4650,10 @@ class ConstantExpression extends Expression {
       getStaticTypeInternal(context);

   @override
-  DartType getStaticTypeInternal(StaticTypeContext context) => type;
+  DartType getStaticTypeInternal(StaticTypeContext context) => constant.getType(context);

   @override
   R accept<R>(ExpressionVisitor<R> v) => v.visitConstantExpression(this);

This would avoid backends having to workaround this issue and special casing ConstantExpression specially.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-dart-modelFor issues related to conformance to the language spec in the parser, compilers or the CLI analyzer.cfe-encodingsEncoding related CFE issues.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions