Skip to content

frontend type inference emits "UnknownType" instead of DynamicType #32847

Closed
@sigmundch

Description

@sigmundch

Consider this example:

foo<T>(Iterable<T> Function(int) f) {}

main() {
  foo((int x) => (x > 1) ? [] : [1, 2].map((e) => 1));

  foo((int x) {
    if (x > 1) {
      return [];
    } else {
      return [1, 2].map((e) => e);
    }
  });
}

It appears that type inference correctly infers that the first closure return an Iterable<dynamic>, but it doesn't infer the same thing for the second closure. Instead it generates Iterable<UnknownType>. This causes failures down the line (we noticed the failure also as a crash in the dart2js backend).

Here is the output from dumping the IR with fasta-compile:

> pkg/front_end/tool/fasta compile --strong --dump-ir m.dart                                                                                          
static method foo<T extends core::Object>((core::int) → core::Iterable<self::foo::T> f) → dynamic {}
static method main() → dynamic {                                                     
  self::foo<dynamic>((core::int x) → core::Iterable<dynamic> => (x.{core::num::>}(1) ?{core::Object} <dynamic>[] : <core::int>[1, 2].{core::Iterable::map}<c
ore::int>((core::int e) → core::int => 1)) as{TypeError} core::Iterable<dynamic>);
  self::foo<dynamic>((core::int x) → core::Iterable<<UnknownType>> {                
    if(x.{core::num::>}(1)) {                                                      
      return <dynamic>[];                              
    }                                                                            
    else {                                                              
      return <core::int>[1, 2].{core::Iterable::map}<core::int>((core::int e) → core::int => e);
    }                                                                                    
  });                                                                             
}                        
                                                                                                                                       
Unhandled exception:     
Unsupported operation: serialization of generic DartTypes                                                                                      
#0      BinaryPrinter.defaultDartType (package:kernel/binary/ast_to_binary.dart:1745:5)
#1      UnknownType.accept (package:front_end/src/fasta/type_inference/type_schema.dart:72:16)                                                           
#2      BinaryPrinter.writeNode (package:kernel/binary/ast_to_binary.dart:229:10)                                  
#3      BinaryPrinter.writeNodeList (package:kernel/binary/ast_to_binary.dart:221:7)
#4      BinaryPrinter.visitInterfaceType (package:kernel/binary/ast_to_binary.dart:1644:7)
#5      InterfaceType.accept (package:kernel/ast.dart:4883:34)
#6      BinaryPrinter.writeNode (package:kernel/binary/ast_to_binary.dart:229:10)              
#7      BinaryPrinter.visitFunctionNode (package:kernel/binary/ast_to_binary.dart:949:5)         
#8      FunctionNode.accept (package:kernel/ast.dart:1991:30)                                                                        
#9      BinaryPrinter.writeNode (package:kernel/binary/ast_to_binary.dart:229:10)                                          
#10     BinaryPrinter.visitFunctionExpression (package:kernel/binary/ast_to_binary.dart:1308:5)
#11     FunctionExpression.accept (package:kernel/ast.dart:3510:36)                                                                 
#12     BinaryPrinter.writeNode (package:kernel/binary/ast_to_binary.dart:229:10)
#13     BinaryPrinter.writeNodeList (package:kernel/binary/ast_to_binary.dart:221:7)                             
#14     BinaryPrinter.visitArguments (package:kernel/binary/ast_to_binary.dart:1125:5)            
#15     Arguments.accept (package:kernel/ast.dart:2686:30)                                         
#16     BinaryPrinter.writeNode (package:kernel/binary/ast_to_binary.dart:229:10)                        
#17     BinaryPrinter.visitStaticInvocation (package:kernel/binary/ast_to_binary.dart:1108:5
...

@kmillikin - can someone on the FE side take a look?

Metadata

Metadata

Assignees

Labels

P1A high priority bug; for example, a single project is unusable or has many test failurescustomer-dart2jslegacy-area-front-endLegacy: Use area-dart-model instead.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions