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

Fix #2546. Add constant context tests #2547

Merged
merged 3 commits into from
Feb 19, 2024
Merged

Conversation

sgrekhov
Copy link
Contributor

No description provided.

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! Suggested a couple of adjustments.

}

main() {
const c1 = (){C(1);};
Copy link
Member

Choose a reason for hiding this comment

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

This is actually the initializing expression, not a subexpression of any other expression.

Perhaps?:

Suggested change
const c1 = (){C(1);};
const c1 = C((){});

c2 needs a similar adjustment, but c3 is fine (except that perhaps the ^^^ should be adjusted).

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 to C((){C(1);}). According to the description C(1) must be a subexpression of a function literal, therefore I added it to the body

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I misread that!

It's tricky because it seems to be a kind of negation of the following: e is in a constant context if "e is an immediate subexpression of an expression e0 which occurs in a constant context, where e0 is not a function literal".

However, the negation of that is something like "e is not in a constant context if e is not an immediate subexpression of any other expression, or it is an immediate subexpression of an expression e0 that doesn't occur in a constant context, or it is an immediate subexpression of an expression e0 that occurs in a constant context and which is a function literal".

But the latter is a compile-time error: It is always an error for a function literal to occur in a constant context (because it's never a constant expression).

That part should probably be taken out. I think it was included because I had a specific feature in mind, namely constant function literals.

If we had had support for constant function literals then we could have had constant expressions using them: const () { print('Hello, world!'); }. In the body of a constant function literal we can have arbitrary code (it can have side effects, it can change mutable state, etc), just like we can have arbitrary code in the body of a top-level function, and a tear-off of that function is still a constant expression. The constant function literal just cannot access any entities with a lifetime that isn't "infinite" (that is, it can refer to top-level declarations and static members of an enclosing class, if any, they cannot refer to instance variables or local scopes).

Anyway, we don't have constant function literals (yet!), so the last option just disappears.

It isn't particularly useful to test how we deal with a lot of different things inside a function literal that occurs in a constant context, because that function literal is always a compile-time error.

However, I don't see any compile-time errors here (except for function literals occurring in a location where a constant expression is required), I just see the distinction between "is in a constant context" and "is not in a constant context".

If we start negating the criterion (like, changing "is not a function literal" to "is a function literal") then we just get into a case where we have "is not in a constant context", which is a location where far, far fewer things are errors.

I guess I just don't get the point when I'm reading that description. ;-)

Copy link
Contributor Author

@sgrekhov sgrekhov left a comment

Choose a reason for hiding this comment

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

Thank you. Updated, please take another look

}

main() {
const c1 = (){C(1);};
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 to C((){C(1);}). According to the description C(1) must be a subexpression of a function literal, therefore I added it to the body

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!

Just one thing—there was a long comment. It still says 'pending', so it was probably never sent, but I hope it will be sent now.

It's mostly about me being confused. ;-)

The point is that we have a rule that says "e is in a constant context if blah-blah", and then we have a test that says something like "assume e and not-blah-blah", and the consequence would be that e is not in a constant context. However, the test description seems to assume that it is an error, but there are of course lots of situations where an expression isn't in a constant context, and it's not an error.

On top of that, it is an error for a function literal to be a subexpression of a constant expression, so we do get an error as claimed.

The whole thing is just a little unclear.

So please take a look at the comment 'Oh, I misread that!' and see if it makes sense to you. ;-)

}

main() {
const c1 = (){C(1);};
Copy link
Member

Choose a reason for hiding this comment

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

Oh, I misread that!

It's tricky because it seems to be a kind of negation of the following: e is in a constant context if "e is an immediate subexpression of an expression e0 which occurs in a constant context, where e0 is not a function literal".

However, the negation of that is something like "e is not in a constant context if e is not an immediate subexpression of any other expression, or it is an immediate subexpression of an expression e0 that doesn't occur in a constant context, or it is an immediate subexpression of an expression e0 that occurs in a constant context and which is a function literal".

But the latter is a compile-time error: It is always an error for a function literal to occur in a constant context (because it's never a constant expression).

That part should probably be taken out. I think it was included because I had a specific feature in mind, namely constant function literals.

If we had had support for constant function literals then we could have had constant expressions using them: const () { print('Hello, world!'); }. In the body of a constant function literal we can have arbitrary code (it can have side effects, it can change mutable state, etc), just like we can have arbitrary code in the body of a top-level function, and a tear-off of that function is still a constant expression. The constant function literal just cannot access any entities with a lifetime that isn't "infinite" (that is, it can refer to top-level declarations and static members of an enclosing class, if any, they cannot refer to instance variables or local scopes).

Anyway, we don't have constant function literals (yet!), so the last option just disappears.

It isn't particularly useful to test how we deal with a lot of different things inside a function literal that occurs in a constant context, because that function literal is always a compile-time error.

However, I don't see any compile-time errors here (except for function literals occurring in a location where a constant expression is required), I just see the distinction between "is in a constant context" and "is not in a constant context".

If we start negating the criterion (like, changing "is not a function literal" to "is a function literal") then we just get into a case where we have "is not in a constant context", which is a location where far, far fewer things are errors.

I guess I just don't get the point when I'm reading that description. ;-)

@sgrekhov
Copy link
Contributor Author

I was in doubt if we need this text because a function literal in a constant context is an error itself. But eventually, I decided to add it because I was thinking that if we have this statement in the spec, that means there are some reasons to add it. No problem; I just deleted this test. Thank you.

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 44be0d3 into dart-lang:master Feb 19, 2024
2 checks passed
copybara-service bot pushed a commit to dart-lang/sdk that referenced this pull request Feb 21, 2024
2024-02-20 sgrekhov22@gmail.com dart-lang/co19#2496. Add missing part file (dart-lang/co19#2552)
2024-02-20 sgrekhov22@gmail.com dart-lang/co19#2549. Update built-in identifier tests. Add missing ones. Part 2 (dart-lang/co19#2551)
2024-02-20 sgrekhov22@gmail.com dart-lang/co19#2549. Update built-in identifier tests. Add missing ones. Part 1 (dart-lang/co19#2550)
2024-02-19 sgrekhov22@gmail.com Fix dart-lang/co19#2546. Add constant context tests (dart-lang/co19#2547)
2024-02-15 sgrekhov22@gmail.com Fixes dart-lang/co19#2496. Fix co19 tests that became failing after test runner update (dart-lang/co19#2497)
2024-02-14 sgrekhov22@gmail.com dart-lang/co19#1400. Add cascade operator test and more constants tests (dart-lang/co19#2545)
2024-02-14 sgrekhov22@gmail.com dart-lang/co19#2350. Add more factory constructor tests (dart-lang/co19#2543)
2024-02-14 sgrekhov22@gmail.com dart-lang/co19#1400. Add representation variable initialization test (dart-lang/co19#2544)
2024-02-13 sgrekhov22@gmail.com dart-lang/co19#2119. Remove unsupported element from Stream.first/lastWhere tests description (dart-lang/co19#2540)
2024-02-13 sgrekhov22@gmail.com dart-lang/co19#2119. Remove unsupported element from ReceivePort.first/lastWhere tests description (dart-lang/co19#2541)
2024-02-13 sgrekhov22@gmail.com dart-lang/co19#2339. Add more tests for the extension method with the name `type` (dart-lang/co19#2542)
2024-02-12 sgrekhov22@gmail.com Fix dart-lang/co19#2535. Add patterns constants tests for extension types (dart-lang/co19#2539)
2024-02-12 sgrekhov22@gmail.com Fix dart-lang/co19#2485. Update`as` and `is` expressions tests with a function type (dart-lang/co19#2538)
2024-02-12 sgrekhov22@gmail.com Fix dart-lang/co19#2536. Fix use of HINT.UNREACHABLE_SWITCH_CASE (dart-lang/co19#2537)
2024-02-09 sgrekhov22@gmail.com dart-lang/co19#2119. Remove unused code, add issues numbers (dart-lang/co19#2534)
2024-02-09 sgrekhov22@gmail.com dart-lang/co19#2529. Fix failing API core tests (dart-lang/co19#2533)
2024-02-09 sgrekhov22@gmail.com dart-lang/co19#2529. Delete API tests with compile-time errors (dart-lang/co19#2531)
2024-02-09 sgrekhov22@gmail.com dart-lang/co19#2529. Fix and enable Iterable/forEach_A03_t01 (dart-lang/co19#2532)

Change-Id: I03e721d3871d5bf2b9773fbf7ebadb2a1804a111
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/353260
Reviewed-by: Alexander Thomas <athom@google.com>
Reviewed-by: Erik Ernst <eernst@google.com>
Commit-Queue: Alexander Thomas <athom@google.com>
/// occurs as `@e` in a construct derived from ⟨metadata⟩
/// @author sgrekhov22@gmail.com

import 'dart:mirrors';
Copy link

Choose a reason for hiding this comment

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

'dart:mirrors' will not work in the VM AOT mode, this test needs to be skipped for AOT builds.

Also 'dart:mirrors' is an unsupported feature, not sure why we need to add new tests to test it's funcitonality

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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.

3 participants