RefEscapingTests.cs 52.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

using System.Linq;
using Microsoft.CodeAnalysis.CSharp.Symbols.Retargeting;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.CSharp.UnitTests.Semantics
{
    [CompilerTrait(CompilerFeature.ReadOnlyReferences)]
    public class RefEscapingTests : CompilingTestBase
    {
        [Fact()]
18
        public void RefLikeReturnEscape()
19 20
        {
            var text = @"
V
vsadov 已提交
21 22
    using System;

23 24 25 26 27 28 29 30 31 32 33
    class Program
    {
        static void Main()
        {
        }

        static ref int Test1(S1 arg)
        {
            return ref (new int[1])[0];
        }

V
vsadov 已提交
34
        static S1 MayWrap(ref Span<int> arg)
35 36 37 38 39 40
        {
            return default;
        }

        static ref int Test3()
        {
V
vsadov 已提交
41
            Span<int> local = stackalloc int[1];
42
            return ref Test1(MayWrap(ref local));
43 44 45 46 47 48 49
        }

        ref struct S1
        {
        }
    }
";
V
vsadov 已提交
50 51
            CreateCompilationWithMscorlibAndSpan(text).VerifyDiagnostics(
                // (23,42): error CS8526: Cannot use local 'local' in this context because it may expose referenced variables outside of their declaration scope
52
                //             return ref Test1(MayWrap(ref local));
V
vsadov 已提交
53 54
                Diagnostic(ErrorCode.ERR_EscapeLocal, "local").WithArguments("local").WithLocation(23, 42),
                // (23,30): error CS8521: Cannot use a result of 'Program.MayWrap(ref Span<int>)' in this context because it may expose variables referenced by parameter 'arg' outside of their declaration scope
55
                //             return ref Test1(MayWrap(ref local));
V
vsadov 已提交
56 57
                Diagnostic(ErrorCode.ERR_EscapeCall, "MayWrap(ref local)").WithArguments("Program.MayWrap(ref System.Span<int>)", "arg").WithLocation(23, 30),
                // (23,24): error CS8521: Cannot use a result of 'Program.Test1(Program.S1)' in this context because it may expose variables referenced by parameter 'arg' outside of their declaration scope
58
                //             return ref Test1(MayWrap(ref local));
V
vsadov 已提交
59
                Diagnostic(ErrorCode.ERR_EscapeCall, "Test1(MayWrap(ref local))").WithArguments("Program.Test1(Program.S1)", "arg").WithLocation(23, 24)
60 61 62 63
            );
        }

        [Fact()]
64
        public void RefLikeReturnEscape1()
65 66
        {
            var text = @"
V
vsadov 已提交
67 68
    using System;

69 70 71 72 73 74 75 76 77 78 79
    class Program
    {
        static void Main()
        {
        }

        static ref int Test1(S1 arg)
        {
            return ref (new int[1])[0];
        }

V
vsadov 已提交
80
        static S1 MayWrap(Span<int> arg)
81 82 83 84 85 86
        {
            return default;
        }

        static ref int Test3()
        {
V
vsadov 已提交
87 88
            Span<int> local = stackalloc int[1];
            var sp = MayWrap(local);
89 90 91 92 93 94 95 96
            return ref Test1(sp);
        }

        ref struct S1
        {
        }
    }
";
V
vsadov 已提交
97 98
            CreateCompilationWithMscorlibAndSpan(text).VerifyDiagnostics(
                // (24,30): error CS8526: Cannot use local 'sp' in this context because it may expose referenced variables outside of their declaration scope
99
                //             return ref Test1(sp);
V
vsadov 已提交
100 101
                Diagnostic(ErrorCode.ERR_EscapeLocal, "sp").WithArguments("sp").WithLocation(24, 30),
                // (24,24): error CS8521: Cannot use a result of 'Program.Test1(Program.S1)' in this context because it may expose variables referenced by parameter 'arg' outside of their declaration scope
102
                //             return ref Test1(sp);
V
vsadov 已提交
103
                Diagnostic(ErrorCode.ERR_EscapeCall, "Test1(sp)").WithArguments("Program.Test1(Program.S1)", "arg").WithLocation(24, 24)
104 105
            );
        }
V
vsadov 已提交
106

107 108 109 110
        [Fact()]
        public void RefLikeReturnEscapeWithRefLikes()
        {
            var text = @"
V
vsadov 已提交
111
    using System;
112 113 114 115 116 117 118 119 120 121 122
    class Program
    {
        static void Main()
        {
        }

        static ref int Test1(ref S1 arg)
        {
            return ref (new int[1])[0];
        }

V
vsadov 已提交
123
        static S1 MayWrap(Span<int> arg)
124 125 126 127 128 129
        {
            return default;
        }

        static ref int Test3()
        {
V
vsadov 已提交
130 131 132
            Span<int> local = stackalloc int[1];
            var sp = MayWrap(local);
            return ref Test1(ref sp);    // error1
133 134 135 136
        }

        static ref int Test4()
        {
V
vsadov 已提交
137 138
            var sp = MayWrap(default);
            return ref Test1(ref sp);    // error2
139 140 141 142 143 144 145
        }

        ref struct S1
        {
        }
    }
";
V
vsadov 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158
            CreateCompilationWithMscorlibAndSpan(text).VerifyDiagnostics(
                // (23,34): error CS8168: Cannot return local 'sp' by reference because it is not a ref local
                //             return ref Test1(ref sp);    // error1
                Diagnostic(ErrorCode.ERR_RefReturnLocal, "sp").WithArguments("sp").WithLocation(23, 34),
                // (23,24): error CS8521: Cannot use a result of 'Program.Test1(ref Program.S1)' in this context because it may expose variables referenced by parameter 'arg' outside of their declaration scope
                //             return ref Test1(ref sp);    // error1
                Diagnostic(ErrorCode.ERR_EscapeCall, "Test1(ref sp)").WithArguments("Program.Test1(ref Program.S1)", "arg").WithLocation(23, 24),
                // (29,34): error CS8168: Cannot return local 'sp' by reference because it is not a ref local
                //             return ref Test1(ref sp);    // error2
                Diagnostic(ErrorCode.ERR_RefReturnLocal, "sp").WithArguments("sp").WithLocation(29, 34),
                // (29,24): error CS8521: Cannot use a result of 'Program.Test1(ref Program.S1)' in this context because it may expose variables referenced by parameter 'arg' outside of their declaration scope
                //             return ref Test1(ref sp);    // error2
                Diagnostic(ErrorCode.ERR_EscapeCall, "Test1(ref sp)").WithArguments("Program.Test1(ref Program.S1)", "arg").WithLocation(29, 24)
159 160 161 162 163 164 165
            );
        }

        [Fact()]
        public void RefLikeReturnEscapeWithRefLikes1()
        {
            var text = @"
V
vsadov 已提交
166
    using System;
167 168 169 170 171 172
    class Program
    {
        static void Main()
        {
        }

V
vsadov 已提交
173
        static ref Span<int> Test1(ref S1 arg)
174
        {
V
vsadov 已提交
175
            throw null;
176 177
        }

V
vsadov 已提交
178
        static S1 MayWrap(Span<int> arg)
179 180 181 182 183 184
        {
            return default;
        }

        static void Test5()
        {
V
vsadov 已提交
185
            var sp = MayWrap(default);
186 187

            // returnable. 
V
vsadov 已提交
188
            var spR = MayWrap(Test1(ref sp));   
189

V
vsadov 已提交
190 191
            Span<int> local = stackalloc int[1];
            var sp1 = MayWrap(local);
192

V
vsadov 已提交
193 194
            // not returnable by value. (since it refers to a local data)
            var spNr = MayWrap(Test1(ref sp1));   
195 196 197 198

            // error
            spR = spNr;

199 200
            // ok, picks the narrowest val-escape
            var ternary =  true? spR: spNr;
201 202 203 204 205 206 207 208 209 210

            // error
            spR = ternary;
        }

        ref struct S1
        {
        }
    }
";
V
vsadov 已提交
211
            CreateCompilationWithMscorlibAndSpan(text).VerifyDiagnostics(
212
                // (33,19): error CS8526: Cannot use local 'spNr' in this context because it may expose referenced variables outside of their declaration scope
213
                //             spR = spNr;
214 215
                Diagnostic(ErrorCode.ERR_EscapeLocal, "spNr").WithArguments("spNr").WithLocation(33, 19),
                // (39,19): error CS8526: Cannot use local 'ternary' in this context because it may expose referenced variables outside of their declaration scope
216
                //             spR = ternary;
217
                Diagnostic(ErrorCode.ERR_EscapeLocal, "ternary").WithArguments("ternary").WithLocation(39, 19)
218 219 220
            );
        }
        
V
vsadov 已提交
221
        [Fact()]
222
        public void RefLikeReturnEscapeInParam()
V
vsadov 已提交
223 224
        {
            var text = @"
V
vsadov 已提交
225
    using System;
V
vsadov 已提交
226 227 228 229 230 231 232 233 234 235 236
    class Program
    {
        static void Main()
        {
        }

        static ref int Test1(S1 arg)
        {
            return ref (new int[1])[0];
        }

V
vsadov 已提交
237
        static S1 MayWrap(ref readonly Span<int> arg)
V
vsadov 已提交
238 239 240 241 242 243
        {
            return default;
        }

        static ref int Test3()
        {
V
vsadov 已提交
244
            Span<int> local = stackalloc int[1];
245
            var sp = MayWrap(local);
V
vsadov 已提交
246 247

            // not an error
248
            sp = MayWrap(local);
V
vsadov 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261

            // not an error
            sp = sp;

            // error here
            return ref Test1(sp);
        }

        ref struct S1
        {
        }
    }
";
V
vsadov 已提交
262 263
            CreateCompilationWithMscorlibAndSpan(text).VerifyDiagnostics(
                // (31,30): error CS8526: Cannot use local 'sp' in this context because it may expose referenced variables outside of their declaration scope
V
vsadov 已提交
264
                //             return ref Test1(sp);
V
vsadov 已提交
265 266
                Diagnostic(ErrorCode.ERR_EscapeLocal, "sp").WithArguments("sp").WithLocation(31, 30),
                // (31,24): error CS8521: Cannot use a result of 'Program.Test1(Program.S1)' in this context because it may expose variables referenced by parameter 'arg' outside of their declaration scope
267
                //             return ref Test1(sp);
V
vsadov 已提交
268 269
                Diagnostic(ErrorCode.ERR_EscapeCall, "Test1(sp)").WithArguments("Program.Test1(Program.S1)", "arg").WithLocation(31, 24),
                // (28,13): warning CS1717: Assignment made to same variable; did you mean to assign something else?
V
vsadov 已提交
270
                //             sp = sp;
V
vsadov 已提交
271
                Diagnostic(ErrorCode.WRN_AssignmentToSelf, "sp = sp").WithLocation(28, 13)
V
vsadov 已提交
272 273 274 275
            );
        }

        [Fact()]
276
        public void RefLikeReturnEscapeInParamOptional()
V
vsadov 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289
        {
            var text = @"
    class Program
    {
        static void Main()
        {
        }

        static ref int Test1(S1 arg)
        {
            return ref (new int[1])[0];
        }

V
vsadov 已提交
290
        static S1 MayNotWrap(ref readonly int arg = 123)
V
vsadov 已提交
291 292 293 294 295 296
        {
            return default;
        }

        static ref int Test3()
        {
V
vsadov 已提交
297 298
            // ok
            return ref Test1(MayNotWrap());
V
vsadov 已提交
299 300 301 302 303 304 305
        }

        ref struct S1
        {
        }
    }
";
V
vsadov 已提交
306
            CreateCompilationWithMscorlibAndSpan(text).VerifyDiagnostics();
V
vsadov 已提交
307 308 309
        }

        [Fact()]
310
        public void RefLikeScopeEscape()
V
vsadov 已提交
311 312
        {
            var text = @"
V
vsadov 已提交
313
    using System;
V
vsadov 已提交
314 315 316 317
    class Program
    {
        static void Main()
        {
V
vsadov 已提交
318
            Span<int> outer = default;
319

V
vsadov 已提交
320
            S1 x = MayWrap(outer);
321 322

            {
V
vsadov 已提交
323
                 Span<int> inner = stackalloc int[1];
324 325

                // valid
V
vsadov 已提交
326
                x = MayWrap(outer);
327 328
    
                // error
V
vsadov 已提交
329
                x = MayWrap(inner);
330 331 332
            }
        }

V
vsadov 已提交
333
        static S1 MayWrap(Span<int> arg)
334 335 336 337 338 339 340 341 342
        {
            return default;
        }

        ref struct S1
        {
        }
    }
";
V
vsadov 已提交
343 344 345 346 347 348 349
            CreateCompilationWithMscorlibAndSpan(text).VerifyDiagnostics(
                // (18,29): error CS8526: Cannot use local 'inner' in this context because it may expose referenced variables outside of their declaration scope
                //                 x = MayWrap(inner);
                Diagnostic(ErrorCode.ERR_EscapeLocal, "inner").WithArguments("inner").WithLocation(18, 29),
                // (18,21): error CS8521: Cannot use a result of 'Program.MayWrap(Span<int>)' in this context because it may expose variables referenced by parameter 'arg' outside of their declaration scope
                //                 x = MayWrap(inner);
                Diagnostic(ErrorCode.ERR_EscapeCall, "MayWrap(inner)").WithArguments("Program.MayWrap(System.Span<int>)", "arg").WithLocation(18, 21)
350 351 352 353 354 355 356
            );
        }

        [Fact()]
        public void RefLikeScopeEscapeReturnable()
        {
            var text = @"
V
vsadov 已提交
357
    using System;
358 359 360 361
    class Program
    {
        static void Main()
        {
V
vsadov 已提交
362
            Span<int> outer = default;
363 364

            // make x returnable
V
vsadov 已提交
365 366
            S1 x = default;

367
            {
V
vsadov 已提交
368
                Span<int> inner = stackalloc int[0];
369 370 371 372 373 374 375 376 377

                // valid
                x = MayWrap(ref outer);
    
                // error
                x = MayWrap(ref inner);
            }
        }

V
vsadov 已提交
378
        static S1 MayWrap(ref Span<int> arg)
379 380 381 382 383 384 385 386 387
        {
            return default;
        }

        ref struct S1
        {
        }
    }
";
V
vsadov 已提交
388 389
            CreateCompilationWithMscorlibAndSpan(text).VerifyDiagnostics(
                // (19,33): error CS8526: Cannot use local 'inner' in this context because it may expose referenced variables outside of their declaration scope
390
                //                 x = MayWrap(ref inner);
V
vsadov 已提交
391 392
                Diagnostic(ErrorCode.ERR_EscapeLocal, "inner").WithArguments("inner").WithLocation(19, 33),
                // (19,21): error CS8521: Cannot use a result of 'Program.MayWrap(ref Span<int>)' in this context because it may expose variables referenced by parameter 'arg' outside of their declaration scope
393
                //                 x = MayWrap(ref inner);
V
vsadov 已提交
394
                Diagnostic(ErrorCode.ERR_EscapeCall, "MayWrap(ref inner)").WithArguments("Program.MayWrap(ref System.Span<int>)", "arg").WithLocation(19, 21)
395 396 397 398 399 400 401
            );
        }

        [Fact()]
        public void RefLikeScopeEscapeThis()
        {
            var text = @"
V
vsadov 已提交
402
    using System;
403 404 405 406
    class Program
    {
        static void Main()
        {
V
vsadov 已提交
407
            Span<int> outer = default;
V
vsadov 已提交
408

409 410
            S1 x = MayWrap(ref outer);

V
vsadov 已提交
411
            {
V
vsadov 已提交
412
                Span<int> inner = stackalloc int[1];
V
vsadov 已提交
413 414

                // valid
415 416 417 418
                x = S1.NotSlice(1);

                // valid
                x = MayWrap(ref outer).Slice(1);
V
vsadov 已提交
419 420
    
                // error
421
                x = MayWrap(ref inner).Slice(1);
V
vsadov 已提交
422 423 424
            }
        }

V
vsadov 已提交
425
        static S1 MayWrap(ref Span<int> arg)
V
vsadov 已提交
426 427 428 429 430 431
        {
            return default;
        }

        ref struct S1
        {
432 433 434
            public static S1 NotSlice(int x) => default;

            public S1 Slice(int x) => this;
V
vsadov 已提交
435 436 437
        }
    }
";
V
vsadov 已提交
438 439
            CreateCompilationWithMscorlibAndSpan(text).VerifyDiagnostics(
                // (21,33): error CS8526: Cannot use local 'inner' in this context because it may expose referenced variables outside of their declaration scope
440
                //                 x = MayWrap(ref inner).Slice(1);
V
vsadov 已提交
441 442
                Diagnostic(ErrorCode.ERR_EscapeLocal, "inner").WithArguments("inner").WithLocation(21, 33),
                // (21,21): error CS8521: Cannot use a result of 'Program.MayWrap(ref Span<int>)' in this context because it may expose variables referenced by parameter 'arg' outside of their declaration scope
443
                //                 x = MayWrap(ref inner).Slice(1);
V
vsadov 已提交
444
                Diagnostic(ErrorCode.ERR_EscapeCall, "MayWrap(ref inner)").WithArguments("Program.MayWrap(ref System.Span<int>)", "arg").WithLocation(21, 21)
445 446 447 448 449 450 451
            );
        }

        [Fact()]
        public void RefLikeScopeEscapeThisRef()
        {
            var text = @"
V
vsadov 已提交
452
using System;
453 454 455 456
class Program
{
    static void Main()
    {
V
vsadov 已提交
457
        Span<int> outer = default;
458 459 460 461

        ref S1 x = ref MayWrap(ref outer)[0];

        {
V
vsadov 已提交
462
            Span<int> inner = stackalloc int[1];
V
vsadov 已提交
463

464 465 466
            // valid
            x[0] = MayWrap(ref outer).Slice(1)[0];

467 468 469
            // error, technically rules for this case can be relaxed, 
            // but ref-like typed ref-returning properties are nearly impossible to implement in a useful way
            //
470 471
            x[0] = MayWrap(ref inner).Slice(1)[0];

472 473 474
            // error, technically rules for this case can be relaxed, 
            // but ref-like typed ref-returning properties are nearly impossible to implement in a useful way
            //
475 476 477 478 479 480 481
            x[x] = MayWrap(ref inner).Slice(1)[0];

            // error
            x.ReturnsRefArg(ref x) = MayWrap(ref inner).Slice(1)[0];
        }
    }

V
vsadov 已提交
482
    static S1 MayWrap(ref Span<int> arg)
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
    {
        return default;
    }

    ref struct S1
    {
        public ref S1 this[int i] => throw null;

        public ref S1 this[S1 i] => throw null;

        public ref S1 ReturnsRefArg(ref S1 arg) => ref arg;

        public S1 Slice(int x) => this;
    }
}
";
V
vsadov 已提交
499 500
            CreateCompilationWithMscorlibAndSpan(text).VerifyDiagnostics(
                // (20,32): error CS8526: Cannot use local 'inner' in this context because it may expose referenced variables outside of their declaration scope
501
                //             x[0] = MayWrap(ref inner).Slice(1)[0];
V
vsadov 已提交
502 503
                Diagnostic(ErrorCode.ERR_EscapeLocal, "inner").WithArguments("inner").WithLocation(20, 32),
                // (20,20): error CS8521: Cannot use a result of 'Program.MayWrap(ref Span<int>)' in this context because it may expose variables referenced by parameter 'arg' outside of their declaration scope
504
                //             x[0] = MayWrap(ref inner).Slice(1)[0];
V
vsadov 已提交
505 506
                Diagnostic(ErrorCode.ERR_EscapeCall, "MayWrap(ref inner)").WithArguments("Program.MayWrap(ref System.Span<int>)", "arg").WithLocation(20, 20),
                // (25,32): error CS8526: Cannot use local 'inner' in this context because it may expose referenced variables outside of their declaration scope
507
                //             x[x] = MayWrap(ref inner).Slice(1)[0];
V
vsadov 已提交
508 509
                Diagnostic(ErrorCode.ERR_EscapeLocal, "inner").WithArguments("inner").WithLocation(25, 32),
                // (25,20): error CS8521: Cannot use a result of 'Program.MayWrap(ref Span<int>)' in this context because it may expose variables referenced by parameter 'arg' outside of their declaration scope
510
                //             x[x] = MayWrap(ref inner).Slice(1)[0];
V
vsadov 已提交
511 512
                Diagnostic(ErrorCode.ERR_EscapeCall, "MayWrap(ref inner)").WithArguments("Program.MayWrap(ref System.Span<int>)", "arg").WithLocation(25, 20),
                // (28,50): error CS8526: Cannot use local 'inner' in this context because it may expose referenced variables outside of their declaration scope
513
                //             x.ReturnsRefArg(ref x) = MayWrap(ref inner).Slice(1)[0];
V
vsadov 已提交
514 515
                Diagnostic(ErrorCode.ERR_EscapeLocal, "inner").WithArguments("inner").WithLocation(28, 50),
                // (28,38): error CS8521: Cannot use a result of 'Program.MayWrap(ref Span<int>)' in this context because it may expose variables referenced by parameter 'arg' outside of their declaration scope
516
                //             x.ReturnsRefArg(ref x) = MayWrap(ref inner).Slice(1)[0];
V
vsadov 已提交
517 518
                Diagnostic(ErrorCode.ERR_EscapeCall, "MayWrap(ref inner)").WithArguments("Program.MayWrap(ref System.Span<int>)", "arg").WithLocation(28, 38),
                // (43,56): error CS8166: Cannot return a parameter by reference 'arg' because it is not a ref or out parameter
519
                //         public ref S1 ReturnsRefArg(ref S1 arg) => ref arg;
V
vsadov 已提交
520
                Diagnostic(ErrorCode.ERR_RefReturnParameter, "arg").WithArguments("arg").WithLocation(43, 56)
521 522 523 524 525 526 527
            );
        }

        [Fact()]
        public void RefLikeScopeEscapeField()
        {
            var text = @"
V
vsadov 已提交
528
using System;
529 530 531 532
class Program
{
    static void Main()
    {
V
vsadov 已提交
533
        Span<int> outer = default;
534

V
vsadov 已提交
535
        S1 x = MayWrap(outer);
536 537

        {
V
vsadov 已提交
538
            Span<int> inner = stackalloc int[1];
539 540

            // valid
V
vsadov 已提交
541
            x.field = MayWrap(outer).Slice(1).field;
542 543

            // error
V
vsadov 已提交
544
            x.field = MayWrap(inner).Slice(1).field;
545 546 547
        }
    }

V
vsadov 已提交
548
    static S1 MayWrap(Span<int> arg)
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
    {
        return default;
    }

    ref struct S1
    {
        public S0 field;

        public S1 Slice(int x) => this;
    }

    ref struct S0
    {
    }
}
";
V
vsadov 已提交
565 566 567 568 569 570 571
            CreateCompilationWithMscorlibAndSpan(text).VerifyDiagnostics(
                // (18,31): error CS8526: Cannot use local 'inner' in this context because it may expose referenced variables outside of their declaration scope
                //             x.field = MayWrap(inner).Slice(1).field;
                Diagnostic(ErrorCode.ERR_EscapeLocal, "inner").WithArguments("inner").WithLocation(18, 31),
                // (18,23): error CS8521: Cannot use a result of 'Program.MayWrap(Span<int>)' in this context because it may expose variables referenced by parameter 'arg' outside of their declaration scope
                //             x.field = MayWrap(inner).Slice(1).field;
                Diagnostic(ErrorCode.ERR_EscapeCall, "MayWrap(inner)").WithArguments("Program.MayWrap(System.Span<int>)", "arg").WithLocation(18, 23)
V
vsadov 已提交
572 573
            );
        }
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608

        [Fact()]
        public void RefLikeEscapeParamsAndTopLevel()
        {
            var text = @"
    class Program
    {
        static void Main()
        {
        }

        void Test1(int x)
        {
            int y = 1;

            var rx = MayWrap(ref x);
            var ry = MayWrap(ref y);

            // valid. parameter scope and the top local scope are the same.
            rx = ry;
            
            bool condition = true;
            rx = condition ? rx: ry;
        }

        static S1 MayWrap(ref int arg)
        {
            return default;
        }

        ref struct S1
        {
        }
    }
";
V
vsadov 已提交
609
            CreateCompilationWithMscorlibAndSpan(text).VerifyDiagnostics(
V
vsadov 已提交
610 611 612 613 614 615 616 617
                // no diagnostics expected
            );
        }

        [Fact()]
        public void RefLikeEscapeMixingCallSameArgValue()
        {
            var text = @"
V
vsadov 已提交
618 619
    using System;
    public class Program
V
vsadov 已提交
620 621 622 623 624 625 626 627 628
    {
        static void Main()
        {
        }

        void Test1()
        {
            S1 rOuter = default;

V
vsadov 已提交
629 630
            Span<int> inner = stackalloc int[1];
            S1 rInner = MayWrap(inner);
V
vsadov 已提交
631 632 633 634 635 636 637 638 639 640

            // valid
            MayAssign(ref rOuter);

            // valid
            MayAssign(ref rInner);
        }

        static void MayAssign(ref S1 arg1)
        {
V
vsadov 已提交
641 642
            // valid
            arg1 = MayWrap(arg1.field);
V
vsadov 已提交
643 644
        }

V
vsadov 已提交
645
        static S1 MayWrap(Span<int> arg)
V
vsadov 已提交
646 647 648 649
        {
            return default;
        }

V
vsadov 已提交
650
        public ref struct S1
V
vsadov 已提交
651
        {
V
vsadov 已提交
652
            public Span<int> field;
V
vsadov 已提交
653 654 655
        }
    }
";
V
vsadov 已提交
656
            CreateCompilationWithMscorlibAndSpan(text).VerifyDiagnostics();
V
vsadov 已提交
657 658 659 660 661 662
        }

        [Fact()]
        public void RefLikeEscapeMixingCall()
        {
            var text = @"
V
vsadov 已提交
663
    using System;
V
vsadov 已提交
664 665 666 667 668 669 670 671 672 673
    class Program
    {
        static void Main()
        {
        }

        void Test1()
        {
            S1 rOuter = default;

V
vsadov 已提交
674
            Span<int> inner = stackalloc int[1];
V
vsadov 已提交
675 676 677 678 679 680 681 682 683 684 685 686
            S1 rInner = MayWrap(ref inner);

            // valid
            MayAssign(ref rOuter, ref rOuter);

            // error
            MayAssign(ref rOuter, ref rInner);

            // error
            MayAssign(ref inner, ref rOuter);
        }

V
vsadov 已提交
687
        static void MayAssign(ref Span<int> arg1, ref S1 arg2)
V
vsadov 已提交
688 689 690 691 692 693 694 695 696
        {
            arg2 = MayWrap(ref arg1);
        }

        static void MayAssign(ref S1 arg1, ref S1 arg2)
        {
            arg1 = arg2;
        }

V
vsadov 已提交
697
        static S1 MayWrap(ref Span<int> arg)
V
vsadov 已提交
698 699 700 701 702 703 704 705 706
        {
            return default;
        }

        ref struct S1
        {
        }
    }
";
V
vsadov 已提交
707 708
            CreateCompilationWithMscorlibAndSpan(text).VerifyDiagnostics(
                // (20,39): error CS8526: Cannot use local 'rInner' in this context because it may expose referenced variables outside of their declaration scope
V
vsadov 已提交
709
                //             MayAssign(ref rOuter, ref rInner);
V
vsadov 已提交
710 711
                Diagnostic(ErrorCode.ERR_EscapeLocal, "rInner").WithArguments("rInner").WithLocation(20, 39),
                // (20,13): error CS8524: This combination of arguments to 'Program.MayAssign(ref Program.S1, ref Program.S1)' is disallowed because it may expose variables referenced by parameter 'arg2' outside of their declaration scope
V
vsadov 已提交
712
                //             MayAssign(ref rOuter, ref rInner);
V
vsadov 已提交
713 714
                Diagnostic(ErrorCode.ERR_CallArgMixing, "MayAssign(ref rOuter, ref rInner)").WithArguments("Program.MayAssign(ref Program.S1, ref Program.S1)", "arg2").WithLocation(20, 13),
                // (23,27): error CS8526: Cannot use local 'inner' in this context because it may expose referenced variables outside of their declaration scope
V
vsadov 已提交
715
                //             MayAssign(ref inner, ref rOuter);
V
vsadov 已提交
716 717
                Diagnostic(ErrorCode.ERR_EscapeLocal, "inner").WithArguments("inner").WithLocation(23, 27),
                // (23,13): error CS8524: This combination of arguments to 'Program.MayAssign(ref Span<int>, ref Program.S1)' is disallowed because it may expose variables referenced by parameter 'arg1' outside of their declaration scope
V
vsadov 已提交
718
                //             MayAssign(ref inner, ref rOuter);
V
vsadov 已提交
719
                Diagnostic(ErrorCode.ERR_CallArgMixing, "MayAssign(ref inner, ref rOuter)").WithArguments("Program.MayAssign(ref System.Span<int>, ref Program.S1)", "arg1").WithLocation(23, 13)
V
vsadov 已提交
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
            );
        }

        [Fact()]
        public void RefLikeEscapeMixingIndex()
        {
            var text = @"
class Program
{
    static void Main()
    {
    }

    void Test1()
    {
        S1 rOuter = default;

        int inner = 1;
        S1 rInner = MayWrap(ref inner);

        // valid
        int dummy1 = this[rOuter, rOuter];

        // error
        int dummy2 = this[rOuter, rInner];

        // error
        int dummy3 = this[inner, rOuter];
    }

750
    int this[ref readonly int arg1, ref readonly S1 arg2]
V
vsadov 已提交
751 752 753 754 755 756 757 758 759
    {
        get
        {
            // not possible
            // arg2 = MayWrap(ref arg1);
            return 0;
        }
    }

760
    int this[ref readonly S1 arg1, ref readonly S1 arg2]
V
vsadov 已提交
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
    {
        get
        {
            // not possible
            // arg1 = arg2;
            return 0;
        }
    }

    static S1 MayWrap(ref int arg)
    {
        return default;
    }

    ref struct S1
    {
    }
}
";
V
vsadov 已提交
780
            CreateCompilationWithMscorlibAndSpan(text).VerifyDiagnostics(
V
vsadov 已提交
781 782 783 784 785 786 787 788
                // no diagnostics
            );
        }

        [Fact()]
        public void RefLikeEscapeMixingIndexOnRefLike()
        {
            var text = @"
V
vsadov 已提交
789
using System;
V
vsadov 已提交
790 791 792 793 794 795 796 797 798
class Program
{
    static void Main()
    {
    }

    void Test1()
    {
        S1 rOuter = default;
V
vsadov 已提交
799
        rOuter.field = default;
V
vsadov 已提交
800

V
vsadov 已提交
801
        Span<int> inner = stackalloc int[1];
V
vsadov 已提交
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
        S1 rInner = MayWrap(inner);

        // valid
        int dummy1 = rOuter[rOuter];

        // valid
        int dummy2 = rInner[rOuter];

        // error
        int dummy3 = rOuter[rInner];

        // error
        int dummy4 = rOuter[inner];
    }

V
vsadov 已提交
817
    static S1 MayWrap(ref readonly Span<int> arg)
V
vsadov 已提交
818 819 820 821 822 823
    {
        return default;
    }

    ref struct S1
    {
V
vsadov 已提交
824
        public Span<int> field;
V
vsadov 已提交
825

V
vsadov 已提交
826
        public int this[ref readonly Span<int> arg1]
V
vsadov 已提交
827 828 829
        {
            get
            {
830
                // should be an error, arg1 is not ref-returnable, 'this' is val-returnable
V
vsadov 已提交
831 832 833 834 835
                this = MayWrap(arg1);
                return 0;
            }
        }

836
        public int this[ref readonly S1 arg1]
V
vsadov 已提交
837 838 839
        {
            get
            {
V
vsadov 已提交
840
                // ok
V
vsadov 已提交
841
                this = MayWrap(arg1.field);
842 843

                // this is actually OK and thus the errors in corresponding Test1 scenarios.
V
vsadov 已提交
844
                this = arg1;
845

V
vsadov 已提交
846 847 848 849 850 851
                return 0;
            }
        }
    }
}
";
V
vsadov 已提交
852 853
            CreateCompilationWithMscorlibAndSpan(text).VerifyDiagnostics(
                // (24,29): error CS8526: Cannot use local 'rInner' in this context because it may expose referenced variables outside of their declaration scope
V
vsadov 已提交
854
                //         int dummy3 = rOuter[rInner];
V
vsadov 已提交
855
                Diagnostic(ErrorCode.ERR_EscapeLocal, "rInner").WithArguments("rInner").WithLocation(24, 29),
V
vsadov 已提交
856
                // (24,22): error CS8524: This combination of arguments to 'Program.S1.this[ref readonly Program.S1]' is disallowed because it may expose variables referenced by parameter 'arg1' outside of their declaration scope
V
vsadov 已提交
857
                //         int dummy3 = rOuter[rInner];
V
vsadov 已提交
858
                Diagnostic(ErrorCode.ERR_CallArgMixing, "rOuter[rInner]").WithArguments("Program.S1.this[ref readonly Program.S1]", "arg1").WithLocation(24, 22),
V
vsadov 已提交
859
                // (27,29): error CS8526: Cannot use local 'inner' in this context because it may expose referenced variables outside of their declaration scope
V
vsadov 已提交
860
                //         int dummy4 = rOuter[inner];
V
vsadov 已提交
861
                Diagnostic(ErrorCode.ERR_EscapeLocal, "inner").WithArguments("inner").WithLocation(27, 29),
V
vsadov 已提交
862
                // (27,22): error CS8524: This combination of arguments to 'Program.S1.this[ref readonly Span<int>]' is disallowed because it may expose variables referenced by parameter 'arg1' outside of their declaration scope
V
vsadov 已提交
863
                //         int dummy4 = rOuter[inner];
V
vsadov 已提交
864
                Diagnostic(ErrorCode.ERR_CallArgMixing, "rOuter[inner]").WithArguments("Program.S1.this[ref readonly System.Span<int>]", "arg1").WithLocation(27, 22)
V
vsadov 已提交
865 866 867 868 869 870 871
            );
        }

        [Fact()]
        public void RefLikeEscapeMixingCtor()
        {
            var text = @"
V
vsadov 已提交
872
    using System;
V
vsadov 已提交
873 874 875 876 877 878 879
    class Program
    {
        static void Main()
        {
        }

        delegate void D1(ref S1 arg1, ref S1 arg2);
V
vsadov 已提交
880
        delegate void D2(ref Span<int> arg1, ref S1 arg2);
V
vsadov 已提交
881 882 883 884 885

        void Test1()
        {
            S1 rOuter = default;

V
vsadov 已提交
886
            Span<int> inner = stackalloc int[1];
V
vsadov 已提交
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
            S1 rInner = MayWrap(ref inner);

            D1 MayAssignDel1 = MayAssign;
            D2 MayAssignDel2 = MayAssign;

            // valid
            MayAssignDel1(ref rOuter, ref rOuter);

            // error
            MayAssignDel1(ref rOuter, ref rInner);

            // error
            MayAssignDel2(ref inner, ref rOuter);
        }

V
vsadov 已提交
902
        static void MayAssign(ref Span<int> arg1, ref S1 arg2)
V
vsadov 已提交
903 904 905 906 907 908 909 910 911
        {
            arg2 = MayWrap(ref arg1);
        }

        static void MayAssign(ref S1 arg1, ref S1 arg2)
        {
            arg1 = arg2;
        }

V
vsadov 已提交
912
        static S1 MayWrap(ref Span<int> arg)
V
vsadov 已提交
913 914 915 916 917 918 919 920 921
        {
            return default;
        }

        ref struct S1
        {
        }
    }
";
V
vsadov 已提交
922 923
            CreateCompilationWithMscorlibAndSpan(text).VerifyDiagnostics(
                // (26,43): error CS8526: Cannot use local 'rInner' in this context because it may expose referenced variables outside of their declaration scope
V
vsadov 已提交
924
                //             MayAssignDel1(ref rOuter, ref rInner);
V
vsadov 已提交
925 926
                Diagnostic(ErrorCode.ERR_EscapeLocal, "rInner").WithArguments("rInner").WithLocation(26, 43),
                // (26,13): error CS8524: This combination of arguments to 'Program.D1.Invoke(ref Program.S1, ref Program.S1)' is disallowed because it may expose variables referenced by parameter 'arg2' outside of their declaration scope
V
vsadov 已提交
927
                //             MayAssignDel1(ref rOuter, ref rInner);
V
vsadov 已提交
928 929
                Diagnostic(ErrorCode.ERR_CallArgMixing, "MayAssignDel1(ref rOuter, ref rInner)").WithArguments("Program.D1.Invoke(ref Program.S1, ref Program.S1)", "arg2").WithLocation(26, 13),
                // (29,31): error CS8526: Cannot use local 'inner' in this context because it may expose referenced variables outside of their declaration scope
V
vsadov 已提交
930
                //             MayAssignDel2(ref inner, ref rOuter);
V
vsadov 已提交
931 932
                Diagnostic(ErrorCode.ERR_EscapeLocal, "inner").WithArguments("inner").WithLocation(29, 31),
                // (29,13): error CS8524: This combination of arguments to 'Program.D2.Invoke(ref Span<int>, ref Program.S1)' is disallowed because it may expose variables referenced by parameter 'arg1' outside of their declaration scope
V
vsadov 已提交
933
                //             MayAssignDel2(ref inner, ref rOuter);
V
vsadov 已提交
934
                Diagnostic(ErrorCode.ERR_CallArgMixing, "MayAssignDel2(ref inner, ref rOuter)").WithArguments("Program.D2.Invoke(ref System.Span<int>, ref Program.S1)", "arg1").WithLocation(29, 13)
V
vsadov 已提交
935 936 937 938 939 940 941
            );
        }

        [Fact()]
        public void RefLikeEscapeMixingDelegate()
        {
            var text = @"
V
vsadov 已提交
942
    using System;
V
vsadov 已提交
943 944 945 946 947 948 949 950 951 952
    class Program
    {
        static void Main()
        {
        }

        void Test1()
        {
            S1 rOuter = default;

V
vsadov 已提交
953
            Span<int> inner = stackalloc int[2];
V
vsadov 已提交
954 955 956 957 958 959 960 961 962 963 964 965
            S1 rInner = MayWrap(ref inner);

            // valid
            var dummy1 = new Program(ref rOuter, ref rOuter);

            // error
            var dummy2 = new Program(ref rOuter, ref rInner);

            // error
            var dummy3 = new Program(ref inner, ref rOuter);
        }

V
vsadov 已提交
966
        Program(ref Span<int> arg1, ref S1 arg2)
V
vsadov 已提交
967 968 969 970 971 972 973 974 975
        {
            arg2 = MayWrap(ref arg1);
        }

        Program(ref S1 arg1, ref S1 arg2)
        {
            arg1 = arg2;
        }

V
vsadov 已提交
976
        static S1 MayWrap(ref Span<int> arg)
V
vsadov 已提交
977 978 979 980 981 982 983 984 985
        {
            return default;
        }

        ref struct S1
        {
        }
    }
";
V
vsadov 已提交
986 987
            CreateCompilationWithMscorlibAndSpan(text).VerifyDiagnostics(
                // (20,54): error CS8526: Cannot use local 'rInner' in this context because it may expose referenced variables outside of their declaration scope
V
vsadov 已提交
988
                //             var dummy2 = new Program(ref rOuter, ref rInner);
V
vsadov 已提交
989 990
                Diagnostic(ErrorCode.ERR_EscapeLocal, "rInner").WithArguments("rInner").WithLocation(20, 54),
                // (20,26): error CS8524: This combination of arguments to 'Program.Program(ref Program.S1, ref Program.S1)' is disallowed because it may expose variables referenced by parameter 'arg2' outside of their declaration scope
V
vsadov 已提交
991
                //             var dummy2 = new Program(ref rOuter, ref rInner);
V
vsadov 已提交
992 993
                Diagnostic(ErrorCode.ERR_CallArgMixing, "new Program(ref rOuter, ref rInner)").WithArguments("Program.Program(ref Program.S1, ref Program.S1)", "arg2").WithLocation(20, 26),
                // (23,42): error CS8526: Cannot use local 'inner' in this context because it may expose referenced variables outside of their declaration scope
V
vsadov 已提交
994
                //             var dummy3 = new Program(ref inner, ref rOuter);
V
vsadov 已提交
995 996
                Diagnostic(ErrorCode.ERR_EscapeLocal, "inner").WithArguments("inner").WithLocation(23, 42),
                // (23,26): error CS8524: This combination of arguments to 'Program.Program(ref Span<int>, ref Program.S1)' is disallowed because it may expose variables referenced by parameter 'arg1' outside of their declaration scope
V
vsadov 已提交
997
                //             var dummy3 = new Program(ref inner, ref rOuter);
V
vsadov 已提交
998
                Diagnostic(ErrorCode.ERR_CallArgMixing, "new Program(ref inner, ref rOuter)").WithArguments("Program.Program(ref System.Span<int>, ref Program.S1)", "arg1").WithLocation(23, 26)
V
vsadov 已提交
999 1000 1001 1002 1003 1004 1005
            );
        }

        [Fact()]
        public void RefLikeEscapeMixingCallOptionalIn()
        {
            var text = @"
V
vsadov 已提交
1006
    using System;
V
vsadov 已提交
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
    class Program
    {
        static void Main()
        {
        }

        void Test1()
        {
            S1 rOuter = default;

V
vsadov 已提交
1017
            Span<int> inner = stackalloc int[1];
V
vsadov 已提交
1018 1019
            S1 rInner = MayWrap(inner);

V
vsadov 已提交
1020
            // valid, optional arg is of the same escape level
V
vsadov 已提交
1021 1022
            MayAssign(ref rOuter);

V
vsadov 已提交
1023
            // valid, optional arg is of wider escape level
V
vsadov 已提交
1024 1025 1026
            MayAssign(ref rInner);
        }

V
vsadov 已提交
1027
        static void MayAssign(ref S1 arg1, ref readonly Span<int> arg2 = default)
V
vsadov 已提交
1028 1029 1030 1031
        {
            arg1 = MayWrap(arg2);
        }

V
vsadov 已提交
1032
        static S1 MayWrap(ref readonly Span<int> arg)
V
vsadov 已提交
1033 1034 1035 1036 1037 1038 1039 1040 1041
        {
            return default;
        }

        ref struct S1
        {
        }
    }
";
V
vsadov 已提交
1042
            CreateCompilationWithMscorlibAndSpan(text).VerifyDiagnostics();
1043
        }
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056

        [Fact()]
        public void MismatchedRefTernaryEscape()
        {
            var text = @"
class Program
{
    static void Main()
    {
    }

    public static int field = 1;

1057 1058 1059
    bool flag = true;

    ref int Test1()
1060 1061
    {
        var local = 42;
1062 1063 1064

        if (flag) 
            return ref true ? ref field : ref local;
1065 1066 1067 1068

        ref var lr = ref local;
        ref var fr = ref field;

1069 1070 1071 1072
        ref var ternary1 = ref true ? ref lr : ref fr;

        if (flag) 
            return ref ternary1;
1073 1074 1075
    }
}
";
V
vsadov 已提交
1076
            CreateCompilationWithMscorlibAndSpan(text).VerifyDiagnostics(
1077 1078 1079 1080 1081 1082 1083 1084 1085
                // (17,47): error CS8168: Cannot return local 'local' by reference because it is not a ref local
                //             return ref true ? ref field : ref local;
                Diagnostic(ErrorCode.ERR_RefReturnLocal, "local").WithArguments("local").WithLocation(17, 47),
                // (25,24): error CS8157: Cannot return 'ternary1' by reference because it was initialized to a value that cannot be returned by reference
                //             return ref ternary1;
                Diagnostic(ErrorCode.ERR_RefReturnNonreturnableLocal, "ternary1").WithArguments("ternary1").WithLocation(25, 24),
                // (12,13): error CS0161: 'Program.Test1()': not all code paths return a value
                //     ref int Test1()
                Diagnostic(ErrorCode.ERR_ReturnExpected, "Test1").WithArguments("Program.Test1()").WithLocation(12, 13)
V
vsadov 已提交
1086
                );
1087
        }
V
vsadov 已提交
1088

1089 1090 1091 1092
        [Fact()]
        public void MismatchedRefTernaryEscapeBlock()
        {
            var text = @"
V
vsadov 已提交
1093
using System;
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
class Program
{
    static void Main()
    {
    }

    public static int field = 1;

    void Test1()
    {
V
vsadov 已提交
1104
        Span<int> outer = default;
1105 1106 1107
        var sOuter = MayWrap(ref outer);

        {
V
vsadov 已提交
1108
            Span<int> inner = stackalloc int[1];
1109 1110
            var sInner = MayWrap(ref inner);

1111 1112 1113
            ref var ternarySame1 = ref true ? ref sInner : ref sInner;
            ref var ternarySame2 = ref true ? ref sOuter : ref sOuter;

V
vsadov 已提交
1114 1115 1116 1117 1118 1119 1120 1121 1122
            // ok
            ternarySame2 = true ? sOuter : sOuter;

            // error
            ternarySame2 = true ? sOuter : sInner;

            // error
            ternarySame2 = true ? sInner : sOuter;

1123 1124 1125 1126 1127 1128 1129 1130
            // error, mixing val escapes
            ref var ternary1 = ref true ? ref sOuter : ref sInner;

            // error, mixing val escapes
            ref var ternary2 = ref true ? ref sInner : ref sOuter;

            // error, mixing val escapes
            ref var ternary3 = ref true ? ref ternarySame1 : ref ternarySame2;
1131 1132 1133 1134

            ref var ir = ref sInner[1];
            ref var or = ref sOuter[1];

1135 1136
            // no error, indexer cannot ref-return the instance, so ir and or are both safe to return
            ref var ternary4 = ref true ? ref ir : ref or;
1137 1138 1139
        }
    }

V
vsadov 已提交
1140
    static S1 MayWrap(ref Span<int> arg)
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
    {
        return default;
    }

    ref struct S1
    {
        public ref int this[int i] => throw null;
    }
}
";
V
vsadov 已提交
1151
            CreateCompilationWithMscorlibAndSpan(text).VerifyDiagnostics(
V
vsadov 已提交
1152 1153 1154 1155 1156 1157 1158
                // (27,44): error CS8526: Cannot use local 'sInner' in this context because it may expose referenced variables outside of their declaration scope
                //             ternarySame2 = true ? sOuter : sInner;
                Diagnostic(ErrorCode.ERR_EscapeLocal, "sInner").WithArguments("sInner").WithLocation(27, 44),
                // (30,35): error CS8526: Cannot use local 'sInner' in this context because it may expose referenced variables outside of their declaration scope
                //             ternarySame2 = true ? sInner : sOuter;
                Diagnostic(ErrorCode.ERR_EscapeLocal, "sInner").WithArguments("sInner").WithLocation(30, 35),
                // (33,60): error CS8526: Cannot use local 'sInner' in this context because it may expose referenced variables outside of their declaration scope
1159
                //             ref var ternary1 = ref true ? ref sOuter : ref sInner;
V
vsadov 已提交
1160 1161
                Diagnostic(ErrorCode.ERR_EscapeLocal, "sInner").WithArguments("sInner").WithLocation(33, 60),
                // (33,36): error CS8525: Branches of a ref ternary operator cannot refer to variables with incompatible declaration scopes
1162
                //             ref var ternary1 = ref true ? ref sOuter : ref sInner;
V
vsadov 已提交
1163 1164
                Diagnostic(ErrorCode.ERR_MismatchedRefEscapeInTernary, "true ? ref sOuter : ref sInner").WithLocation(33, 36),
                // (36,47): error CS8526: Cannot use local 'sInner' in this context because it may expose referenced variables outside of their declaration scope
1165
                //             ref var ternary2 = ref true ? ref sInner : ref sOuter;
V
vsadov 已提交
1166 1167
                Diagnostic(ErrorCode.ERR_EscapeLocal, "sInner").WithArguments("sInner").WithLocation(36, 47),
                // (36,36): error CS8525: Branches of a ref ternary operator cannot refer to variables with incompatible declaration scopes
1168
                //             ref var ternary2 = ref true ? ref sInner : ref sOuter;
V
vsadov 已提交
1169 1170
                Diagnostic(ErrorCode.ERR_MismatchedRefEscapeInTernary, "true ? ref sInner : ref sOuter").WithLocation(36, 36),
                // (39,47): error CS8526: Cannot use local 'ternarySame1' in this context because it may expose referenced variables outside of their declaration scope
1171
                //             ref var ternary3 = ref true ? ref ternarySame1 : ref ternarySame2;
V
vsadov 已提交
1172 1173
                Diagnostic(ErrorCode.ERR_EscapeLocal, "ternarySame1").WithArguments("ternarySame1").WithLocation(39, 47),
                // (39,36): error CS8525: Branches of a ref ternary operator cannot refer to variables with incompatible declaration scopes
1174
                //             ref var ternary3 = ref true ? ref ternarySame1 : ref ternarySame2;
V
vsadov 已提交
1175
                Diagnostic(ErrorCode.ERR_MismatchedRefEscapeInTernary, "true ? ref ternarySame1 : ref ternarySame2").WithLocation(39, 36)
1176 1177
            );
        }
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217

        [Fact()]
        public void StackallocEscape()
        {
            var text = @"
    using System;
    class Program
    {
        static void Main()
        {

        }

        Span<int> Test0()
        {
            // valid, baseline
            return default(Span<int>);
        }

        Span<int> Test3()
        {
            Span<int> local = stackalloc int[10];
            return true? local : default(Span<int>);
        }
      
        Span<int> Test4(Span<int> arg)
        {
            arg = stackalloc int[10];
            return arg;
        }

        Span<int> Test6()
        {
            Span<int> local = default;
            local = stackalloc int[10];
            return local;
        }
    }
";
            CreateCompilationWithMscorlibAndSpan(text).VerifyDiagnostics(
1218
                // (19,26): error CS8352: Cannot use local 'local' in this context because it may expose referenced variables outside of their declaration scope 
1219 1220
                //             return true? local : default(Span<int>);
                Diagnostic(ErrorCode.ERR_EscapeLocal, "local").WithArguments("local").WithLocation(19, 26),
1221
                // (24,19): error CS8353: A result of a stackalloc expression of type 'Span<int>' cannot be used in this context because it may be exposed outside of the containing method
1222 1223
                //             arg = stackalloc int[10];
                Diagnostic(ErrorCode.ERR_EscapeStackAlloc, "stackalloc int[10]").WithArguments("System.Span<int>").WithLocation(24, 19),
1224
                // (31,21): error CS8353: A result of a stackalloc expression of type 'Span<int>' cannot be used in this context because it may be exposed outside of the containing method
1225 1226 1227 1228
                //             local = stackalloc int[10];
                Diagnostic(ErrorCode.ERR_EscapeStackAlloc, "stackalloc int[10]").WithArguments("System.Span<int>").WithLocation(31, 21)
                );
        }
1229 1230 1231 1232 1233 1234

        [WorkItem(21831, "https://github.com/dotnet/roslyn/issues/21831")]
        [Fact()]
        public void LocalWithNoInitializerEscape()
        {
            var text = @"
V
vsadov 已提交
1235
    using System;
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
    class Program
    {
        static void Main()
        {
            // uninitialized
            S1 sp;

            // ok
            sp = default;
            
V
vsadov 已提交
1246
            Span<int> local = stackalloc int[1];
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257

            // error
            sp = MayWrap(ref local);
        }

        static S1 SefReferringTest()
        {
            S1 sp1 = sp1;
            return sp1;
        }

V
vsadov 已提交
1258
        static S1 MayWrap(ref Span<int> arg)
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
        {
            return default;
        }

        ref struct S1
        {
            public ref int this[int i] => throw null;
        }
    }
";
            CreateCompilationWithMscorlibAndSpan(text).VerifyDiagnostics(
V
vsadov 已提交
1270
                // (16,30): error CS8526: Cannot use local 'local' in this context because it may expose referenced variables outside of their declaration scope
1271
                //             sp = MayWrap(ref local);
V
vsadov 已提交
1272 1273
                Diagnostic(ErrorCode.ERR_EscapeLocal, "local").WithArguments("local").WithLocation(16, 30),
                // (16,18): error CS8521: Cannot use a result of 'Program.MayWrap(ref Span<int>)' in this context because it may expose variables referenced by parameter 'arg' outside of their declaration scope
1274
                //             sp = MayWrap(ref local);
V
vsadov 已提交
1275 1276
                Diagnostic(ErrorCode.ERR_EscapeCall, "MayWrap(ref local)").WithArguments("Program.MayWrap(ref System.Span<int>)", "arg").WithLocation(16, 18),
                // (22,20): error CS8526: Cannot use local 'sp1' in this context because it may expose referenced variables outside of their declaration scope
1277
                //             return sp1;
V
vsadov 已提交
1278
                Diagnostic(ErrorCode.ERR_EscapeLocal, "sp1").WithArguments("sp1").WithLocation(22, 20)
1279 1280
                );
        }
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329

        [WorkItem(21858, "https://github.com/dotnet/roslyn/issues/21858")]
        [Fact()]
        public void FieldOfRefLikeEscape()
        {
            var text = @"
    class Program
    {
        static void Main()
        {
        }

        ref struct S1
        {
            private S2 x;

            public S1(S2 arg) => x = arg;

            public S2 M1()
            {
                // ok
                return x;
            }

            public S2 M2()
            {
                var toReturn = x;

                // ok
                return toReturn;
            }

            public ref S2 M3()
            {
                // not ok
                return ref x;
            }
        }

        ref struct S2{}

    }
";
            CreateCompilationWithMscorlibAndSpan(text).VerifyDiagnostics(
                // (31,28): error CS8170: Struct members cannot return 'this' or other instance members by reference
                //                 return ref x;
                Diagnostic(ErrorCode.ERR_RefReturnStructThis, "x").WithArguments("this").WithLocation(31, 28)
                );
        }
1330 1331 1332 1333 1334 1335

        [WorkItem(21880, "https://github.com/dotnet/roslyn/issues/21880")]
        [Fact()]
        public void MemberOfReadonlyRefLikeEscape()
        {
            var text = @"
V
vsadov 已提交
1336
    using System;
1337 1338 1339 1340 1341
    public static class Program
    {
        public static void Main()
        {
            // OK, SR is readonly
V
vsadov 已提交
1342 1343
            Span<int> value1 = stackalloc int[1];
            new SR().TryGet(out value1);
1344

V
vsadov 已提交
1345 1346
            // error, TryGet can write into the instance
            new SW().TryGet(out value1);
1347 1348 1349 1350 1351
        }
    }

    public readonly ref struct SR
    {
V
vsadov 已提交
1352
        public void TryGet(out Span<int> result)
1353
        {
V
vsadov 已提交
1354
            result = default;
1355 1356 1357 1358 1359
        }
    }

    public ref struct SW
    {
V
vsadov 已提交
1360
        public void TryGet(out Span<int> result)
1361
        {
V
vsadov 已提交
1362
            result = default;
1363 1364 1365 1366
        }
    }
";
            CreateCompilationWithMscorlibAndSpan(text).VerifyDiagnostics(
V
vsadov 已提交
1367 1368 1369 1370 1371 1372
                // (12,33): error CS8526: Cannot use local 'value1' in this context because it may expose referenced variables outside of their declaration scope
                //             new SW().TryGet(out value1);
                Diagnostic(ErrorCode.ERR_EscapeLocal, "value1").WithArguments("value1").WithLocation(12, 33),
                // (12,13): error CS8524: This combination of arguments to 'SW.TryGet(out Span<int>)' is disallowed because it may expose variables referenced by parameter 'result' outside of their declaration scope
                //             new SW().TryGet(out value1);
                Diagnostic(ErrorCode.ERR_CallArgMixing, "new SW().TryGet(out value1)").WithArguments("SW.TryGet(out System.Span<int>)", "result").WithLocation(12, 13)
1373 1374
                );
        }
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410

        [WorkItem(21911, "https://github.com/dotnet/roslyn/issues/21911")]
        [Fact()]
        public void MemberOfReadonlyRefLikeEscapeSpans()
        {
            var text = @"
    using System;

    public static class Program
    {
        public static void Main()
        {
            Span<int> stackAllocated = stackalloc int[100];

            // OK, Span is a readonly struct
            new Span<int>().CopyTo(stackAllocated);

            // OK, ReadOnlySpan is a readonly struct
            new ReadOnlySpan<int>().CopyTo(stackAllocated);

            // not OK, Span is writeable
            new NotReadOnly<int>().CopyTo(stackAllocated);
        }
    }

    public ref struct NotReadOnly<T>
    {
        private Span<T> wrapped;

        public void CopyTo(Span<T> other)
        {
            wrapped = other;
        }
    }
";
            CreateCompilationWithMscorlibAndSpan(text).VerifyDiagnostics(
1411
                // (17,43): error CS8352: Cannot use local 'stackAllocated' in this context because it may expose referenced variables outside of their declaration scope
1412 1413
                //             new NotReadOnly<int>().CopyTo(stackAllocated);
                Diagnostic(ErrorCode.ERR_EscapeLocal, "stackAllocated").WithArguments("stackAllocated").WithLocation(17, 43),
1414
                // (17,13): error CS8350: This combination of arguments to 'NotReadOnly<int>.CopyTo(Span<int>)' is disallowed because it may expose variables referenced by parameter 'other' outside of their declaration scope
1415 1416 1417 1418
                //             new NotReadOnly<int>().CopyTo(stackAllocated);
                Diagnostic(ErrorCode.ERR_CallArgMixing, "new NotReadOnly<int>().CopyTo(stackAllocated)").WithArguments("NotReadOnly<int>.CopyTo(System.Span<int>)", "other").WithLocation(17, 13)
                );
        }
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505

        [WorkItem(22197, "https://github.com/dotnet/roslyn/issues/22197")]
        [Fact()]
        public void RefTernaryMustMatchValEscapes()
        {
            var text = @"
    using System;

    public class C
    {
        bool flag1 = true;
        bool flag2 = false;

        public void M(ref Span<int> global)
        {
            Span<int> local = stackalloc int[10];

            ref var r1 = ref (flag1 ? ref global : ref local);
            ref var r2 = ref (flag2 ? ref global : ref local);

	        // same as         global = local;   which would be an error.
            // but we can’t fail here, since r1 and r2 are basically the same, 
            // so should fail when making r1 and r2 above.
            r1 = r2;   
        }

        public static void Main()
        {
        }
    }
";
            CreateCompilationWithMscorlibAndSpan(text).VerifyDiagnostics(
                // (13,56): error CS8526: Cannot use local 'local' in this context because it may expose referenced variables outside of their declaration scope
                //             ref var r1 = ref (flag1 ? ref global : ref local);
                Diagnostic(ErrorCode.ERR_EscapeLocal, "local").WithArguments("local").WithLocation(13, 56),
                // (13,31): error CS8525: Branches of a ref ternary operator cannot refer to variables with incompatible declaration scopes
                //             ref var r1 = ref (flag1 ? ref global : ref local);
                Diagnostic(ErrorCode.ERR_MismatchedRefEscapeInTernary, "flag1 ? ref global : ref local").WithLocation(13, 31),
                // (14,56): error CS8526: Cannot use local 'local' in this context because it may expose referenced variables outside of their declaration scope
                //             ref var r2 = ref (flag2 ? ref global : ref local);
                Diagnostic(ErrorCode.ERR_EscapeLocal, "local").WithArguments("local").WithLocation(14, 56),
                // (14,31): error CS8525: Branches of a ref ternary operator cannot refer to variables with incompatible declaration scopes
                //             ref var r2 = ref (flag2 ? ref global : ref local);
                Diagnostic(ErrorCode.ERR_MismatchedRefEscapeInTernary, "flag2 ? ref global : ref local").WithLocation(14, 31)
            );
        }

        [WorkItem(22197, "https://github.com/dotnet/roslyn/issues/22197")]
        [Fact()]
        public void RefTernaryMustMatchValEscapes1()
        {
            var text = @"
    using System;

    public class C
    {
        public void M(ref Span<int> global)
        {
            Span<int> local = stackalloc int[0];

            // ok
            (true ? ref local : ref local) = (false ? ref global : ref global);

            // also OK
            (true ? ref local : ref local) = (false ? global : local);
        }

        public static void Main()
        {
        }
    }
";
            var comp = CreateCompilationWithMscorlibAndSpan(text);
            comp.VerifyDiagnostics();

            var compiled = CompileAndVerify(comp,expectedOutput: "", verify: false);
            compiled.VerifyIL("C.M(ref System.Span<int>)", @"
{
  // Code size        8 (0x8)
  .maxstack  1
  IL_0000:  ldarg.1
  IL_0001:  ldobj      ""System.Span<int>""
  IL_0006:  pop
  IL_0007:  ret
}
");
        }
1506 1507
    }
}