Skip to content

Error: Constant evaluation error: The property 'call' can't be accessed on '...' in a constant expression. #45298

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
iapicca opened this issue Mar 12, 2021 · 4 comments
Labels
closed-as-intended Closed as the reported issue is expected behavior

Comments

@iapicca
Copy link

iapicca commented Mar 12, 2021

Given these issues
dart-lang/language#819
dart-lang/language#823
I'm admit I'm not sure if this is a bug or an expected behavior,
if the latter some warning would be great

to reproduce just run this code

class Foo {
  const Foo();
  int call() => 42;
}

const fourtyTwo = Foo();

class Bar {
  final int Function() secretOfLife;
  const Bar([this.secretOfLife = fourtyTwo]);
}

void main() => print('${Bar().secretOfLife}');
francesco@francesco-yoga720:~/projects/issue$ dart run lib/main.dart
lib/main.dart: Warning: Interpreting this as package URI, 'package:issue/main.dart'.
lib/main.dart:10:34: Error: Constant evaluation error:
  const Bar([this.secretOfLife = fourtyTwo]);
                                 ^
lib/main.dart:10:34: Context: The property 'call' can't be accessed on 'Foo {}' in a constant expression.
 - 'Foo' is from 'package:issue/main.dart' ('lib/main.dart').
  const Bar([this.secretOfLife = fourtyTwo]);
                                 ^
lib/main.dart:10:19: Context: While analyzing:
  const Bar([this.secretOfLife = fourtyTwo]);
[✓] Flutter (Channel master, 2.1.0-11.0.pre.229, on Linux, locale en_US.UTF-8)
    • Flutter version 2.1.0-11.0.pre.229 at /home/francesco/snap/flutter/common/flutter
    • Framework revision d20ec4c7d8 (8 hours ago), 2021-03-11 22:18:51 -0800
    • Engine revision 1b68503bc8
    • Dart version 2.13.0 (build 2.13.0-125.0.dev)

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
    • Android SDK at /home/francesco/Android/Sdk
    • Platform android-30, build-tools 30.0.2
    • ANDROID_SDK_ROOT = /home/francesco/Android/Sdk
    • Java binary at: /usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_282-8u282-b08-0ubuntu1~20.04-b08)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • CHROME_EXECUTABLE = /snap/bin/chromium

[✓] Linux toolchain - develop for Linux desktop
    • clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)
    • cmake version 3.10.2
    • ninja version 1.8.2
    • pkg-config version 0.29.1

[!] Android Studio (not installed)
    • Android Studio not found; download from https://developer.android.com/studio/index.html
      (or visit https://flutter.dev/docs/get-started/install/linux#android-setup for detailed instructions).

[✓] Connected device (2 available)
    • Linux (desktop) • linux  • linux-x64      • Linux
    • Chrome (web)    • chrome • web-javascript • Chromium 89.0.4389.82 snap

! Doctor found issues in 1 category.
@eernstg eernstg added the closed-as-intended Closed as the reported issue is expected behavior label Mar 12, 2021
@eernstg
Copy link
Member

eernstg commented Mar 12, 2021

This is working as intended. The only member access you can have in a constant expression is s.length where s is a constant expression evaluating to a String. The point is that constant expression evaluation should not run arbitrary code.

(This particular design choice has been debated heavily for many years, and there are ongoing discussions about it right now, but the current rules specify that e.call is never a constant expression, no matter what e is).

@eernstg eernstg closed this as completed Mar 12, 2021
@iapicca
Copy link
Author

iapicca commented Mar 12, 2021

@eernstg
it would be great to have some warning for this situation

@eernstg
Copy link
Member

eernstg commented Mar 12, 2021

I'm not quite sure what that warning should say, though...

@eernstg
Copy link
Member

eernstg commented Mar 12, 2021

Note that the following proposal in the language repo is slightly related: dart-lang/language#1048.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-as-intended Closed as the reported issue is expected behavior
Projects
None yet
Development

No branches or pull requests

2 participants