Skip to content

Optional new and const crash during compilation on Flutter 0.2.7-pre.6 #32737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
timsneath opened this issue Apr 2, 2018 · 18 comments
Closed
Assignees
Labels
crash Process exits with SIGSEGV, SIGABRT, etc. An unhandled exception is not a crash. legacy-area-front-end Legacy: Use area-dart-model instead. P0 A serious issue requiring immediate resolution
Milestone

Comments

@timsneath
Copy link
Contributor

My code is here:
https://github.com/flutter/flutter_attract/tree/dart2

(original code without optional new and const is in the master branch).

Crash:

[C:\git\flutter_attract] flutter run
Using hardware rendering with device Android SDK built for x86. If you get graphics artifacts, consider enabling software rendering with "--enable-software-rendering".
Launching lib/main.dart on Android SDK built for x86 in debug mode...
Initializing gradle...                                       0.9s
Resolving dependencies...                                    1.0s
Running 'gradlew assembleDebug'...
compiler message: lib/main.dart:105:26: Error: The keyword 'const' or 'new' is required here. Due to an implementation limit, the compiler isn't able to
infer 'const' or 'new' here.
compiler message:                   style: TextStyle(
compiler message:                          ^
compiler message: Unhandled exception:
Crash when compiling file:///C:/git/flutter_attract/lib/main.dart,
at character offset 2237:
lib/main.dart:105:26: Internal problem: Unhandled invalid-expression "lib/main.dart:105:26: Error: The keyword 'const' or 'new' is required here. Due to
an implementation limit, the compiler isn't able to infer 'const' or 'new' here.\n                  style: TextStyle(\n                         ^" in defaultTreeNode.
                  style: TextStyle(
                         ^
#0      internalProblem (package:front_end/src/fasta/problems.dart:30)
#1      unhandled (package:front_end/src/fasta/problems.dart:43)
#2      ConstnessEvaluator.defaultTreeNode (package:front_end/src/fasta/kernel/constness_evaluator.dart:112)
#3      TreeVisitor.defaultExpression (package:kernel/visitor.dart:140)
#4      TreeVisitor.visitInvalidExpression (package:kernel/visitor.dart:142)
#5      InvalidExpression.accept (package:kernel/ast.dart:2121)
#6      ConstnessEvaluator.visitConstructorInvocation (package:front_end/src/fasta/kernel/constness_evaluator.dart:175)
#7      ConstructorInvocation.accept (package:kernel/ast.dart:2983)
#8      ConstnessEvaluator.evaluate (package:front_end/src/fasta/kernel/constness_evaluator.dart:99)
#9      evaluateConstness (package:front_end/src/fasta/kernel/constness_evaluator.dart:466)
#10     BodyBuilder.inferConstness (package:front_end/src/fasta/kernel/body_builder.dart:710)
#11     BodyBuilder.finishFunction (package:front_end/src/fasta/kernel/body_builder.dart:697)
#12     DietListener.listenerFinishFunction (package:front_end/src/fasta/source/diet_listener.dart:684)
#13     DietListener.parseFunctionBody (package:front_end/src/fasta/source/diet_listener.dart:718)
#14     DietListener.buildFunctionBody (package:front_end/src/fasta/source/diet_listener.dart:565)
#15     DietListener.endMethod (package:front_end/src/fasta/source/diet_listener.dart:530)
#16     Parser.parseMethod (package:front_end/src/fasta/parser/parser.dart:3796)
#17     Parser.parseClassMemberImpl (package:front_end/src/fasta/parser/parser.dart:3670)
#18     Parser.parseClassBody (package:front_end/src/fasta/parser/parser.dart:3467)
#19     Parser.parseClass (package:front_end/src/fasta/parser/parser.dart:1699)
#20     Parser.parseClassOrNamedMixinApplication (package:front_end/src/fasta/parser/parser.dart:1659)
#21     Parser.parseTopLevelKeywordDeclaration (package:front_end/src/fasta/parser/parser.dart:535)
#22     Parser.parseTopLevelDeclarationImpl (package:front_end/src/fasta/parser/parser.dart:451)
#23     Parser.parseUnit (package:front_end/src/fasta/parser/parser.dart:335)
#24     SourceLoader.buildBody (package:front_end/src/fasta/source/source_loader.dart:198)
<asynchronous suspension>
#25     Loader.buildBodies (package:front_end/src/fasta/loader.dart:157)
<asynchronous suspension>
#26     KernelTarget.buildComponent (package:front_end/src/fasta/kernel/kernel_target.dart:292)
<asynchronous suspension>
#27     IncrementalCompiler.computeDelta.<anonymous closure> (package:front_end/src/fasta/incremental_compiler.dart:140)
<asynchronous suspension>
#28     CompilerContext.runInContext.<anonymous closure> (package:front_end/src/fasta/compiler_context.dart:105)
#29     _rootRun (dart:async/zone.dart:1126)
#30     _CustomZone.run (dart:async/zone.dart:1023)
#31     runZoned (dart:async/zone.dart:1501)
#32     CompilerContext.runInContext (package:front_end/src/fasta/compiler_context.dart:105)
#33     IncrementalCompiler.computeDelta (package:front_end/src/fasta/incremental_compiler.dart:61)
<asynchronous suspension>
#34     IncrementalCompiler.compile (package:vm/incremental_compiler.dart:33)
<asynchronous suspension>
#35     FrontendCompiler.compile.<anonymous closure> (package:vm/frontend_server.dart:254)
#36     new Future.<anonymous closure> (dart:async/future.dart:174)
#37     _rootRun (dart:async/zone.dart:1122)
#38     _CustomZone.run (dart:async/zone.dart:1023)
#39     _CustomZone.runGuarded (dart:async/zone.dart:925)
#40     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:965)
#4
compiler message: 1     _rootRun (dart:async/zone.dart:1126)
#42     _CustomZone.run (dart:async/zone.dart:1023)
#43     _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:949)
#44     Timer._createTimer.<anonymous closure> (dart:async-patch/dart:async/timer_patch.dart:21)
#45     _Timer._runTimers (dart:isolate-patch/dart:isolate/timer_impl.dart:382)
#46     _Timer._handleMessage (dart:isolate-patch/dart:isolate/timer_impl.dart:416)
#47     _RawReceivePortImpl._handleMessage (dart:isolate-patch/dart:isolate/isolate_patch.dart:165)


#0      internalProblem (package:front_end/src/fasta/problems.dart:30)
#1      unhandled (package:front_end/src/fasta/problems.dart:43)
#2      ConstnessEvaluator.defaultTreeNode (package:front_end/src/fasta/kernel/constness_evaluator.dart:112)
#3      TreeVisitor.defaultExpression (package:kernel/visitor.dart:140)
#4      TreeVisitor.visitInvalidExpression (package:kernel/visitor.dart:142)
#5      InvalidExpression.accept (package:kernel/ast.dart:2121)
#6      ConstnessEvaluator.visitConstructorInvocation (package:front_end/src/fasta/kernel/constness_evaluator.dart:175)
#7      ConstructorInvocation.accept (package:kernel/ast.dart:2983)
#8      ConstnessEvaluator.evaluate (package:front_end/src/fasta/kernel/constness_evaluator.dart:99)
#9      evaluateConstness (package:front_end/src/fasta/kernel/constness_evaluator.dart:466)
#10     BodyBuilder.inferConstness (package:front_end/src/fasta/kernel/body_builder.dart:710)
#11     BodyBuilder.finishFunction (package:front_end/src/fasta/kernel/body_builder.dart:697)
#12     DietListener.listenerFinishFunction (package:front_end/src/fasta/source/diet_listener.dart:684)
#13     DietListener.parseFunctionBody (package:front_end/src/fasta/source/diet_listener.dart:718)
#14     DietListener.buildFunctionBody (package:front_end/src/fasta/source/diet_listener.dart:565)
#15     DietListener.endMethod (package:front_end/src/fasta/source/diet_listener.dart:530)
#16     Parser.parseMethod (package:front_end/src/fasta/parser/parser.dart:3796)
#17     Parser.parseClassMemberImpl (package:front_end/src/fasta/parser/parser.dart:3670)
#18     Parser.parseClassBody (package:front_end/src/fasta/parser/parser.dart:3467)
#19     Parser.parseClass (package:front_end/src/fasta/parser/parser.dart:1699)
#20     Parser.parseClassOrNamedMixinApplication (package:front_end/src/fasta/parser/parser.dart:1659)
#21     Parser.parseTopLevelKeywordDeclaration (package:front_end/src/fasta/parser/parser.dart:535)
#22     Parser.parseTopLevelDeclarationImpl (package:front_end/src/fasta/parser/parser.dart:451)
#23     Parser.parseUnit (package:front_end/src/fasta/parser/parser.dart:335)
#24     SourceLoader.buildBody (package:front_end/src/fasta/source/source_loader.dart:198)
<asynchronous suspension>
#25     Loader.buildBodies (package:front_end/src/fasta/loader.dart:157)
<asynchronous suspension>
#26     KernelTarget.buildComponent (package:front_end/src/fasta/kernel/kernel_target.dart:292)
<asynchronous suspension>
#27     IncrementalCompiler.computeDelta.<anonymous closure> (package:front_end/src/fasta/incremental_compiler.dart:140)
<asynchronous suspension>
#28     CompilerContext.runInContext.<anonymous closure> (package:front_end/src/fasta/compiler_context.dart:105)
#29     _rootRun (dart:async/zone.dart:1126)
#30     _CustomZone.run (dart:async/zone.dart:1023)
#31     runZoned (dart:async/zone.dart:1501)
#32     CompilerContext.runInContext (package:front_end/src/fasta/compiler_context.dart:105)
#33     IncrementalCompiler.computeDelta (package:front_end/src/fasta/incremental_compiler.dart:61)
<asynchronous suspension>
#34     IncrementalCompiler.compile (package:vm/incremental_compiler.dart:33)
<asynchronous suspension>
#35     FrontendCompiler.compile.<anonymous closure> (package:vm/frontend_server.dart:254)
#36     new Future.<anonymous closure> (dart:async/future.dart:174)
#37     _rootRun (dart:async/zone.dart:1122)
#38     _CustomZone.run (dart:async/zone.dart:1023)
#39     _CustomZone.runGuarded (dart:async/zone.dart:925)
#40     _CustomZone.bindCallbackGuarded.<anonymo
compiler message: us closure> (dart:async/zone.dart:965)
#41     _rootRun (dart:async/zone.dart:1126)
#42     _CustomZone.run (dart:async/zone.dart:1023)
#43     _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:949)
#44     Timer._createTimer.<anonymous closure> (dart:async-patch/dart:async/timer_patch.dart:21)
#45     _Timer._runTimers (dart:isolate-patch/dart:isolate/timer_impl.dart:382)
#46     _Timer._handleMessage (dart:isolate-patch/dart:isolate/timer_impl.dart:416)
#47     _RawReceivePortImpl._handleMessage (dart:isolate-patch/dart:isolate/isolate_patch.dart:165)

Compiler terminated unexpectedly on C:\git\flutter_attract\lib/main.dart

FAILURE: Build failed with an exception.

* Where:
Script 'C:\git\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 441

* What went wrong:
Execution failed for task ':app:flutterBuildDebug'.
> Process 'command 'C:\git\flutter\bin\flutter.bat'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

* Get more help at https://help.gradle.org

BUILD FAILED in 4s
Gradle build failed: 1

[C:\git\flutter_attract]

Flutter doctor output:

[C:\git\flutter_attract] flutter doctor -v
[√] Flutter (Channel master, v0.2.7-pre.6, on Microsoft Windows [Version 10.0.14393], locale en-US)
    • Flutter version 0.2.7-pre.6 at c:\git\flutter
    • Framework revision 0c89920069 (17 hours ago), 2018-04-01 17:35:05 -0700
    • Engine revision c903c217a1
    • Dart version 2.0.0-dev.43.0.flutter-52afcba357

[√] Android toolchain - develop for Android devices (Android SDK 27.0.3)
    • Android SDK at C:\Users\timsneath\AppData\Local\Android\sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-27, build-tools 27.0.3
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b02)
    • All Android licenses accepted.

[√] Android Studio (version 3.1)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b02)

[√] VS Code, 64-bit edition (version 1.21.1)
    • VS Code at C:\Program Files\Microsoft VS Code
    • Dart Code extension version 2.11.1

[√] Connected devices (1 available)
    • Android SDK built for x86 • emulator-5554 • android-x86 • Android 8.1.0 (API 27) (emulator)
@dgrove dgrove added legacy-area-front-end Legacy: Use area-dart-model instead. P1 A high priority bug; for example, a single project is unusable or has many test failures crash Process exits with SIGSEGV, SIGABRT, etc. An unhandled exception is not a crash. labels Apr 2, 2018
@tvolkert
Copy link
Contributor

tvolkert commented Apr 2, 2018

Note from the doctor output that the SDK in the error report is build against 52afcba

@dgrove dgrove added P0 A serious issue requiring immediate resolution and removed P1 A high priority bug; for example, a single project is unusable or has many test failures labels Apr 2, 2018
@dgrove dgrove added this to the Dart2 Beta 3 milestone Apr 2, 2018
@dgrove
Copy link
Contributor

dgrove commented Apr 2, 2018

here's a much-cut-down example that still crashes:

import 'package:flutter/material.dart';

void main() {}

class _MyHomePageState {
  static Widget commentWidget(String comment) {
    return Container(
        child: Padding(
          child: FittedBox(
            fit: BoxFit.contain
          ),
       ),
    );
  }
}

compiler message: lib/main.dart:9:18: Error: The keyword 'const' or 'new' is required here. Due to an implementation limit, the compiler isn't able to infer 'const' or 'new' here.
compiler message:           child: FittedBox(
compiler message:                  ^
compiler message: Unhandled exception:
Crash when compiling file:///usr/local/google/home/dgrove/bugs/32737/flutter_attract/lib/main.dart,
at character offset 126:
lib/main.dart:9:18: Internal problem: Unhandled invalid-expression "lib/main.dart:9:18: Error: The keyword 'const' or 'new' is required here. Due to an implementation limit, the compiler isn't able to infer 'const' or 'new' here.\n          child: FittedBox(\n    
             ^" in defaultTreeNode.
          child: FittedBox(
                 ^
#0      internalProblem (package:front_end/src/fasta/problems.dart:30)
#1      unhandled (package:front_end/src/fasta/problems.dart:43)
#2      ConstnessEvaluator.defaultTreeNode (package:front_end/src/fasta/kernel/constness_evaluator.dart:112)
#3      TreeVisitor.defaultExpression (package:kernel/visitor.dart:140)
#4      TreeVisitor.visitInvalidExpression (package:kernel/visitor.dart:142)
#5      InvalidExpression.accept (package:kernel/ast.dart:2121)
#6      ConstnessEvaluator.visitConstructorInvocation (package:front_end/src/fasta/kernel/constness_evaluator.dart:175)
#7      ConstructorInvocation.accept (package:kernel/ast.dart:2983)
#8      ConstnessEvaluator.evaluate (package:front_end/src/fasta/kernel/constness_evaluator.dart:99)
#9      evaluateConstness (package:front_end/src/fasta/kernel/constness_evaluator.dart:466)
#10     BodyBuilder.inferConstness (package:front_end/src/fasta/kernel/body_builder.dart:710)
#11     BodyBuilder.finishFunction (package:front_end/src/fasta/kernel/body_builder.dart:697)
#12     DietListener.listenerFinishFunction (package:front_end/src/fasta/source/diet_listener.dart:684)

@bwilkerson
Copy link
Member

@dgrove Looking at the cut-down example above (I can't find https://github.com/flutter/flutter_attract/tree/dart2), it looks like the problem is that BoxFit isn't defined. Could you try adding an import of package:flutter/painting.dart and telling me whether you're still seeing an error? (I'd try it myself, but I don't have a machine set up with Flutter at this point.)

@tvolkert
Copy link
Contributor

tvolkert commented Apr 2, 2018

/cc @Hixie re: previous comment

@dgrove
Copy link
Contributor

dgrove commented Apr 2, 2018 via email

@bwilkerson
Copy link
Member

Ok. I managed to get set up enough to verify that BoxFit is exported from material.dart. So the "missing" import was a red herring. Sorry about the distraction.

I don't know everything that's going on, but it appears that the exception is caused by handing the ConstnessEvaluator (the piece that determines whether to infer new or const) a constructor invocation with an argument that has been compiled into an invalid expression. I'm not sure which constructor it's visiting, nor which expression is invalid, but I don't see anything obvious in the code to indicate that the invalid expression is correct. So my suspicion is that the first bug is in the fact that an invalid expression was produced.

The second bug is that invalid code can presumably cause an exception. The ConstnessEvaluator clearly needs to handle this case gracefully.

@Hixie
Copy link
Contributor

Hixie commented Apr 2, 2018

Maybe it's because the constructor for Padding is asserting that padding != null, and that throws (since the padding argument is omitted)?

@peter-ahe-google
Copy link
Contributor

I think this boils down to not yet handling constant fields:

const constTopLevelField = 42;

class C {
  const C(x);
  static const constField = 87;
}

main() {
  C(C.constField);
  C(constTopLevelField);
}

Sorry, I haven't actually tested this locally.

@chloestefantsova
Copy link
Contributor

Thank you for the example, Peter. It is exactly the kind of yet unhandled expression kind that caused the issue. CL 49140 adds the support for static gets, and I was able to compile the example code without error with that.

The compiler crash is another issue. It is because the evaluator didn't support nested expressions that require new/const insertion. So, if the inner expression with implicit new/const leads to a compile-time error, the generated invalid-expression node that signals about this compile-time error is then visited during new/const insertion for the outter expression. But, because invalid expressions aren't explicitly handled by the visitor, it leads to an invocation of defaultTreeNode that throws the exception. CL 49122 is supposed to fix that behavior. Interestingly enough, we didn't have tests to catch this issue when I was landing 59d8596.

@JekCharlsonYu
Copy link
Contributor

github issues closed tied to this bug:
#32553
#32729

@chloestefantsova
Copy link
Contributor

CL 49508 adds the support for more expressions. More coming soon.

@dgrove
Copy link
Contributor

dgrove commented Apr 8, 2018

@kmillikin @stefantsov Is there any work remaining here?

@kmillikin
Copy link

This should be fixed. We will reopen if it recurs.

@AndreHaueisen
Copy link

Just had the same error. Here is the code.

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'dart:async';

void main() {
  return runApp(CBNConcept());
}

class CBNConcept extends StatelessWidget {
  final ThemeData _defaultTheme = ThemeData(
    primaryColor: Colors.grey[300],
    primaryColorLight: Colors.grey[200],
    primaryColorDark: Colors.grey[500],
    primaryColorBrightness: Brightness.light,
    accentColor: Colors.redAccent,
  );

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: "Radio Cbn",
        home: CBNHome(),
        theme: _defaultTheme);
  }
}

class CBNHome extends StatelessWidget {
  final String stubImage = "http://estaticos.globoradio.globo.com/fotos/2017/09/d49d707a-a3f6-49bf-b49a-1c0ef5bbab10.jpg.295x295_q80_box-721%2C1%2C3079%2C2359_detail.jpg";
  final String stubNews = "Presidente diz nao ter duvidas da responsabilidade do regime sirio";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Radio CBN")),
      body: Card(
        child: Container(
          margin: EdgeInsets.all(8.0),
          child: Column(
            children: <Widget>[
              Image.network(stubImage),
              Text(
                "Emmanuel Macron",
                style: TextStyle(fontSize: 18.0),
              ),
              Text(
                stubNews,
                style: TextStyle(fontSize: 14.0),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

After adding new keyword it compiled

Flutter Doctor:

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel beta, v0.2.8, on Microsoft Windows [Version 10.0.16299.371], locale en-US)
[√] Android toolchain - develop for Android devices (Android SDK 27.0.3)
[√] Android Studio (version 3.1)
[√] Connected devices (1 available)

• No issues found!

@timsneath
Copy link
Contributor Author

Hi @AndreHaueisen -- thanks for building with Flutter!

The fix hasn't made its way to the beta channel yet, but I've tested the code you wrote and it works perfectly without new on the latest builds from the dev and master channels.

You can try for yourself by running flutter channel dev and then flutter upgrade, if you're comfortable to be closer to the bleeding edge.

@AndreHaueisen
Copy link

Hi @timsneath
My mistake then. Thanks for clarifying that.
Keep up the good work.

@rajeshzmoke
Copy link

@timsneath but i could have widgets without 'new' in beta 1 with dart 2 enabled.. but i cant do the same in beta 2 with dart 2 enabled...why?

@kmillikin
Copy link

Hi @rajeshzmoke. That's due to churn as we (the Dart team) sorted out how this feature should work.

In beta 1, we would infer new almost everywhere, unless you were already inside an outer const.

We wanted to try to see if we could come up with a simple way to infer const in more places when there was a const constructor available. While we worked on that, we made it an error to have code that relied on us inferring const or new where both were possible so you wouldn't rely on behavior that we intended to change. Inferring const was still OK if it was required, and inferring new was still OK if const wasn't possible.

If all your widgets have const constructors, then it will probably look quite a bit like the feature just doesn't work in beta 2 at all.

We've settled on the original (beta 1) behavior for the time being, so all this churn was unnecessary and I apologize for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crash Process exits with SIGSEGV, SIGABRT, etc. An unhandled exception is not a crash. legacy-area-front-end Legacy: Use area-dart-model instead. P0 A serious issue requiring immediate resolution
Projects
None yet
Development

No branches or pull requests