Skip to content

Commit 81b66f3

Browse files
committed
Cleanup
1 parent 1a69700 commit 81b66f3

7 files changed

Lines changed: 104 additions & 22 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ internal override QuickAttributeChecker QuickAttributeChecker
186186
usingDirectives = compilationUnit.Usings;
187187
break;
188188

189-
case NamespaceDeclarationSyntax namespaceDecl:
189+
case BaseNamespaceDeclarationSyntax namespaceDecl:
190190
usingDirectives = namespaceDecl.Usings;
191191
break;
192192

@@ -207,7 +207,7 @@ protected override ImportChain BuildImportChain()
207207
{
208208
var previous = Next!.ImportChain;
209209

210-
if (_declarationSyntax is NamespaceDeclarationSyntax namespaceDecl)
210+
if (_declarationSyntax is BaseNamespaceDeclarationSyntax namespaceDecl)
211211
{
212212
// For each dotted name add an empty entry in the chain
213213
var name = namespaceDecl.Name;

src/Compilers/CSharp/Portable/Compilation/SyntaxTreeSemanticModel.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2278,8 +2278,7 @@ private NamespaceOrTypeSymbol GetDeclaredTypeMemberContainer(CSharpSyntaxNode me
22782278
if (memberDeclaration.Parent.Kind() == SyntaxKind.CompilationUnit)
22792279
{
22802280
// top-level namespace:
2281-
if (memberDeclaration.Kind() == SyntaxKind.NamespaceDeclaration ||
2282-
memberDeclaration.Kind() == SyntaxKind.SingleLineNamespaceDeclaration)
2281+
if (memberDeclaration.Kind() is SyntaxKind.NamespaceDeclaration or SyntaxKind.SingleLineNamespaceDeclaration)
22832282
{
22842283
return _compilation.Assembly.GlobalNamespace;
22852284
}
@@ -2310,8 +2309,8 @@ private NamespaceOrTypeSymbol GetDeclaredTypeMemberContainer(CSharpSyntaxNode me
23102309
}
23112310

23122311
// a namespace or a type in an explicitly declared namespace:
2313-
if (memberDeclaration.Kind() is SyntaxKind.NamespaceDeclaration or SyntaxKind.SingleLineNamespaceDeclaration ||
2314-
SyntaxFacts.IsTypeDeclaration(memberDeclaration.Kind()))
2312+
if (memberDeclaration.Kind() is SyntaxKind.NamespaceDeclaration or SyntaxKind.SingleLineNamespaceDeclaration
2313+
|| SyntaxFacts.IsTypeDeclaration(memberDeclaration.Kind()))
23152314
{
23162315
return container;
23172316
}

src/Compilers/CSharp/Portable/Parser/LanguageParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ private BaseNamespaceDeclarationSyntax ParseNamespaceDeclarationCore(
290290

291291
SyntaxListBuilder initialBadNodes = null;
292292
this.ParseNamespaceBody(ref semicolon, ref body, ref initialBadNodes, SyntaxKind.SingleLineNamespaceDeclaration);
293-
Debug.Assert(initialBadNodes == null); // init bad nodes should have been attached to open brace...
293+
Debug.Assert(initialBadNodes == null); // init bad nodes should have been attached to semicolon...
294294

295295
namespaceToken = CheckFeatureAvailability(namespaceToken, MessageID.IDS_SingleLineNamespace);
296296
return _syntaxFactory.SingleLineNamespaceDeclaration(

src/Compilers/CSharp/Test/Symbol/Symbols/AssemblyAndNamespaceTests.cs

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ public static int Main()
590590
}
591591
}
592592
";
593-
CreateCompilationWithMscorlib45(test).VerifyDiagnostics(
593+
CreateCompilationWithMscorlib45(test, parseOptions: TestOptions.RegularWithFileScopedNamespaces).VerifyDiagnostics(
594594
// (2,1): error CS1671: A namespace declaration cannot have modifiers or attributes
595595
Diagnostic(ErrorCode.ERR_BadModifiersOnNamespace, "public").WithLocation(2, 1));
596596
}
@@ -626,15 +626,13 @@ namespace N { }
626626
Diagnostic(ErrorCode.ERR_BadModifiersOnNamespace, "[System.Obsolete]").WithLocation(1, 1));
627627
}
628628

629-
private static readonly CSharpParseOptions s_previewOptions = CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.Preview);
630-
631629
[Fact]
632630
public void NamespaceWithSemicolon1()
633631
{
634632
var test =
635633
@"namespace A;";
636634

637-
CreateCompilationWithMscorlib45(test, parseOptions: s_previewOptions).VerifyDiagnostics();
635+
CreateCompilationWithMscorlib45(test, parseOptions: TestOptions.RegularWithFileScopedNamespaces).VerifyDiagnostics();
638636
}
639637

640638
[Fact]
@@ -643,7 +641,7 @@ public void NamespaceWithSemicolon3()
643641
var test =
644642
@"namespace A.B;";
645643

646-
CreateCompilationWithMscorlib45(test, parseOptions: s_previewOptions).VerifyDiagnostics();
644+
CreateCompilationWithMscorlib45(test, parseOptions: TestOptions.RegularWithFileScopedNamespaces).VerifyDiagnostics();
647645
}
648646

649647
[Fact]
@@ -653,7 +651,7 @@ public void MultipleSingleLineNamespaces()
653651
@"namespace A;
654652
namespace B;";
655653

656-
CreateCompilationWithMscorlib45(test, parseOptions: s_previewOptions).VerifyDiagnostics(
654+
CreateCompilationWithMscorlib45(test, parseOptions: TestOptions.RegularWithFileScopedNamespaces).VerifyDiagnostics(
657655
// (2,11): error CS8907: Source file can only contain one single-line namespace declaration.
658656
// namespace B;
659657
Diagnostic(ErrorCode.ERR_MultipleSingleLineNamespace, "B").WithLocation(2, 11));
@@ -668,7 +666,7 @@ public void SingleLineNamespaceNestedInNormalNamespace()
668666
namespace B;
669667
}";
670668

671-
CreateCompilationWithMscorlib45(test, parseOptions: s_previewOptions).VerifyDiagnostics(
669+
CreateCompilationWithMscorlib45(test, parseOptions: TestOptions.RegularWithFileScopedNamespaces).VerifyDiagnostics(
672670
// (3,15): error CS8908: Source file can not contain both single-line and normal namespace declarations.
673671
// namespace B;
674672
Diagnostic(ErrorCode.ERR_SingleLineAndNormalNamespace, "B").WithLocation(3, 15));
@@ -683,7 +681,7 @@ namespace B
683681
{
684682
}";
685683

686-
CreateCompilationWithMscorlib45(test, parseOptions: s_previewOptions).VerifyDiagnostics(
684+
CreateCompilationWithMscorlib45(test, parseOptions: TestOptions.RegularWithFileScopedNamespaces).VerifyDiagnostics(
687685
// (2,11): error CS8908: Source file can only contain single-line and normal namespace declarations.
688686
// namespace B
689687
Diagnostic(ErrorCode.ERR_SingleLineAndNormalNamespace, "B").WithLocation(2, 11));
@@ -698,7 +696,7 @@ public void NormalAndSingleLineNamespace2()
698696
}
699697
namespace B;";
700698

701-
CreateCompilationWithMscorlib45(test, parseOptions: s_previewOptions).VerifyDiagnostics(
699+
CreateCompilationWithMscorlib45(test, parseOptions: TestOptions.RegularWithFileScopedNamespaces).VerifyDiagnostics(
702700
// (4,11): error CS8909: Single-line namespace must precede all other members in a file.
703701
// namespace B;
704702
Diagnostic(ErrorCode.ERR_SingleLineNamespaceNotBeforeAllMembers, "B").WithLocation(4, 11));
@@ -711,7 +709,7 @@ public void NamespaceWithPrecedingUsing()
711709
@"using System;
712710
namespace A;";
713711

714-
CreateCompilationWithMscorlib45(test, parseOptions: s_previewOptions).VerifyDiagnostics(
712+
CreateCompilationWithMscorlib45(test, parseOptions: TestOptions.RegularWithFileScopedNamespaces).VerifyDiagnostics(
715713
// (1,1): hidden CS8019: Unnecessary using directive.
716714
// using System;
717715
Diagnostic(ErrorCode.HDN_UnusedUsingDirective, "using System;").WithLocation(1, 1));
@@ -724,7 +722,7 @@ public void NamespaceWithFollowingUsing()
724722
@"namespace X;
725723
using System;";
726724

727-
CreateCompilationWithMscorlib45(test, parseOptions: s_previewOptions).VerifyDiagnostics(
725+
CreateCompilationWithMscorlib45(test, parseOptions: TestOptions.RegularWithFileScopedNamespaces).VerifyDiagnostics(
728726
// (2,1): hidden CS8019: Unnecessary using directive.
729727
// using System;
730728
Diagnostic(ErrorCode.HDN_UnusedUsingDirective, "using System;").WithLocation(2, 1));
@@ -737,7 +735,7 @@ public void NamespaceWithPrecedingType()
737735
@"class X { }
738736
namespace System;";
739737

740-
CreateCompilationWithMscorlib45(test, parseOptions: s_previewOptions).VerifyDiagnostics(
738+
CreateCompilationWithMscorlib45(test, parseOptions: TestOptions.RegularWithFileScopedNamespaces).VerifyDiagnostics(
741739
// (2,11): error CS8909: Single-line namespace must precede all other members in a file.
742740
// namespace System;
743741
Diagnostic(ErrorCode.ERR_SingleLineNamespaceNotBeforeAllMembers, "System").WithLocation(2, 11));
@@ -750,7 +748,7 @@ public void NamespaceWithFollowingType()
750748
@"namespace System;
751749
class X { }";
752750

753-
CreateCompilationWithMscorlib45(test, parseOptions: s_previewOptions).VerifyDiagnostics();
751+
CreateCompilationWithMscorlib45(test, parseOptions: TestOptions.RegularWithFileScopedNamespaces).VerifyDiagnostics();
754752
}
755753

756754
[Fact]
@@ -761,7 +759,7 @@ public void SingleLineNamespaceWithPrecedingStatement()
761759
System.Console.WriteLine();
762760
namespace B;";
763761

764-
CreateCompilationWithMscorlib45(test, parseOptions: s_previewOptions).VerifyDiagnostics(
762+
CreateCompilationWithMscorlib45(test, parseOptions: TestOptions.RegularWithFileScopedNamespaces).VerifyDiagnostics(
765763
// (3,11): error CS8914: Single-line namespace must precede all other members in a file.
766764
// namespace B;
767765
Diagnostic(ErrorCode.ERR_SingleLineNamespaceNotBeforeAllMembers, "B").WithLocation(3, 11));
@@ -775,7 +773,7 @@ public void SingleLineNamespaceWithFollowingStatement()
775773
namespace B;
776774
System.Console.WriteLine();";
777775

778-
CreateCompilationWithMscorlib45(test, parseOptions: s_previewOptions).VerifyDiagnostics(
776+
CreateCompilationWithMscorlib45(test, parseOptions: TestOptions.RegularWithFileScopedNamespaces).VerifyDiagnostics(
779777
// (3,16): error CS0116: A namespace cannot directly contain members such as fields or methods
780778
// System.Console.WriteLine();
781779
Diagnostic(ErrorCode.ERR_NamespaceUnexpected, "WriteLine").WithLocation(3, 16),
@@ -786,5 +784,51 @@ namespace B;
786784
// System.Console.WriteLine();
787785
Diagnostic(ErrorCode.ERR_EOFExpected, ";").WithLocation(3, 27));
788786
}
787+
788+
[Fact]
789+
public void SingleLineNamespaceUsingsBeforeAndAfter()
790+
{
791+
var source1 = @"
792+
namespace A
793+
{
794+
class C1 { }
795+
}
796+
797+
namespace B
798+
{
799+
class C2 { }
800+
}
801+
";
802+
var source2 = @"
803+
using A;
804+
namespace X;
805+
using B;
806+
807+
class C
808+
{
809+
void M()
810+
{
811+
new C1();
812+
new C2();
813+
}
814+
}
815+
";
816+
817+
CreateCompilationWithMscorlib45(new[] { source1, source2 }, parseOptions: TestOptions.RegularWithFileScopedNamespaces).VerifyDiagnostics();
818+
}
819+
820+
[Fact]
821+
public void SingleLineNamespaceFollowedByVariable()
822+
{
823+
var test = @"
824+
namespace B;
825+
int x; // 1
826+
";
827+
828+
CreateCompilationWithMscorlib45(test, parseOptions: TestOptions.RegularWithFileScopedNamespaces).VerifyDiagnostics(
829+
// (3,5): error CS0116: A namespace cannot directly contain members such as fields, methods or statements
830+
// int x; // 1
831+
Diagnostic(ErrorCode.ERR_NamespaceUnexpected, "x").WithLocation(3, 5));
832+
}
789833
}
790834
}

src/Compilers/CSharp/Test/Syntax/Parsing/ParsingErrorRecoveryTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7096,6 +7096,7 @@ class c
70967096
Assert.True(classDecl.OpenBraceToken.IsMissing);
70977097
Assert.True(classDecl.CloseBraceToken.IsMissing);
70987098
var ns = root.DescendantNodes().OfType<SingleLineNamespaceDeclarationSyntax>().Single();
7099+
Assert.False(ns.SemicolonToken.IsMissing);
70997100
}
71007101

71017102
[WorkItem(947819, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/947819")]

src/Compilers/CSharp/Test/Syntax/Parsing/SingleLineDeclarationParsingTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,5 +814,42 @@ public void NamespaceWithAttributes()
814814
}
815815
EOF();
816816
}
817+
818+
[Fact]
819+
public void NamespaceFollowedByVariable()
820+
{
821+
UsingNode(
822+
@"namespace A; int x;", TestOptions.RegularPreview);
823+
824+
N(SyntaxKind.CompilationUnit);
825+
{
826+
N(SyntaxKind.SingleLineNamespaceDeclaration);
827+
{
828+
N(SyntaxKind.NamespaceKeyword);
829+
N(SyntaxKind.IdentifierName);
830+
{
831+
N(SyntaxKind.IdentifierToken, "A");
832+
}
833+
N(SyntaxKind.SemicolonToken);
834+
N(SyntaxKind.FieldDeclaration);
835+
{
836+
N(SyntaxKind.VariableDeclaration);
837+
{
838+
N(SyntaxKind.PredefinedType);
839+
{
840+
N(SyntaxKind.IntKeyword);
841+
}
842+
N(SyntaxKind.VariableDeclarator);
843+
{
844+
N(SyntaxKind.IdentifierToken, "x");
845+
}
846+
}
847+
N(SyntaxKind.SemicolonToken);
848+
}
849+
}
850+
N(SyntaxKind.EndOfFileToken);
851+
}
852+
EOF();
853+
}
817854
}
818855
}

src/Compilers/Test/Utilities/CSharp/TestOptions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public static class TestOptions
3333
public static readonly CSharpParseOptions WithoutCovariantReturns = Regular.WithLanguageVersion(LanguageVersion.CSharp8);
3434

3535
public static readonly CSharpParseOptions RegularWithExtendedPartialMethods = RegularPreview;
36+
public static readonly CSharpParseOptions RegularWithFileScopedNamespaces = Regular.WithLanguageVersion(MessageID.IDS_SingleLineNamespace.RequiredVersion());
3637

3738
private static readonly SmallDictionary<string, string> s_experimentalFeatures = new SmallDictionary<string, string> { };
3839
public static readonly CSharpParseOptions ExperimentalParseOptions =

0 commit comments

Comments
 (0)