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

Open
18 tasks
Tracked by #55673
kallentu opened this issue May 6, 2024 · 5 comments
Open
18 tasks
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.
}
  • Error to have an occurrence of super._ as a declaration of a formal parameter in a constructor. This error also occurs in the case where the super parameter has an explicitly declared type and/or default value.
class B {
  final _;
  B(this._);
}

class C {
  C(super._); // Error.
}
  • 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>
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