dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.12k stars 1.57k forks source link

Discrepancy between Analyzer and Compiler on nullability propagation in records #55743

Open escamoteur opened 4 months ago

escamoteur commented 4 months ago

I just stumbled upon this here

grafik

The analyzer doesn't tell me that I need a ! or ? with a.j but the compiler complains. If I add a second ! the analyzer complains that it is not needed.

This only happens with records. I tried first to reproduce what I found in our project by assigning to variables. There the compiler didn't complain.

Here we get the problem

void func(A? a)
{
  final ({int x, int y}) x = (x: a!.i, y: a.j);
}

but with

void func(A? a)
{
  final x = a!.i;
  final y = a.j;
}

the compiler doesn't complain

eernstg commented 4 months ago

OK, so record literal flow analysis differs for the two tools. I'd assume that the more detailed analysis is the desired one, which motivates the use of 'area-front-end'. It is surprising to me that the flow analysis differs, but this might be because the ongoing unification hasn't reached the flow analysis yet.

@johnniwinther, do you agree on this description, and the classification as 'area-front-end'?

eernstg commented 4 months ago

@stereotype441, WDYT?

escamoteur commented 4 months ago

@eernstg what would be the correct behaviour according to the language spec? Should one ! be enough?

eernstg commented 4 months ago

We don't have a finalized specification of the flow analysis. See flow-analysis.md for some information about it.

The reason why we'd prefer the analysis where the second ! isn't required is that (1) it is sound, (2) it is less verbose, less redundant, and (3) we already rely on flow analysis to detect similar ordering guarantees in the evaluation of expressions. So one ! should be enough.

escamoteur commented 4 months ago

So the compiler and not the analyzer has the problem. Am 16. Mai 2024, 15:36 +0200 schrieb Erik Ernst @.***>:

We don't have a finalized specification of the flow analysis. See flow-analysis.md for some information about it. The reason why we'd prefer the analysis where the second ! isn't required is that (1) it is sound, (2) it is less verbose, less redundant, and (3) we already rely on flow analysis to detect similar ordering guarantees in the evaluation of expressions. So one ! should be enough. — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>

escamoteur commented 4 months ago

on a side note, (not sure if that would be possible) but I see a difference between using an ! on a non-nullable type to which the analyzer should complain and using a second ! in a case like the above.