Skip to content

Improve handling of generic specialization and constraints #367

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

Merged
merged 6 commits into from
Apr 30, 2025

Conversation

Cirras
Copy link
Collaborator

@Cirras Cirras commented Apr 17, 2025

This PR was prompted by a name resolution failure caused by an implicit specialization failure, which involved a dynamic array being passed to a generic open array. It was meant to be a small improvement to the analyzer's support for implicit specialization, adding support for routines with generic open array parameters.

In testing that change, I realized that there were cases in generic specialization that necessitate full support for constraints.
Consider the following example:

type
  TFoo = class
    procedure Bar<T: class>(Baz: T); overload;
    procedure Bar<T>(Baz: array of T); overload;
  end;

With the existing behavior (no handling for the class constraint), we're unable to disambiguate a call to TFoo.Bar with a TArray<TObject> argument. We need to eliminate the first overload due to the class constraint being violated.

Naturally, this had API implications (but it wasn't too big a mess):

  • new ConstraintNode AST nodes
  • new Constraint type hierarchy
  • deprecated existing API methods that naively assumed all constraints were class/interface constraints (type references)

@Cirras Cirras requested a review from fourls April 17, 2025 08:07
@Cirras Cirras force-pushed the implicit_specialization_is_not_my_speciality branch from 6c19247 to aab971b Compare April 17, 2025 08:09
Copy link
Collaborator

@fourls fourls left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I have a few minor improvement ideas and questions.

Cirras added 6 commits April 28, 2025 15:07
Previously, our implicit specialization required **exact** mappings of
types to top-level type parameters, but open arrays can go one level
deep with the type parameter as the element type.
Technically speaking, we should never be calling
`NameReferenceNode::getLastName` downstream of the `readNameReference`
method. It means while we're looping over each part of the name
reference, we're looking at the last part of the name each time.

This is a subtle correctness thing, there should be no behavior change.
We don't typically list every new method when they're being added as
part of a new type.
@Cirras Cirras force-pushed the implicit_specialization_is_not_my_speciality branch from aab971b to 162bd38 Compare April 28, 2025 07:01
@Cirras Cirras requested a review from fourls April 28, 2025 07:01
Copy link
Collaborator

@fourls fourls left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@fourls fourls merged commit 249bd47 into master Apr 30, 2025
4 checks passed
@fourls fourls deleted the implicit_specialization_is_not_my_speciality branch April 30, 2025 06:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants