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

Disallow inheritance from breaking generic constraints #22064

Open
Graveflo opened this issue Jun 10, 2023 · 2 comments
Open

Disallow inheritance from breaking generic constraints #22064

Graveflo opened this issue Jun 10, 2023 · 2 comments
Labels
Generics Inheritance Invalid Code Acceptance Everything related to compiler not complaining about invalid code

Comments

@Graveflo
Copy link
Contributor

Summary

Inheriting from an object that has a constraint allows the constraint of the base class to be broken without warning.

type
  SomeThing = distinct object
  BaseClass[T: SomeThing] = object of RootObj
    encap: T
  IEraseConstraint[T] = object of BaseClass[T]

discard IEraseConstraint[int]()

Description

In the following example, either IEraseConstraint[T] = object of BaseClass[T] for it's lack of verbosity or IEraseConstraint[int]() for its breaking of base class constrains, should be a compile time error or warning.

Alternatives

No response

Examples

No response

Backwards Compatibility

No response

Links

No response

@metagn
Copy link
Collaborator

metagn commented Jun 10, 2023

Related: #21181

I think the "wildcard" generic parameters are allowed to pass any constraint early on (at the generic declaration) but then the constraint is not checked during instantiation

@Graveflo
Copy link
Contributor Author

Interesting. Sounds like that might be a fairly straight forward fix.
This pattern is very easy to do by mistake and can lead to unintended program states. I think that a warning would go a long way here if nothing else because the constraints can be restated. However, more consistent with the rest of the language and in a design where safety is critical, an error makes the most sense.

@metagn metagn added Generics Inheritance Invalid Code Acceptance Everything related to compiler not complaining about invalid code labels Aug 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Generics Inheritance Invalid Code Acceptance Everything related to compiler not complaining about invalid code
Projects
None yet
Development

No branches or pull requests

2 participants