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

[Wildcard Variables] Language Tests in tests/language #55652

Closed
18 tasks done
Tracked by #55673
kallentu opened this issue May 6, 2024 · 5 comments
Closed
18 tasks done
Tracked by #55673

[Wildcard Variables] Language Tests in tests/language #55652

kallentu opened this issue May 6, 2024 · 5 comments
Assignees
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). feature-wildcard-variables Implementation of the wildcard variables feature type-test

Comments

@kallentu
Copy link
Member

kallentu commented May 6, 2024

Write up language tests for the tests/language repository.
Examples pulled from the spec.

Test Cases

May not be exhaustive.

  • Multiple _ are allowed in function parameters. This includes top-level functions, local functions, function expressions ("lambdas"), instance methods, static methods, constructors, etc. It includes all parameter kinds: simple, field formals, and function-typed formals, etc.:
Foo(_, this._, super._, void _(), {_}) {}

list.where((_) => true);
  • Multiple _ are allowed in local variable declaration statement variables.
  • Multiple _ are allowed in for loop variable declarations.
  • Multiple _ are allowed in catch clause parameters.
  • Multiple _ are allowed in generic type and generic function type parameters.
  • Other declarations can still be named _ as they are today: top-level variables, top-level function names, type names, member names, etc. are unchanged.
  • _ is still a wildcard in patterns
int a;
(_, a) = (1, 2); // Valid.
  • Error if we cannot resolve _ to a member or top-level declaration.
main() {
  _ = 1; // Error.
}

class C {
  var _;

  test() {
    _ = 2; // OK.
  }
}
  • Wildcards do not shadow.
class C {
  var _ = 'field';

  test() {
    var _ = 'local';

    _ = 'assign'; // Assigns to the field and not the local.
  }
}
  • A positional initializing formal named _ does still initialize a field named _ (and you can still have a field with that name)
class C {
  var _;

  C(this._); // OK.
}
  • _ can't be accessed inside an initializer list.
class C {
  var _;
  var other;

  C(this._): other = _; // Error, cannot access `this`.
}
  • The name _ can be used in the body, but this is a reference to the field, not the parameter.
class C {
  var _;

  C(this._) {
    print(_); // OK. Prints the field.
  }
}
  • Error to have two initializing formals named _.
class C {
  var _;
  C(this._, this._); // Error.
}
  • super._ will pass on the given actual argument to the corresponding superconstructor parameter.
class B {
  final _;
  B(this._);
}

class C extends B {
  C(super._); // OK.
}
  • An extension type can have a parameter named _. This means that the representation variable is named _, and no formal parameter name is introduced into any scopes.
extension type E(int _) {
  int get value => _; // OK, the representation variable name is `_`.
  int get sameValue => this._; // OK.
}
  • No error when a local declaration _ isn't used. (Avoid unused variable warnings.)
  • The name _ is not introduced into the enclosing scope.
  • Every kind of declaration named _ which is specified to be wildcarded is indeed accepted without compile-time errors
@kallentu kallentu added area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). type-test labels May 6, 2024
@kallentu kallentu changed the title [Wildcards] Language Tests in tests/language [Wildcard Variables] Language Tests in tests/language May 7, 2024
@kallentu kallentu added the feature-wildcard-variables Implementation of the wildcard variables feature label May 7, 2024
@kallentu kallentu self-assigned this May 7, 2024
@kallentu
Copy link
Member Author

kallentu commented May 7, 2024

cc @dart-lang/language-team

I wrote up some potential test cases in this issue. I'm going to start making language tests for the feature.
Let me know if there's something missing or I should test something in particular.

@lrhn
Copy link
Member

lrhn commented May 7, 2024

Multiple _ are allowed in generic type and generic function type parameters.

Not sure how to read this.
It's out talking about parameters of genetic function types, or about type parameters?
That is, does it allow Function<_, _>() as a genetic function with two anonymous type parameters?

(I'm fine with that, but it is outside the "local variables only" constraint that I had understood. But I just checked the spec, and that's what it says. So perfectly fine, just hard to read the phrasing.)

Error to have an occurrence of super._ as a declaration of a formal parameter in a constructor.

Not sure that needs to be an error.

class S {
  final int x, y;
  S(this.x, this.y);
}
class U extends S {
  U(super._ , super._) ;
}

looks perfectly reasonable to me. Only the position matters anyway, the names would only be for documentation or accessing in an initializer list.
If we allow this._, or a forwarding factory constructor of

  factory S.fwd(int _, int _) = S;

(which there is no reason not to allow) then I see no reason to treat super._ differently.

Checked the spec. It disallows super._ because it breaks the current desugaring specification of super parameters.
That's a bad reason for disallowing an otherwise percent good feature, and s good reason for not using desugaring as specification.

I'll suggest changing that, and allowing super._. even if it means treating _ as binding a fresh name, instead of being a non-binding declaration in that case.

Rest looks fine

@eernstg
Copy link
Member

eernstg commented May 8, 2024

Is this one covered?: Every kind of declaration named _ which is specified to be wildcarded is indeed accepted without compile-time errors, and it does not introduce the name _ into the enclosing scope.

The discussion about super._ being an error or not would be taken here: #55661.

@kallentu
Copy link
Member Author

kallentu commented May 8, 2024

Not sure how to read this.

Yeah, it is about generic type parameters. The examples Bob gave were below. I'll name the tests better than these bullets I wrote, hopefully :)

class T<_> {}
void genericFunction<_>() {}
takeGenericCallback(<_>() => true);

Every kind of declaration named _ which is specified to be wildcarded is indeed accepted without compile-time errors, and it does not introduce the name _ into the enclosing scope.

@eernstg What do you mean by the first part of this line? "Every kind of declaration named _ which is specified to be wildcarded is indeed accepted without compile-time errors". Can you explain this a bit more?

I'll add "it does not introduce the name _ into the enclosing scope " to the list though, thank you.

@eernstg
Copy link
Member

eernstg commented May 13, 2024

What do you mean by ...

I just meant that we're allowed to use the wildcard feature in all the cases where it should be available.

void main() {
  int _ = 1, _ = 2; // Should be accepted.
  int i = _; // There's no `_` in scope, so this is an "unknown identifier" error.

  void localFunction(int _, String _, [Object? _]) { // Declarations OK.
    int i = _; // Unknown identifier.
  }
}

This group of tests would subsume some "doesn't shadow" tests, because that's just one of the consequences of having a declaration that does not introduce a name into the current scope, focusing on the special case where some (directly or indirectly) enclosing scope does have a declaration with the name _.

The reason why I mentioned this criterion is that I think it would be possible to forget one kind of wildcarded declarations, and not get into any conflicts with the other criteria. For example, we should remember that formal type parameters of a class can also be wildcarded:

class C<_> {
  // Stuff that doesn't actually need access to the actual type argument.

  // Also:
  List<_> get emptyList => const <Never>[]; // Unknown identifier.
}

copybara-service bot pushed a commit that referenced this issue May 17, 2024
Making language tests in small batches. These are very basic ones to test that you can have multiple local declarations named `_` in the same namespace without a collision error.

Bug: #55652
Change-Id: I4d00e9072ba745b363d68db72505c599953c41ad
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/366022
Reviewed-by: Lasse Nielsen <lrn@google.com>
Commit-Queue: Kallen Tu <kallentu@google.com>
Reviewed-by: Erik Ernst <eernst@google.com>
copybara-service bot pushed a commit that referenced this issue May 21, 2024
…ations.

Added a little section for late wildcard variables too.

This CL tests mixing top-level wildcard declarations with local wildcard declarations and the non-shadowing, non-binding behaviour.

Bug: #55652
Change-Id: I72e7cfb1b2d80a3934af355579c36252881cf3fb
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/367241
Reviewed-by: Lasse Nielsen <lrn@google.com>
Commit-Queue: Kallen Tu <kallentu@google.com>
copybara-service bot pushed a commit that referenced this issue May 24, 2024
Fix follow up comments with an async rethrow test.
Tests that wildcard patterns still work while mixed with wildcard variables.

Bug: #55652
Change-Id: If6ab4de68ff27ad51215427a7183f1aed7229947
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/367501
Reviewed-by: Erik Ernst <eernst@google.com>
Commit-Queue: Kallen Tu <kallentu@google.com>
copybara-service bot pushed a commit that referenced this issue May 29, 2024
…rameters.

Next set of tests with initializing formal parameters.

Bug: #55652
Change-Id: I1d8ff69249ad75c8065a159c333f91b62b30f769
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/367822
Commit-Queue: Kallen Tu <kallentu@google.com>
Reviewed-by: Lasse Nielsen <lrn@google.com>
copybara-service bot pushed a commit that referenced this issue May 31, 2024
Bug: #55652
Change-Id: I9f17727bcc96bec9fbb01664244a44710f8f02e6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/368061
Reviewed-by: Lasse Nielsen <lrn@google.com>
Commit-Queue: Kallen Tu <kallentu@google.com>
copybara-service bot pushed a commit that referenced this issue Jun 11, 2024
…nums, extension types.

Bug: #55652
Change-Id: Id1c48c3f6345d13c9c5f88224085a863e30f5aaf
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/367985
Reviewed-by: Lasse Nielsen <lrn@google.com>
Commit-Queue: Kallen Tu <kallentu@google.com>
copybara-service bot pushed a commit that referenced this issue Jun 12, 2024
This switch would've failed regardless. This CL switches the types of the typedef R so that this test would pass.

Bug: #55652
Change-Id: Ic36863a38e3197bd9012907caa9f445faf4945b3
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/371021
Commit-Queue: Kallen Tu <kallentu@google.com>
Reviewed-by: Lasse Nielsen <lrn@google.com>
copybara-service bot pushed a commit that referenced this issue Jun 27, 2024
This CL fixes some bugs in a few tests and removes unnamed optional default tests.

Bug: #55652
Change-Id: I41be6eb2f45598803eaeddd99375aa9cdb3fa990
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/373324
Commit-Queue: Kallen Tu <kallentu@google.com>
Reviewed-by: Lasse Nielsen <lrn@google.com>
copybara-service bot pushed a commit that referenced this issue Aug 20, 2024
Update each error test for wildcard variables with their proper expectations.

Bug: #55652
Change-Id: I2c1e2716f501354afcbb9b5297beb854c3a9598a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/381309
Commit-Queue: Kallen Tu <kallentu@google.com>
Reviewed-by: Bob Nystrom <rnystrom@google.com>
copybara-service bot pushed a commit that referenced this issue Sep 4, 2024
Missing a few cases for interactions between wildcard variables and const.

Bug: #55652
Change-Id: I4ae1ca0041b5b8ca3c24ab866bd9d8e19832ef2e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/382963
Commit-Queue: Kallen Tu <kallentu@google.com>
Reviewed-by: Bob Nystrom <rnystrom@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). feature-wildcard-variables Implementation of the wildcard variables feature type-test
Projects
None yet
Development

No branches or pull requests

3 participants