Skip to content

Record syntax in LangVer=8 should not complain about top-level statement #45900

@jcouv

Description

@jcouv

I'm not sure how to improve this, but I'm concerned it's going to cause a lot of confusion with users.
We should also test the scenario with record nested in another type.

        [Fact]
        public void Test()
        {
            var src = @"
record R(int P = 42)
{
}
";

            var comp = CreateCompilation(new[] { src, IsExternalInitTypeDefinition }, parseOptions: TestOptions.Regular8);
            comp.VerifyDiagnostics(
                // error CS8805: Program using top-level statements must be an executable.
                Diagnostic(ErrorCode.ERR_SimpleProgramNotAnExecutable).WithLocation(1, 1),
                // (2,1): error CS8652: The feature 'top-level statements' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version.
                // record R(int P = 42)
                Diagnostic(ErrorCode.ERR_FeatureInPreview, @"record R(int P = 42)
{
}").WithArguments("top-level statements").WithLocation(2, 1),
                // (2,1): error CS0246: The type or namespace name 'record' could not be found (are you missing a using directive or an assembly reference?)
                // record R(int P = 42)
                Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "record").WithArguments("record").WithLocation(2, 1),
                // (2,8): error CS0161: 'R(int)': not all code paths return a value
                // record R(int P = 42)
                Diagnostic(ErrorCode.ERR_ReturnExpected, "R").WithArguments("R(int)").WithLocation(2, 8),
                // (2,8): warning CS8321: The local function 'R' is declared but never used
                // record R(int P = 42)
                Diagnostic(ErrorCode.WRN_UnreferencedLocalFunction, "R").WithArguments("R").WithLocation(2, 8)
                );
        }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions