Coder Social home page Coder Social logo

Type promotion failed in named records about sdk HOT 7 OPEN

yiiim avatar yiiim commented on July 2, 2024 1
Type promotion failed in named records

from sdk.

Comments (7)

lrhn avatar lrhn commented on July 2, 2024

This looks like it's working as currently intended. (Edit, narrator voice: It did not.)

Code inside a closure cannot promote a variable if there are assignments to it outside of the closure (maybe only later).
That is the case here.

The fact that the only assignment is to a non-null isn't taken into account. The value may change at a time that is not known to not happen between the a! and the a.isEven, and that's enough to disable promotion.

from sdk.

yiiim avatar yiiim commented on July 2, 2024

So is this an error that belongs to the IDE plug-in? I did not get any error prompts in the vscode Dart plug-in v3.86.0.

from sdk.

yiiim avatar yiiim commented on July 2, 2024

If it's an unnamed record type, this code can work properly.

void main() {
  int? a;
  Future(
    () {
      (String, String) test = (
        "${a!.isEven}",
        "${a.isEven}",
      );
      print(test);
    },
  );
  a = 0;
}

from sdk.

lrhn avatar lrhn commented on July 2, 2024

Good point. It seems like DDC is promoting the variable in one case, so it should in the other too.

from sdk.

johnniwinther avatar johnniwinther commented on July 2, 2024

cc @stereotype441

from sdk.

tp avatar tp commented on July 2, 2024

In our codebase we had a similar case (and given that it's a closure parameter (which could even be final)) I felt this is a stronger example of the issue:

void main() {
  f((x) => [x!.a, x.b]); // OK
  f((x) => (x!.a, x.b)); // OK
  f((x) => (a: x!.a, b: x.b)); // ERR
}

void f(void Function(X?) c) {
  c(X());
}

class X {
  final a = 1;
  final b = 2;
}

passes dart analyze (which wants one to not add a ! to x.b), but fails compilation with

compileDDC
main.dart:3:33: Error: Property 'b' cannot be accessed on 'X?' because it is potentially null.
 - 'X' is from 'package:dartpad_sample/main.dart' ('/tmp/dartpadTJDUDK/lib/main.dart').
Try accessing using ?. instead.
  f((final x) => (a: x!.a, b: x.b));
                                ^

from sdk.

lrhn avatar lrhn commented on July 2, 2024

Seems to be related to information flow between expressions in a record expression that contains named fields.
(As in: it's not there. The promotions of a prior field are not used as starting state for the next field expression, but only if the record expression itself contains named fields.)

void main() {
  ({int x, int y})? a = (x: 42, y: 37) as dynamic;
  var yes = DateTime.now().millisecondsSinceEpoch > 0;
  // Named fields, requires promotion to flow between field expressions.
  // All fail.
  if (yes) {
    print((a!.x, n2: a.y)); // ERR
  }
  if (yes) {
    print((n1: a!.x, a.y)); // ERR
  }
  if (yes) {
    print((n1: a!.x, n2: a.y)); // ERR
  }
  if (yes) {
    print((a!.x, a.y, n3: 0)); // ERR
  }
  // Promotion flow out of record. Works.
  if (yes) {
    print((a!.x, n2: 0));
    print(a.y);
  }
  if (yes) {
    print((n1: a!.x, n2: 0));
    print(a.y);
  }
  // Promotion flow inside field expression. Works.
  if (yes) {
    print((a!.x + a.y, n2: 0));
  }
  if (yes) {
    print((n1: a!.x + a.y, n2: 0));
  }
  // No mamed fields in same record as promotion. Works.
  if (yes) {
    print((a!.x, (n2: a.y)));
    print(a.y);
  }
  if (yes) {
    print((a!.x, a.y));
  }
  // Not records, all work.
  if (yes) {
    print([a!.x, a.y]);
  }
  if (yes) {
    foo(a!.x, a.y);
  }
  if (yes) {
    bar(x: a!.x, y: a.y);
  }
}

void foo([int x = 0, int y = 0]) {
  print("$x,$y");
}

void bar({int x = 0, int y = 0}) {
  print("$x,$y");
}

from sdk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.