Skip to content

Implement Value generics #75518

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 40 commits into from
Sep 5, 2024
Merged

Implement Value generics #75518

merged 40 commits into from
Sep 5, 2024

Conversation

Azoy
Copy link
Contributor

@Azoy Azoy commented Jul 27, 2024

This implements the feature described in the GenericsManifesto here: https://github.com/swiftlang/swift/blob/main/docs/GenericsManifesto.md#generic-value-parameters

One can introduce a value generic parameter by marking the type let in the generic parameter clause:

struct MyType<let N: Int>

and you must declare the value type that this is a value for. At the moment this is limited to Int (but I've left the door open to "easily" expanding it for potential future directions). You can specialize a type with a value generic with an integer literal or by passing another value generic with the same underlying value type:

func test1(with myType: MyType<128>) {
  ...
}

func test2<let M: Int>(with myType: MyType<M>) {
  ...
}

This is still a WIP.

@Azoy
Copy link
Contributor Author

Azoy commented Jul 28, 2024

@swift-ci please smoke test

1 similar comment
@Azoy
Copy link
Contributor Author

Azoy commented Jul 28, 2024

@swift-ci please smoke test

@Azoy Azoy force-pushed the integer-generics branch 2 times, most recently from 09935be to f5408c9 Compare July 30, 2024 16:49
@Azoy
Copy link
Contributor Author

Azoy commented Jul 30, 2024

@swift-ci please smoke test

@Azoy
Copy link
Contributor Author

Azoy commented Jul 30, 2024

swiftlang/llvm-project#9030
@swift-ci please smoke test

@Azoy
Copy link
Contributor Author

Azoy commented Jul 30, 2024

Please test with following pull request:
swiftlang/llvm-project#9030

@swift-ci please smoke test

2 similar comments
@Azoy
Copy link
Contributor Author

Azoy commented Jul 31, 2024

Please test with following pull request:
swiftlang/llvm-project#9030

@swift-ci please smoke test

@Azoy
Copy link
Contributor Author

Azoy commented Jul 31, 2024

Please test with following pull request:
swiftlang/llvm-project#9030

@swift-ci please smoke test

@Azoy Azoy changed the title [WIP] Value generics Implement Value generics Aug 8, 2024
@Azoy Azoy marked this pull request as ready for review August 8, 2024 17:37
@Azoy
Copy link
Contributor Author

Azoy commented Sep 4, 2024

Please test with following pull request:
swiftlang/swift-syntax#2785
swiftlang/llvm-project#9030

@swift-ci please test

@Azoy
Copy link
Contributor Author

Azoy commented Sep 5, 2024

Please test with following pull request:
swiftlang/swift-syntax#2785
swiftlang/llvm-project#9030

@swift-ci please test Windows

1 similar comment
@Azoy
Copy link
Contributor Author

Azoy commented Sep 5, 2024

Please test with following pull request:
swiftlang/swift-syntax#2785
swiftlang/llvm-project#9030

@swift-ci please test Windows

@Azoy Azoy merged commit 45d7ea3 into swiftlang:main Sep 5, 2024
5 checks passed
@Azoy Azoy deleted the integer-generics branch September 5, 2024 22:33
kateinoigakukun added a commit to kateinoigakukun/swift that referenced this pull request Sep 6, 2024
The test was failing on 32-bit platforms after swiftlang#75518
Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

Only saw the changes to parse integer values as types now. I think there are some problems with the current implementation.

Comment on lines +179 to +184
// Eat any '-' preceding the type.
SourceLoc minusLoc;
if (Tok.isMinus()) {
minusLoc = consumeToken();
}

Copy link
Member

Choose a reason for hiding this comment

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

Does this mean that we drop the - in all cases where it’s not followed by an integer literal? So, does this mean that the following code is now valid?

let x: -String = 2

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, this is an issue. I'll make a change that only eats preceding - if the following token is an integer literal.

@@ -1574,7 +1588,7 @@ bool Parser::canParseType() {
return false;
break;
case tok::oper_prefix:
if (Tok.getText() != "~") {
if (Tok.getText() != "~" && Tok.getText() != "-") {
Copy link
Member

Choose a reason for hiding this comment

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

Similar here, does this mean that we return true from canParseType for -String?

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.

7 participants