Skip to content

Remove support for 'ref scoped'#62788

Merged
333fred merged 8 commits intodotnet:mainfrom
jcouv:ref-scoped
Jul 26, 2022
Merged

Remove support for 'ref scoped'#62788
333fred merged 8 commits intodotnet:mainfrom
jcouv:ref-scoped

Conversation

@jcouv
Copy link
Copy Markdown
Member

@jcouv jcouv commented Jul 20, 2022

Fixes #62338

Relates to test plan #59194

@jcouv jcouv self-assigned this Jul 20, 2022
@ghost ghost added the Area-Compilers label Jul 20, 2022
@jcouv jcouv force-pushed the ref-scoped branch 2 times, most recently from ffaac73 to a0c1933 Compare July 20, 2022 22:15
@jcouv jcouv marked this pull request as ready for review July 21, 2022 04:52
@jcouv jcouv requested review from a team as code owners July 21, 2022 04:52
@jcouv jcouv requested a review from cston July 21, 2022 15:42
@jcouv
Copy link
Copy Markdown
Member Author

jcouv commented Jul 22, 2022

@dotnet/roslyn-compiler for review please.

@@ -541,14 +540,28 @@ internal static void CheckParameterModifiers(BaseParameterSyntax parameter, Bind
case SyntaxKind.ScopedKeyword when !parsingFunctionPointerParams:
ModifierUtils.CheckScopedModifierAvailability(parameter, modifier, diagnostics);
{
Copy link
Copy Markdown
Contributor

@cston cston Jul 22, 2022

Choose a reason for hiding this comment

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

{

Minor: This enclosing block is no longer necessary. #Closed

static R F1(ref R r) { Console.WriteLine(r._i); return new R(); }
static R F2(scoped ref R r) { Console.WriteLine(r._i); return new R(); }
static R F3(ref scoped R r) { Console.WriteLine(r._i); return new R(); }
static R F4(scoped ref R r) { Console.WriteLine(r._i); return new R(); }
Copy link
Copy Markdown
Contributor

@cston cston Jul 22, 2022

Choose a reason for hiding this comment

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

static R F4(scoped ref R r) { Console.WriteLine(r._i); return new R(); }

Consider keeping this case and d4 below, to verify the synthesized delegate is shared between d2 and d4. #Closed

@cston
Copy link
Copy Markdown
Contributor

cston commented Jul 22, 2022

public static implicit operator B<T>(in scoped A<T> a) => default;

Consider keeping this and changing the parameter to scoped in A<T> a, to ensure we're testing operators.


In reply to: 1192746687


In reply to: 1192746687


In reply to: 1192746687


In reply to: 1192746687


Refers to: src/Compilers/CSharp/Test/Semantic/Semantics/RefFieldTests.cs:6692 in 95c778a. [](commit_id = 95c778a, deletion_comment = True)

@cston
Copy link
Copy Markdown
Contributor

cston commented Jul 22, 2022

public void M4(scoped ref R<T> r) { }

Let's test ref R<T> r and scoped ref R<T> r pairs instead. In short, keep the M3() methods but change ref scoped to simply ref.


In reply to: 1192753712


In reply to: 1192753712


In reply to: 1192753712


Refers to: src/Compilers/CSharp/Test/Semantic/Semantics/RefFieldTests.cs:8741 in 95c778a. [](commit_id = 95c778a, deletion_comment = True)

@cston
Copy link
Copy Markdown
Contributor

cston commented Jul 22, 2022

public abstract R<T> F3(ref scoped R<T> r);

Let's test ref rather than ref scoped.


In reply to: 1192754176


In reply to: 1192754176


Refers to: src/Compilers/CSharp/Test/Semantic/Semantics/RefFieldTests.cs:8800 in 95c778a. [](commit_id = 95c778a, deletion_comment = True)

@cston
Copy link
Copy Markdown
Contributor

cston commented Jul 22, 2022

R<T> F3(ref scoped R<T> r);

Let's test ref rather than ref scoped.


In reply to: 1192754503


In reply to: 1192754503


Refers to: src/Compilers/CSharp/Test/Semantic/Semantics/RefFieldTests.cs:8860 in 95c778a. [](commit_id = 95c778a, deletion_comment = True)

@cston
Copy link
Copy Markdown
Contributor

cston commented Jul 22, 2022

    var f = (ref scoped Unknown u) => { };

Change to scoped ref?


In reply to: 1192758031


In reply to: 1192758031


Refers to: src/Compilers/CSharp/Test/Semantic/Semantics/RefFieldTests.cs:8968 in 95c778a. [](commit_id = 95c778a, deletion_comment = False)

@cston
Copy link
Copy Markdown
Contributor

cston commented Jul 22, 2022

Extra blank line.


In reply to: 1192759346


Refers to: src/Compilers/CSharp/Test/Semantic/Semantics/RefFieldTests.cs:12301 in 95c778a. [](commit_id = 95c778a, deletion_comment = False)

@cston
Copy link
Copy Markdown
Contributor

cston commented Jul 22, 2022

        var comp = CreateCompilationWithSpanAndMemoryExtensions(source);

CreateCompilation here and for other calls in this test.


In reply to: 1192760273


In reply to: 1192760273


Refers to: src/Compilers/CSharp/Test/Semantic/Semantics/RefFieldTests.cs:12310 in 95c778a. [](commit_id = 95c778a, deletion_comment = False)

static void M(R r0)
{
scoped R r1;
ref readonly scoped R r2 = ref r1;
Copy link
Copy Markdown
Contributor

@cston cston Jul 22, 2022

Choose a reason for hiding this comment

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

ref readonly scoped

We should include a ref readonly example. Perhaps change r3 to scoped ref readonly R r3; #Pending

scoped R r1;
ref readonly scoped R r2 = ref r1;
scoped R r1 = r0;
scoped ref R r3 = ref r0;
Copy link
Copy Markdown
Contributor

@cston cston Jul 22, 2022

Choose a reason for hiding this comment

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

scoped ref R r3

scoped ref readonly R r3 #Closed

@jcouv jcouv requested a review from cston July 25, 2022 17:49
@jcouv
Copy link
Copy Markdown
Member Author

jcouv commented Jul 25, 2022

@dotnet/roslyn-compiler for second review. Thanks

@cston
Copy link
Copy Markdown
Contributor

cston commented Jul 25, 2022

public void M3(ref scoped R<string> r) { } // 3

scoped ref?


In reply to: 1194511793


In reply to: 1194511793


Refers to: src/Compilers/CSharp/Test/Semantic/Semantics/RefFieldTests.cs:8292 in 2d8194d. [](commit_id = 2d8194d, deletion_comment = False)

@cston
Copy link
Copy Markdown
Contributor

cston commented Jul 25, 2022

public override object this[scoped R<string> r] { get { return null; } set { } } // 5

F3(scoped ref R<string> r) and F4(scoped ref R<string> r) instead?


In reply to: 1194512755


Refers to: src/Compilers/CSharp/Test/Semantic/Semantics/RefFieldTests.cs:8353 in 2d8194d. [](commit_id = 2d8194d, deletion_comment = False)

@@ -7343,9 +7076,33 @@ public void DelegateReturnTypeScope(LanguageVersion langVersion)
";
var comp = CreateCompilation(source, parseOptions: TestOptions.Regular.WithLanguageVersion(langVersion));
comp.VerifyEmitDiagnostics(
// (2,14): error CS0106: The modifier 'scoped' is not valid for this item
// (2,14): error CS0246: The type or namespace name 'scoped' could not be found (are you missing a using directive or an assembly reference?)
Copy link
Copy Markdown
Member

@333fred 333fred Jul 26, 2022

Choose a reason for hiding this comment

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

This is an extremely bad parse. I think we should be able to do better. #Resolved

scoped {refModifier} scoped S s3 = ref s;
}}
}}";
var comp = CreateCompilation(source);
comp.VerifyDiagnostics(
// (6,29): error CS9048: The 'scoped' modifier can be used for refs and ref struct values only.
// (6,22): error CS0246: The type or namespace name 'scoped' could not be found (are you missing a using directive or an assembly reference?)
Copy link
Copy Markdown
Member

@333fred 333fred Jul 26, 2022

Choose a reason for hiding this comment

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

Same here. We forbid types named scoped in C# 11, right? This should be unambiguously a bad modifier, not a type name. #Resolved

Copy link
Copy Markdown
Member

@333fred 333fred left a comment

Choose a reason for hiding this comment

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

Done review pass. I went through some of the tests, and I don't think the approach we're taking towards misplaced scoped is good. We should be able to recognize that it's a misplaced modifier and give a proper error, rather than dumping the user into a bad state. In particular, I expect people accidentally typing ref scoped instead of scoped ref to a very common failure (I still don't remember off the top of my head if it's ref readonly or readonly ref), so we need to do better for the error recovery here.


## `scoped` treated as a contextual keyword

***Introduced in Visual Studio 2022 version 17.4.*** Starting in C# 11, types cannot be named `scoped`. The compiler will report an error on all such type names. To work around this, the type name and all usages must be escaped with an `@`:
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

📝 the full implementation of this break will come shortly

@@ -1,5 +1,22 @@
# This document lists known breaking changes in Roslyn after .NET 6 all the way to .NET 7.

## `scoped` treated as a contextual keyword
Copy link
Copy Markdown
Contributor

@cston cston Jul 26, 2022

Choose a reason for hiding this comment

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

Should this be "Types cannot be named scoped" instead, assuming that captures the break, for consistency with the changes for file and required? #Resolved

// ref struct scoped { }
Diagnostic(ErrorCode.WRN_LowerCaseTypeName, "scoped").WithArguments("scoped").WithLocation(5, 12));
// (4,15): warning CS0219: The variable 's4' is assigned but its value is never used
// scoped scoped s4 = default; // 2
Copy link
Copy Markdown
Member

@333fred 333fred Jul 26, 2022

Choose a reason for hiding this comment

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

Are we expecting an error on types named scoped in a future PR? Presumably what's happening is that the parser isn't treating the second scoped as a contextual keyword here? #Resolved

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't expect a blanket error on referencing types names scoped. I only expect that anytime we're looking for scoped, we'll prefer the keyword interpretation to the identifier interpretation.
The behavior for scoped scoped s4 is an existing issue. Filed tracking issue: #62950

public void Local_06(LanguageVersion langVersion)
{
string source =
@"scoped.nested a;
Copy link
Copy Markdown
Contributor

@cston cston Jul 26, 2022

Choose a reason for hiding this comment

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

scoped.nested

It seems odd that scoped.nested is treated as a type name in scoped.nested a; but not in ref scoped.nested b;. #Resolved

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Filed follow-up issue: #62950

string source =
@"using scoped s;
using ref scoped r;
using ref scoped r r2;
Copy link
Copy Markdown
Contributor

@cston cston Jul 26, 2022

Choose a reason for hiding this comment

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

using ref scoped r r2

Covered in Using_02_RefScoped? #Resolved

@"await using scoped s;
await using ref scoped;
await using ref scoped r;
await using ref scoped r r2;
Copy link
Copy Markdown
Contributor

@cston cston Jul 26, 2022

Choose a reason for hiding this comment

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

Covered in Using_04_RefScoped? #Resolved

@jcouv jcouv requested a review from 333fred July 26, 2022 19:45
@jcouv jcouv added this to the 17.4 milestone Jul 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remove ref scoped

4 participants