-
Notifications
You must be signed in to change notification settings - Fork 91
Add support for nonnull constraints #830
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -416,6 +416,7 @@ primary_constraint | |
| 'class' | ||
| 'struct' | ||
| 'unmanaged' | ||
| 'notnull' | ||
; | ||
|
||
secondary_constraints | ||
|
@@ -434,7 +435,7 @@ Each *type_parameter_constraints_clause* consists of the token `where`, followed | |
|
||
The list of constraints given in a `where` clause can include any of the following components, in this order: a single primary constraint, one or more secondary constraints, and the constructor constraint, `new()`. | ||
|
||
A primary constraint can be a class type, the ***reference type constraint*** `class`, the ***value type constraint*** `struct`, or the ***unmanaged type constraint*** `unmanaged`. | ||
A primary constraint can be a class type, the ***reference type constraint*** `class`, the ***value type constraint*** `struct`, or the ***nonullable type constraint*** `notnull`. | ||
|
||
A secondary constraint can be a *type_parameter* or *interface_type*. | ||
|
||
|
@@ -490,7 +491,49 @@ Any constraints shall be consistent among dependent type parameters. If type par | |
|
||
It is valid for `S` to have the value type constraint and `T` to have the reference type constraint. Effectively this limits `T` to the types `System.Object`, `System.ValueType`, `System.Enum`, and any interface type. | ||
|
||
If the `where` clause for a type parameter includes a constructor constraint (which has the form `new()`), it is possible to use the `new` operator to create instances of the type ([§12.8.17.2](expressions.md#128172-object-creation-expressions)). Any type argument used for a type parameter with a constructor constraint shall be a value type, a non-abstract class having a public parameterless constructor, or a type parameter having the value type constraint or constructor constraint. | ||
The token `notnull` is neither a keyword nor a contextual keyword. When it is encountered, it will either: | ||
|
||
- Bind to a type named `notnull` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gafter pointed out it could be an alias - we can just strike "named |
||
- Bind to no type, in which case, it is interpreted as the nonnullable type constraint. | ||
|
||
The nonnullable type constraint specifies that a type argument used for the type parameter shall be a non-nullable value type or non-nullable reference type. | ||
|
||
Generic declarations that include the `notnull` constraint may be used in a nullable oblivious context; however, that constraint is ignored, and a warning shall be generated. | ||
|
||
> *Example*: Consider the following: | ||
> | ||
> <!-- Example: {template:"standalone-lib-without-using", name:"TypeParameterConstraints0", expectedWarnings:["CS8714","CS8714","CS8631"], ignoredWarnings:["CS0168"]} --> | ||
> ```csharp | ||
> #nullable enable | ||
> public class C { } | ||
> public class A<T> where T : notnull { } | ||
> public class B1<T> where T : C { } | ||
> public class B2<T> where T : C? { } | ||
> class Test | ||
> { | ||
> static void M() | ||
> { | ||
> // nonnull constraint allows nonnullable struct type argument | ||
> A<int> x1; | ||
> // warning: nonnull constraint prohibits nullable struct type argument | ||
> A<int?> x2; | ||
> // nonnullconstraint allows nonnullable class type argument | ||
> A<C> x3; | ||
> // warning: nonnull constraint prohibits nullable class type argument | ||
> A<C?> x4; | ||
> // nonnullable base class requirement allows nonnullable class type argument | ||
> B1<C> x5; | ||
> // warning: nonnullable base class requirement prohibits nullable class type argument | ||
> B1<C?> x6; | ||
> // nullable base class requirement allows nonnullable class type argument | ||
> B2<C> x7; | ||
> // nullable base class requirement allows nullable class type argument | ||
> B2<C?> x8; | ||
> } | ||
> } | ||
> ``` | ||
|
||
If the `where` clause for a type parameter includes a constructor constraint (which has the form `new()`), it is possible to use the `new` operator to create instances of the type ([§12.8.16.2](expressions.md#128162-object-creation-expressions)). Any type argument used for a type parameter with a constructor constraint shall be a value type, a non-abstract class having a public parameterless constructor, or a type parameter having the value type constraint or constructor constraint. | ||
|
||
It is a compile-time error for *type_parameter_constraints* having a *primary_constraint* of `struct` or `unmanaged` to also have a *constructor_constraint*. | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As per #1246, we believe it is a contextual keyword.