Skip to content
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

Type inference error occurred during compilation (Dart 2.19.2) #51431

Closed
ipcjs opened this issue Feb 16, 2023 · 4 comments
Closed

Type inference error occurred during compilation (Dart 2.19.2) #51431

ipcjs opened this issue Feb 16, 2023 · 4 comments

Comments

@ipcjs
Copy link

ipcjs commented Feb 16, 2023

When running the demo_220216 with Dart 2.19.2 and setting the minimum SDK version to 2.17.0, a type inference error occurs. However, with Flutter 3.3/Dart 2.18, there is no problem. Setting the minimum SDK version to 2.18.0+ also does not cause any issues.

Dart Version: Dart SDK version: 2.19.2 (stable) (Tue Feb 7 18:37:17 2023 +0000) on "macos_arm64"

Demo

Code:
demo_220216.zip

bin/demo_220216.dart:

class Value<T> {
  const Value({
    required this.getter,
    required this.setter,
  });
  final T Function() getter;
  final void Function(T value) setter;
}

final v = Value(
  getter: () => 1,
  setter: (value) {
    int intValue = value;
    print(intValue);
  },
);

void main() {
  print(v);
}

pubspec.yaml:

environment:
  # sdk: '>=2.19.0 <3.0.0' # Success
  # sdk: '>=2.18.0 <3.0.0' # Success
  sdk: '>=2.17.0 <3.0.0' # Error: A value of type 'Object?' can't be assigned to a variable of type 'int'.

Except Output:

Instance of 'Value<int>'

Actual Output(Compile error):

bin/demo_220216.dart:13:20: Error: A value of type 'Object?' can't be assigned to a variable of type 'int'.
 - 'Object' is from 'dart:core'.
    int intValue = value;
                   ^
@mraleph
Copy link
Member

mraleph commented Feb 16, 2023

2.18 is the version when horizontal type inference was added to the language. So what you are seeing is expected.

See CHANGELOG.md for more details on this.

@mraleph mraleph closed this as completed Feb 16, 2023
@mraleph
Copy link
Member

mraleph commented Feb 16, 2023

That being said, it's a bit strange that the same error was not reported on 2.18. Probably there was a bug in analyzer in 2.18. Any idea @stereotype441 ?

@ipcjs
Copy link
Author

ipcjs commented Feb 16, 2023

In all the cases I have tried, the analyzer does not report any errors, only the compiler reports errors during compilation.

@ipcjs ipcjs changed the title Type inference error occurred while running (Dart 2.19.2) Type inference error occurred during compilation (Dart 2.19.2) Feb 16, 2023
@stereotype441
Copy link
Member

stereotype441 commented Feb 17, 2023

This looks similar to #51128. The underlying issue with that bug was that back in 2.18, some top level variable initializer expressions received extra type inference in the anlayzer due to an accident of how it does top level inference. The underlying analyzer bug has since been fixed: #51137.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants