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

Method invocations of trait-bound generics need explicit upcasting #6059

Open
amaurremi opened this issue Jan 20, 2025 · 0 comments
Open

Method invocations of trait-bound generics need explicit upcasting #6059

amaurremi opened this issue Jan 20, 2025 · 0 comments
Labels
during 2: compilation of correct program Dafny rejects a valid program during compilation has-workaround: yes There is a known workaround kind: enhancement Enhancements, feature requests, etc. These are NOT bugs, but ways we can improve Dafny part: language definition Relating to the Dafny language definition itself part: resolver Resolution and typechecking

Comments

@amaurremi
Copy link
Contributor

amaurremi commented Jan 20, 2025

Summary

Dafny currently does not allow direct invocation of methods that are defined in a trait, if the invocation is on a generic type bound by that trait:

trait T {
  method foo()
}

method bar<U extends T>(t: U) {
  t.foo();
}

Compiling this code would yield the error member 'foo' does not exist in type parameter 'U'.

Background and Motivation

Even though U is a subtype of T, the compiler does not allow direct invocation of foo on t.

To work around this, we need an explicit cast:

method bar<U extends T>(t: U) {
  (t as T).foo();
}

This behavior is unintuitive and adds unnecessary verbosity.

Proposed Feature

Enhance Dafny's support for bounded polymorphism by allowing direct invocation of methods on trait-bound generics without requiring upcasting. For the example above, t.foo() should compile without errors.

Alternatives

Update the documentation to explain why explicit upcasting in this scenario is required. This would reduce confusion but not resolve the underlying issue of verbosity and usability.

@amaurremi amaurremi added during 2: compilation of correct program Dafny rejects a valid program during compilation has-workaround: yes There is a known workaround kind: enhancement Enhancements, feature requests, etc. These are NOT bugs, but ways we can improve Dafny part: language definition Relating to the Dafny language definition itself part: resolver Resolution and typechecking labels Jan 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
during 2: compilation of correct program Dafny rejects a valid program during compilation has-workaround: yes There is a known workaround kind: enhancement Enhancements, feature requests, etc. These are NOT bugs, but ways we can improve Dafny part: language definition Relating to the Dafny language definition itself part: resolver Resolution and typechecking
Projects
None yet
Development

No branches or pull requests

1 participant