Skip to content

Commit 39ed7eb

Browse files
author
Neal Gafter
authored
A fixed initializer must be bound to its natural type (#46293)
Fixes #46231
1 parent 183b5aa commit 39ed7eb

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,6 +1307,7 @@ private bool IsValidFixedVariableInitializer(TypeSymbol declType, SourceLocalSym
13071307
hasErrors = true;
13081308
}
13091309

1310+
initializerOpt = BindToNaturalType(initializerOpt, diagnostics, reportNoTargetType: false);
13101311
initializerOpt = GetFixedLocalCollectionInitializer(initializerOpt, elementType, declType, fixedPatternMethod, hasErrors, diagnostics);
13111312
return true;
13121313
}

src/Compilers/CSharp/Test/Semantic/Semantics/TargetTypedConditionalOperatorTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,5 +416,42 @@ public static void Test2() {
416416
.VerifyDiagnostics();
417417
CompileAndVerify(comp, expectedOutput: expectedOutput);
418418
}
419+
420+
[Fact, WorkItem(46231, "https://github.com/dotnet/roslyn/issues/46231")]
421+
public void TestFixedConditional()
422+
{
423+
var source = @"
424+
public class Program {
425+
public unsafe static void Test(bool b, int i)
426+
{
427+
fixed (byte * p = b ? new byte[i] : null)
428+
{
429+
}
430+
}
431+
}";
432+
CreateCompilation(source, parseOptions: TestOptions.Regular8, options: TestOptions.DebugDll.WithAllowUnsafe(true))
433+
.VerifyEmitDiagnostics();
434+
CreateCompilation(source, parseOptions: TestOptions.Regular.WithLanguageVersion(MessageID.IDS_FeatureTargetTypedConditional.RequiredVersion()), options: TestOptions.DebugDll.WithAllowUnsafe(true))
435+
.VerifyEmitDiagnostics();
436+
}
437+
438+
[Fact]
439+
public void TestUsingConditional()
440+
{
441+
var source = @"
442+
using System;
443+
public class Program {
444+
public static void Test(bool b, IDisposable d)
445+
{
446+
using (IDisposable x = b ? d : null)
447+
{
448+
}
449+
}
450+
}";
451+
CreateCompilation(source, parseOptions: TestOptions.Regular8, options: TestOptions.DebugDll)
452+
.VerifyEmitDiagnostics();
453+
CreateCompilation(source, parseOptions: TestOptions.Regular.WithLanguageVersion(MessageID.IDS_FeatureTargetTypedConditional.RequiredVersion()), options: TestOptions.DebugDll)
454+
.VerifyEmitDiagnostics();
455+
}
419456
}
420457
}

0 commit comments

Comments
 (0)