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

Fixes #1398. Fix tests that use unreachable code after Never #1402

Merged
merged 2 commits into from
Sep 1, 2022

Conversation

sgrekhov
Copy link
Contributor

No description provided.

Copy link
Member

@stereotype441 stereotype441 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@eernstg eernstg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, except that I don't understand why we would stop reporting a warning for the use of ?. when the receiver has type Never.

/// getter on a receiver of static type Never via a null aware operator.
/// @author sgrekhov@unipro.ru
/// @issue 39866

// Requirements=nnbd-strong

void test(var x) {
if (x is Never) {
x?.toString();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would we stop reporting a warning for ?. because x cannot be null?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for all cases below. Have the analyzer based tools actually stopped emitting this warning?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think warnings are not reported for the unreachable code. @stereotype441 could you please confirm?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, I see what happened. An unintentional consequence of Flag additional code as unreachable due to types Null and Never. is that if (x is Never) no longer promotes the type of x to Never, so these errors no longer appear.

Personally I think that's a good change, so I'm inclined to leave things as they are and just update the CHANGELOG to clarify what happened. My rationale is that promoting a variable's type to Never is unhelpful to users for the same reason that promoting a type to Null is unhelpful--it effectively hides the previous (unpromoted) type, making it impossible for the compiler to help detect errors like misspelled methods and passing the wrong number and type of arguments to methods. The new behavior of simply marking the code unreachable, but leaving the type of the variable alone, is better.

I'll speak with some folks today to confirm whether others think this is a good idea.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, and @sgrekhov, I should mention: if you want to continue testing behaviors that result from an expression having type Never, you can still achieve that by having a function whose return type is Never.

For example instead of this:

void test(var x) {
  if (x is Never) {
    x?.toString();
    ...

You could do this:

Never f() => throw '';
void test(var x) {
  f()?.toString();
  ...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sgrekhov, I've discussed this behavior change with @leafpetersen and it looks like we're going to stick with the current behavior. So I think you should feel free to either (a) land this change as is, or (b) change this test to use the Never f() => throw ''; approach I described above, so that it continues exercising the behavior of Never, as intended.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend using Never f() => throw ''; in order to ensure that the tests actually test the treatment of member accesses on a receiver of type Never.

Another thing is that when x is not promoted to have type Never then lines 27-29 in LanguageFeatures/nnbd/expression_typing_A02_t01.dart are accepted (that is, there is no error there) because x has type dynamic. Surely that's not what the test is intended to test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the tests to use Never foo() => throw Exception();. Please review

@@ -7,47 +7,24 @@
/// Implementations that provide feedback about dead or unreachable code are
/// encouraged to indicate that any arguments to the invocation are unreachable.
///
/// @description Check that it is a warning to call a method, setter, or
/// @description Check that it is not an error to call a method, setter, or
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why we would have this update. Is it from a spec document?

@@ -7,44 +7,21 @@
/// Implementations that provide feedback about dead or unreachable code are
/// encouraged to indicate that any arguments to the invocation are unreachable.
///
/// @description Check that it is a warning to call a method, setter, or
/// getter on a receiver of static type Never via a null aware operator.
/// @description Check that it is not an error to call a method, setter, or
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as last time we had the same description, and same comment below.

@@ -7,47 +7,24 @@
/// Implementations that provide feedback about dead or unreachable code are
/// encouraged to indicate that any arguments to the invocation are unreachable.
///
/// @description Check that it is a warning to call a method, setter, or
/// getter on a receiver of static type Never via a null aware operator. Test
/// @description Check that it is not an error to call a method, setter, or
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

Copy link
Member

@eernstg eernstg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these tests should be changed from using if (x is Never) { ...Never n = x.something ...} to ...Never n = f().something ... where Never f() => throw '';, such that the description of the test matches the actual behavior of the test.

/// getter on a receiver of static type Never via a null aware operator.
/// @author sgrekhov@unipro.ru
/// @issue 39866

// Requirements=nnbd-strong

void test(var x) {
if (x is Never) {
x?.toString();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend using Never f() => throw ''; in order to ensure that the tests actually test the treatment of member accesses on a receiver of type Never.

Another thing is that when x is not promoted to have type Never then lines 27-29 in LanguageFeatures/nnbd/expression_typing_A02_t01.dart are accepted (that is, there is no error there) because x has type dynamic. Surely that's not what the test is intended to test.

Copy link
Member

@eernstg eernstg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@eernstg eernstg merged commit efd1d26 into dart-lang:master Sep 1, 2022
copybara-service bot pushed a commit to dart-lang/sdk that referenced this pull request Sep 12, 2022
2022-09-02 sgrekhov22@gmail.com dart-lang/co19#1399. on clause tests added (dart-lang/co19#1416)
2022-09-02 sgrekhov22@gmail.com dart-lang/co19#1399. [Records] Type annotations and record expressions tests updated (dart-lang/co19#1415)
2022-09-01 sgrekhov22@gmail.com Fixes dart-lang/co19#1398. Fix tests that use unreachable code after `Never` (dart-lang/co19#1402)
2022-08-31 sgrekhov22@gmail.com dart-lang/co19#1399. [Records] Typos in subtyping tests description fixed (dart-lang/co19#1414)
2022-08-31 sgrekhov22@gmail.com dart-lang/co19#1399. [Records] Subtyping tests for records added (dart-lang/co19#1412)
2022-08-31 asashour@yahoo.com Fix typo (dart-lang/co19#1413)
2022-08-30 sgrekhov22@gmail.com dart-lang/co19#1405. BytesBuilder tests moved from dart:io to dart:typed_data (dart-lang/co19#1410)
2022-08-30 sgrekhov22@gmail.com dart-lang/co19#1399. Tests for record types. Part 1 (dart-lang/co19#1395)
2022-08-24 sgrekhov22@gmail.com dart-lang/co19#1405. Don't use deprecated API in co19 tests. Update generated files (dart-lang/co19#1407)
2022-08-24 sgrekhov22@gmail.com dart-lang/co19#1405. Don't use deprecated API in co19 tests (dart-lang/co19#1406)
2022-08-23 sgrekhov22@gmail.com Fixes dart-lang/co19#1361. Private fields promotion tests added (dart-lang/co19#1391)
2022-08-23 sgrekhov22@gmail.com Fixes dart-lang/co19#1394. Add missing compile-error (dart-lang/co19#1396)
2022-08-23 sgrekhov22@gmail.com Fixes dart-lang/co19#1403. Added test that `super` cannot be used as an expression (dart-lang/co19#1404)
2022-08-15 sgrekhov22@gmail.com dart-lang/co19#1388. FFI tests fixed for 32-bit systems (dart-lang/co19#1392)
2022-08-11 sgrekhov22@gmail.com dart-lang/co19#1388. FFI test failures fixed (dart-lang/co19#1389)

Change-Id: I2eee6c193eed1ce8a511f3ef5667ded947fbfad8
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/257700
Reviewed-by: Alexander Thomas <athom@google.com>
@sgrekhov sgrekhov deleted the co19-1398 branch September 29, 2022 09:47
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

Successfully merging this pull request may close these issues.

None yet

3 participants