@@ -5237,96 +5237,14 @@ void test(SemanticModelOptions options)
52375237 }
52385238 }
52395239
5240- [ Fact , WorkItem ( "TODO2 " ) ]
5241- public void Repro_71522 ( )
5240+ [ Fact , WorkItem ( "https://github.com/dotnet/roslyn/issues/71522 " ) ]
5241+ public void CollectionExpression_NestedNullability_01 ( )
52425242 {
52435243 var source = """
52445244 #nullable enable
52455245
5246- using System;
5247- using System.Collections.Generic;
5248- using System.Linq.Expressions;
5249-
5250- class C
5251- {
5252- void M(string[] args)
5253- {
5254- int value = 3;
5255- M(value is 1 ? ["1"] : new[] { "4" });
5256- }
5257- }
5258- """ ;
5259-
5260- var comp = CreateCompilation ( source ) ;
5261- var tree = comp . SyntaxTrees [ 0 ] ;
5262- var model = comp . GetSemanticModel ( tree ) ;
5263-
5264- var root = tree . GetRoot ( ) ;
5265- var arrayCreation = root . DescendantNodes ( ) . OfType < ImplicitArrayCreationExpressionSyntax > ( ) . Single ( ) ;
5266-
5267- var typeInfo = model . GetTypeInfo ( arrayCreation ) ;
5268- var type = ( IArrayTypeSymbol ) typeInfo . Type ;
5269- Assert . Equal ( "System.String[]" , type . ToTestDisplayString ( ) ) ;
5270- Assert . Equal ( PublicNullableAnnotation . NotAnnotated , type . NullableAnnotation ) ;
5271- Assert . Equal ( PublicNullableAnnotation . NotAnnotated , type . ElementNullableAnnotation ) ;
5272-
5273- var collectionExpr = root . DescendantNodes ( ) . OfType < CollectionExpressionSyntax > ( ) . Single ( ) ;
5274- typeInfo = model . GetTypeInfo ( collectionExpr ) ;
5275- type = ( IArrayTypeSymbol ) typeInfo . ConvertedType ;
5276- Assert . Equal ( "System.String[]" , type . ToTestDisplayString ( ) ) ;
5277- Assert . Equal ( PublicNullableAnnotation . NotAnnotated , type . NullableAnnotation ) ;
5278- Assert . Equal ( PublicNullableAnnotation . NotAnnotated , type . ElementNullableAnnotation ) ;
5279- }
5280-
5281- [ Fact , WorkItem ( "TODO2" ) ]
5282- public void Repro_71522_2 ( )
5283- {
5284- var source = """
5285- #nullable enable
5286-
5287- using System;
5288- using System.Collections.Generic;
5289- using System.Linq.Expressions;
5290-
5291- class C
5292- {
5293- void M(bool b)
5294- {
5295- string[] x = ["1"];
5296- }
5297- }
5298- """ ;
5299-
5300- var comp = CreateCompilation ( source ) ;
5301- var tree = comp . SyntaxTrees [ 0 ] ;
5302- var model = comp . GetSemanticModel ( tree ) ;
5303-
5304- var root = tree . GetRoot ( ) ;
5305- var collectionExpr = root . DescendantNodes ( ) . OfType < CollectionExpressionSyntax > ( ) . Single ( ) ;
5306- var typeInfo = model . GetTypeInfo ( collectionExpr ) ;
5307- var type = ( IArrayTypeSymbol ) typeInfo . ConvertedType ;
5308- Assert . Equal ( "System.String[]" , type . ToTestDisplayString ( ) ) ;
5309- Assert . Equal ( PublicNullableAnnotation . NotAnnotated , type . NullableAnnotation ) ;
5310- Assert . Equal ( PublicNullableAnnotation . NotAnnotated , type . ElementNullableAnnotation ) ;
5311- }
5312-
5313- [ Fact , WorkItem ( "TODO2" ) ]
5314- public void Repro_71522_3 ( )
5315- {
5316- var source = """
5317- #nullable enable
5318-
5319- using System;
5320- using System.Collections.Generic;
5321- using System.Linq.Expressions;
5322-
5323- class C
5324- {
5325- void M(bool b)
5326- {
5327- string[] x = b ? ["1"] : null!;
5328- }
5329- }
5246+ var b = false;
5247+ var arr = b ? ["1"] : new[] { "2" };
53305248 """ ;
53315249
53325250 var comp = CreateCompilation ( source ) ;
@@ -5342,8 +5260,8 @@ void M(bool b)
53425260 Assert . Equal ( PublicNullableAnnotation . NotAnnotated , type . ElementNullableAnnotation ) ;
53435261 }
53445262
5345- [ Fact , WorkItem ( "TODO2 " ) ]
5346- public void Repro_71522_4 ( )
5263+ [ Fact , WorkItem ( "https://github.com/dotnet/roslyn/issues/71522 " ) ]
5264+ public void CollectionExpression_NestedNullability_02 ( )
53475265 {
53485266 var source = """
53495267 #nullable enable
@@ -5354,11 +5272,9 @@ public void Repro_71522_4()
53545272
53555273 class C
53565274 {
5357- void M(string[] arr, bool b )
5275+ void M()
53585276 {
5359- //M(b ? ["1"] : new[] { "1" }, b);
5360- // If we replace with the following line instead, the test will pass
5361- M(b ? ["1"] : arr, b);
5277+ string[] x = ["1"];
53625278 }
53635279 }
53645280 """ ;
@@ -5376,14 +5292,14 @@ void M(string[] arr, bool b)
53765292 Assert . Equal ( PublicNullableAnnotation . NotAnnotated , type . ElementNullableAnnotation ) ;
53775293 }
53785294
5379- [ Fact , WorkItem ( "TODO2 " ) ]
5380- public void Repro_71522_5 ( )
5295+ [ Fact , WorkItem ( "https://github.com/dotnet/roslyn/issues/71522 " ) ]
5296+ public void CollectionExpression_NestedNullability_03 ( )
53815297 {
53825298 var source = """
53835299 #nullable enable
53845300
53855301 var b = false;
5386- var arr = b ? ["1"] : new[] { "2" };
5302+ var arr = b switch { true => ["1"], false => new[] { "2" } };
53875303 """ ;
53885304
53895305 var comp = CreateCompilation ( source ) ;
@@ -5396,20 +5312,16 @@ public void Repro_71522_5()
53965312 var type = ( IArrayTypeSymbol ) typeInfo . ConvertedType ;
53975313 Assert . Equal ( "System.String[]" , type . ToTestDisplayString ( ) ) ;
53985314 Assert . Equal ( PublicNullableAnnotation . NotAnnotated , type . NullableAnnotation ) ;
5399-
5400- // TODO2: Unexpectedly getting None here. But we do get NotAnnotated when asking about the new[] expression itself.
5401- // It's quite possible that we are failing to re-visit this node after nullable analysis and associate the proper nullability with it.
54025315 Assert . Equal ( PublicNullableAnnotation . NotAnnotated , type . ElementNullableAnnotation ) ;
54035316 }
54045317
5405- [ Fact , WorkItem ( "TODO2 " ) ]
5406- public void Repro_71522_6_TargetedConditional ( )
5318+ [ Fact , WorkItem ( "https://github.com/dotnet/roslyn/issues/71522 " ) ]
5319+ public void CollectionExpression_NestedNullability_04 ( )
54075320 {
54085321 var source = """
54095322 #nullable enable
54105323
5411- var b = false;
5412- string[] arr = b ? ["1"] : null;
5324+ var arr = new[] { ["1"], new[] { "2" } };
54135325 """ ;
54145326
54155327 var comp = CreateCompilation ( source ) ;
@@ -5422,36 +5334,56 @@ public void Repro_71522_6_TargetedConditional()
54225334 var type = ( IArrayTypeSymbol ) typeInfo . ConvertedType ;
54235335 Assert . Equal ( "System.String[]" , type . ToTestDisplayString ( ) ) ;
54245336 Assert . Equal ( PublicNullableAnnotation . NotAnnotated , type . NullableAnnotation ) ;
5425-
5426- // TODO2: Unexpectedly getting None here. But we do get NotAnnotated when asking about the new[] expression itself.
5427- // It's quite possible that we are failing to re-visit this node after nullable analysis and associate the proper nullability with it.
54285337 Assert . Equal ( PublicNullableAnnotation . NotAnnotated , type . ElementNullableAnnotation ) ;
54295338 }
54305339
5431- [ Fact , WorkItem ( "TODO2 " ) ]
5432- public void Repro_WithSwitchExpr ( )
5340+ [ Fact , WorkItem ( "https://github.com/dotnet/roslyn/issues/71522 " ) ]
5341+ public void CollectionExpression_NestedNullability_05 ( )
54335342 {
54345343 var source = """
54355344 #nullable enable
5345+ public class C
5346+ {
5347+ public string[] M1(bool b)
5348+ {
5349+ var arr = b ? ["1"] : new[] { "2" };
5350+ arr[0] = null; // 1
5351+ return arr;
5352+ }
54365353
5437- var b = false;
5438- var arr = b switch { true => ["1"], false => new[] { "2" } };
5354+ public string[] M2(bool b)
5355+ {
5356+ var arr = new[] { "2" };
5357+ arr = b ? ["1"] : arr;
5358+ arr[0] = null; // 2
5359+ return arr;
5360+ }
5361+ }
54395362 """ ;
54405363
54415364 var comp = CreateCompilation ( source ) ;
5365+ comp . VerifyEmitDiagnostics (
5366+ // (7,18): warning CS8625: Cannot convert null literal to non-nullable reference type.
5367+ // arr[0] = null; // 1
5368+ Diagnostic ( ErrorCode . WRN_NullAsNonNullable , "null" ) . WithLocation ( 7 , 18 ) ,
5369+ // (15,18): warning CS8625: Cannot convert null literal to non-nullable reference type.
5370+ // arr[0] = null; // 2
5371+ Diagnostic ( ErrorCode . WRN_NullAsNonNullable , "null" ) . WithLocation ( 15 , 18 ) ) ;
5372+
54425373 var tree = comp . SyntaxTrees [ 0 ] ;
54435374 var model = comp . GetSemanticModel ( tree ) ;
54445375
54455376 var root = tree . GetRoot ( ) ;
5446- var collectionExpr = root . DescendantNodes ( ) . OfType < CollectionExpressionSyntax > ( ) . Single ( ) ;
5447- var typeInfo = model . GetTypeInfo ( collectionExpr ) ;
5448- var type = ( IArrayTypeSymbol ) typeInfo . ConvertedType ;
5449- Assert . Equal ( "System.String[]" , type . ToTestDisplayString ( ) ) ;
5450- Assert . Equal ( PublicNullableAnnotation . NotAnnotated , type . NullableAnnotation ) ;
5451-
5452- // TODO2: Unexpectedly getting None here. But we do get NotAnnotated when asking about the new[] expression itself.
5453- // It's quite possible that we are failing to re-visit this node after nullable analysis and associate the proper nullability with it.
5454- Assert . Equal ( PublicNullableAnnotation . NotAnnotated , type . ElementNullableAnnotation ) ;
5377+ var collectionExprs = root . DescendantNodes ( ) . OfType < CollectionExpressionSyntax > ( ) . ToArray ( ) ;
5378+ Assert . Equal ( 2 , collectionExprs . Length ) ;
5379+ foreach ( var collectionExpr in collectionExprs )
5380+ {
5381+ var typeInfo = model . GetTypeInfo ( collectionExpr ) ;
5382+ var type = ( IArrayTypeSymbol ) typeInfo . ConvertedType ;
5383+ Assert . Equal ( "System.String[]" , type . ToTestDisplayString ( ) ) ;
5384+ Assert . Equal ( PublicNullableAnnotation . NotAnnotated , type . NullableAnnotation ) ;
5385+ Assert . Equal ( PublicNullableAnnotation . NotAnnotated , type . ElementNullableAnnotation ) ;
5386+ }
54555387 }
54565388 }
54575389}
0 commit comments