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

[analyzer] Missing error for generic function instantiation on function object #46590

Closed
eernstg opened this issue Jul 12, 2021 · 3 comments
Closed
Assignees
Labels
analyzer-spec Issues with the analyzer's implementation of the language spec area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on

Comments

@eernstg
Copy link
Member

eernstg commented Jul 12, 2021

Consider the following program:

X id<X>(X x) => x;

void main() {
  int Function(int) f = id; // No problems, instantiates `id` as `id<int>`.
  var idVar = id; // Tears off `id`, yielding a generic function object.
  int Function(int) g = idVar; // Error.
}

The initialization of g is a compile-time error, because idVar is a variable and not a function declaration. However, the analyzer (as of commit ffde158) does not report any errors for this program.

With --enable-experiment=constructor-tearoff we should get an error at the explicit instantiation idVar<int> as well, and that error is also missing. Note that this question was discussed here.

Note that this should not be a breaking change, because the CFE already reports this error.

@eernstg eernstg added area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. analyzer-spec Issues with the analyzer's implementation of the language spec labels Jul 12, 2021
@srawlins srawlins added the P2 A bug or feature request we're likely to work on label Jul 20, 2021
@srawlins
Copy link
Member

Thanks for the report, @eernstg . I'm trying to figure out from the spec what the error condition is.

Let f of the form [...] be an expression that denotes a declaration of a local function, a static method, or a toplevel function, and let G be the static type of f. Consider the situation where G [is a generic function type] and the context type is a nongeneric function type F. In this situation a compile-time error occurs except when the following step succeeds: Generic function type instantiation. [...]

Since, as you say, idVar does not denote a declaration of a local function, a static method, or a toplevel function, then we are not in this situation, and the mentioned compile-time error does not occur.

dart-bot pushed a commit that referenced this issue Jul 26, 2021
Only the following short list of elements are allowed to be type-
instantiated: top-level function, local function, instance method,
constructor, type.

Bug: #46233 and
Change-Id: I37e2a793b7fb25ef670152490e73b227b7c0cdac
#46590
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/207643
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
@eernstg
Copy link
Member Author

eernstg commented Jul 26, 2021

The error condition is that idVar does not denote a declaration of a local function, a static method, or a toplevel function, and in that case we always get a compile-time error (we're trying to assign a generic function to a variable whose type is a non-generic function type, or trying to pass an actual argument or returning a value, etc., in a similar setting, so it's just a type mismatch).

When we operate on a local function, a static method, or a toplevel function, we get a compile-time error unless generic function type instantiation succeeds. (That is, in that particular situation we get a second chance, even though there is a type mismatch: We might be able to "repair" the type mismatch using a generic instantiation).

@eernstg
Copy link
Member Author

eernstg commented Aug 25, 2021

I'm afraid this rule is getting reversed: The language team queried the CFE and backend teams about the need to have the restriction that "we can't perform a generic instantiation on an existing function object" (we've had this rule in the specification for years, but implementations did not enforce it consistently). It turns out to be unnecessary, so we are working on removing that restriction. Cf. dart-lang/language#1812.

I think this is a useful change, we're removing a limitation which was conceptually unjustified (surprising, and based on implementation considerations), and now those implementation considerations turn out to be irrelevant after all. So we might as well just remove the limitation.

So I'll close this issue.

@eernstg eernstg closed this as completed Aug 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-spec Issues with the analyzer's implementation of the language spec area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on
Projects
None yet
Development

No branches or pull requests

2 participants