Skip to content

Analyzer doesn't properly implement horizontal inference when record types are involved #59933

@stereotype441

Description

@stereotype441

Thanks to @mraleph for discovering this issue, and @lrhn for bringing it to my attention.

The following code is accepted by the CFE but not the analyzer:

void main() {
  f(('',), (s) { s.length; });
  g([''], (s) { s.length; });
}

void f<T>((T,) v, void Function(T) fn) {
  fn(v.$1);
}

void g<T>(List<T> v, void Function(T) fn) {
  fn(v[0]);
}

The analyzer reports the error:

  error • test.dart:2:20 • The property 'length' can't be unconditionally accessed because the receiver can be 'null'. Try making the access conditional (using '?.') or adding a null check to the target ('!'). •
          unchecked_use_of_nullable_value

The reason this is happening is that when examining the type signature for f, the analyzer fails to notice that the type variable T appears free in the type (T,), so when analyzing f(('',), (s) { s.length; }), it doesn't schedule a round of horizontal inference between analysis of ('',) and (s) { s.length; }.

I'm working on a fix.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions