Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
228e2dcf
R
roslyn
项目概览
lwm1986
/
roslyn
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
roslyn
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
228e2dcf
编写于
7月 28, 2020
作者:
J
Julien Couvreur
提交者:
GitHub
7月 28, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Always use a ParameterSyntax to make record properties (#46225)
上级
ded0bc5c
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
273 addition
and
3 deletion
+273
-3
src/Compilers/CSharp/Portable/Symbols/Symbol.cs
src/Compilers/CSharp/Portable/Symbols/Symbol.cs
+27
-3
src/Compilers/CSharp/Test/Semantic/Semantics/RecordTests.cs
src/Compilers/CSharp/Test/Semantic/Semantics/RecordTests.cs
+246
-0
未找到文件。
src/Compilers/CSharp/Portable/Symbols/Symbol.cs
浏览文件 @
228e2dcf
...
...
@@ -348,18 +348,42 @@ internal static ImmutableArray<SyntaxReference> GetDeclaringSyntaxReferenceHelpe
foreach
(
Location
location
in
locations
)
{
// Location may be null. See https://github.com/dotnet/roslyn/issues/28862.
if
(
location
==
null
)
if
(
location
==
null
||
!
location
.
IsInSource
)
{
continue
;
}
if
(
location
.
IsInSource
)
if
(
location
.
SourceSpan
.
Length
!=
0
)
{
SyntaxToken
token
=
(
SyntaxToken
)
location
.
SourceTree
.
GetRoot
().
FindToken
(
location
.
SourceSpan
.
Start
);
SyntaxToken
token
=
location
.
SourceTree
.
GetRoot
().
FindToken
(
location
.
SourceSpan
.
Start
);
if
(
token
.
Kind
()
!=
SyntaxKind
.
None
)
{
CSharpSyntaxNode
node
=
token
.
Parent
.
FirstAncestorOrSelf
<
TNode
>();
if
(
node
!=
null
)
{
builder
.
Add
(
node
.
GetReference
());
}
}
}
else
{
// Since the location we're interested in can't contain a token, we'll inspect the whole tree,
// pruning away branches that don't contain that location. We'll pick the narrowest node of the type
// we're looking for.
// eg: finding the ParameterSyntax from the empty location of a blank identifier
SyntaxNode
parent
=
location
.
SourceTree
.
GetRoot
();
SyntaxNode
found
=
null
;
foreach
(
var
descendant
in
parent
.
DescendantNodesAndSelf
(
c
=>
c
.
Location
.
SourceSpan
.
Contains
(
location
.
SourceSpan
)))
{
if
(
descendant
is
TNode
&&
descendant
.
Location
.
SourceSpan
.
Contains
(
location
.
SourceSpan
))
{
found
=
descendant
;
}
}
if
(
found
is
object
)
{
builder
.
Add
(
found
.
GetReference
());
}
}
}
...
...
src/Compilers/CSharp/Test/Semantic/Semantics/RecordTests.cs
浏览文件 @
228e2dcf
...
...
@@ -147,6 +147,243 @@ class Point(int x, int y);
comp.VerifyDiagnostics();
}
[Fact, WorkItem(46123, "https://github.com/dotnet/roslyn/issues/46123")]
public void IncompletePositionalRecord()
{
string source = @"
public record A(int i,) { }
";
var comp = CreateCompilation(source);
comp.VerifyEmitDiagnostics(
// (2,23): error CS1031: Type expected
// public record A(int i,) { }
Diagnostic(ErrorCode.ERR_TypeExpected, ")").WithLocation(2, 23),
// (2,23): error CS1001: Identifier expected
// public record A(int i,) { }
Diagnostic(ErrorCode.ERR_IdentifierExpected, ")").WithLocation(2, 23)
);
var expectedMembers = new[]
{
"System.Type A.EqualityContract { get; }",
"System.Int32 A.i { get; init; }",
"? A. { get; init; }"
};
AssertEx.Equal(expectedMembers,
comp.GetMember<NamedTypeSymbol>("A").GetMembers().OfType<PropertySymbol>().ToTestDisplayStrings());
AssertEx.Equal(new[] { "A..ctor(System.Int32 i, ? )", "A..ctor(A original)" },
comp.GetMember<NamedTypeSymbol>("A").Constructors.ToTestDisplayStrings());
var primaryCtor = comp.GetMember<NamedTypeSymbol>("A").Constructors.First();
Assert.Equal("A..ctor(System.Int32 i, ? )", primaryCtor.ToTestDisplayString());
Assert.IsType<ParameterSyntax>(primaryCtor.Parameters[1].DeclaringSyntaxReferences.Single().GetSyntax());
}
[Fact, WorkItem(46123, "https://github.com/dotnet/roslyn/issues/46123")]
public void IncompletePositionalRecord_WithTrivia()
{
string source = @"
public record A(int i, // A
// B
, /* C */ ) { }
";
var comp = CreateCompilation(source);
comp.VerifyEmitDiagnostics(
// (2,23): error CS1031: Type expected
// public record A(int i, // A
Diagnostic(ErrorCode.ERR_TypeExpected, "").WithLocation(2, 23),
// (2,23): error CS1001: Identifier expected
// public record A(int i, // A
Diagnostic(ErrorCode.ERR_IdentifierExpected, "").WithLocation(2, 23),
// (4,15): error CS1031: Type expected
// , /* C */ ) { }
Diagnostic(ErrorCode.ERR_TypeExpected, ")").WithLocation(4, 15),
// (4,15): error CS1001: Identifier expected
// , /* C */ ) { }
Diagnostic(ErrorCode.ERR_IdentifierExpected, ")").WithLocation(4, 15),
// (4,15): error CS0102: The type 'A' already contains a definition for ''
// , /* C */ ) { }
Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "").WithArguments("A", "").WithLocation(4, 15)
);
var primaryCtor = comp.GetMember<NamedTypeSymbol>("A").Constructors.First();
Assert.Equal("A..ctor(System.Int32 i, ? , ? )", primaryCtor.ToTestDisplayString());
Assert.IsType<ParameterSyntax>(primaryCtor.Parameters[0].DeclaringSyntaxReferences.Single().GetSyntax());
Assert.IsType<ParameterSyntax>(primaryCtor.Parameters[1].DeclaringSyntaxReferences.Single().GetSyntax());
Assert.IsType<ParameterSyntax>(primaryCtor.Parameters[2].DeclaringSyntaxReferences.Single().GetSyntax());
}
[Fact, WorkItem(46123, "https://github.com/dotnet/roslyn/issues/46123")]
public void IncompleteConstructor()
{
string source = @"
public class C
{
C(int i, ) { }
}
";
var comp = CreateCompilation(source);
comp.VerifyEmitDiagnostics(
// (4,14): error CS1031: Type expected
// C(int i, ) { }
Diagnostic(ErrorCode.ERR_TypeExpected, ")").WithLocation(4, 14),
// (4,14): error CS1001: Identifier expected
// C(int i, ) { }
Diagnostic(ErrorCode.ERR_IdentifierExpected, ")").WithLocation(4, 14)
);
var ctor = comp.GetMember<NamedTypeSymbol>("C").Constructors.Single();
Assert.Equal("C..ctor(System.Int32 i, ? )", ctor.ToTestDisplayString());
Assert.IsType<ParameterSyntax>(ctor.Parameters[1].DeclaringSyntaxReferences.Single().GetSyntax());
Assert.Equal(0, ctor.Parameters[1].Locations.Single().SourceSpan.Length);
}
[Fact, WorkItem(46123, "https://github.com/dotnet/roslyn/issues/46123")]
public void IncompletePositionalRecord_WithType()
{
string source = @"
public record A(int i, int ) { }
";
var comp = CreateCompilation(source);
comp.VerifyEmitDiagnostics(
// (2,28): error CS1001: Identifier expected
// public record A(int i, int ) { }
Diagnostic(ErrorCode.ERR_IdentifierExpected, ")").WithLocation(2, 28)
);
var expectedMembers = new[]
{
"System.Type A.EqualityContract { get; }",
"System.Int32 A.i { get; init; }",
"System.Int32 A. { get; init; }"
};
AssertEx.Equal(expectedMembers,
comp.GetMember<NamedTypeSymbol>("A").GetMembers().OfType<PropertySymbol>().ToTestDisplayStrings());
var ctor = comp.GetMember<NamedTypeSymbol>("A").Constructors[0];
Assert.Equal("A..ctor(System.Int32 i, System.Int32 )", ctor.ToTestDisplayString());
Assert.IsType<ParameterSyntax>(ctor.Parameters[1].DeclaringSyntaxReferences.Single().GetSyntax());
Assert.Equal(0, ctor.Parameters[1].Locations.Single().SourceSpan.Length);
}
[Fact, WorkItem(46123, "https://github.com/dotnet/roslyn/issues/46123")]
public void IncompletePositionalRecord_WithTwoTypes()
{
string source = @"
public record A(int, string ) { }
";
var comp = CreateCompilation(source);
comp.VerifyEmitDiagnostics(
// (2,20): error CS1001: Identifier expected
// public record A(int, string ) { }
Diagnostic(ErrorCode.ERR_IdentifierExpected, ",").WithLocation(2, 20),
// (2,29): error CS1001: Identifier expected
// public record A(int, string ) { }
Diagnostic(ErrorCode.ERR_IdentifierExpected, ")").WithLocation(2, 29),
// (2,29): error CS0102: The type 'A' already contains a definition for ''
// public record A(int, string ) { }
Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "").WithArguments("A", "").WithLocation(2, 29)
);
var expectedMembers = new[]
{
"System.Type A.EqualityContract { get; }",
"System.Int32 A. { get; init; }",
"System.String A. { get; init; }"
};
AssertEx.Equal(expectedMembers,
comp.GetMember<NamedTypeSymbol>("A").GetMembers().OfType<PropertySymbol>().ToTestDisplayStrings());
AssertEx.Equal(new[] { "A..ctor(System.Int32 , System.String )", "A..ctor(A original)" },
comp.GetMember<NamedTypeSymbol>("A").Constructors.ToTestDisplayStrings());
Assert.IsType<ParameterSyntax>(comp.GetMember<NamedTypeSymbol>("A").Constructors[0].Parameters[1].DeclaringSyntaxReferences.Single().GetSyntax());
}
[Fact, WorkItem(46123, "https://github.com/dotnet/roslyn/issues/46123")]
public void IncompletePositionalRecord_WithTwoTypes_SameType()
{
string source = @"
public record A(int, int ) { }
";
var comp = CreateCompilation(source);
comp.VerifyEmitDiagnostics(
// (2,20): error CS1001: Identifier expected
// public record A(int, int ) { }
Diagnostic(ErrorCode.ERR_IdentifierExpected, ",").WithLocation(2, 20),
// (2,26): error CS1001: Identifier expected
// public record A(int, int ) { }
Diagnostic(ErrorCode.ERR_IdentifierExpected, ")").WithLocation(2, 26),
// (2,26): error CS0102: The type 'A' already contains a definition for ''
// public record A(int, int ) { }
Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "").WithArguments("A", "").WithLocation(2, 26)
);
var expectedMembers = new[]
{
"System.Type A.EqualityContract { get; }",
"System.Int32 A. { get; init; }",
"System.Int32 A. { get; init; }"
};
AssertEx.Equal(expectedMembers,
comp.GetMember<NamedTypeSymbol>("A").GetMembers().OfType<PropertySymbol>().ToTestDisplayStrings());
AssertEx.Equal(new[] { "A..ctor(System.Int32 , System.Int32 )", "A..ctor(A original)" },
comp.GetMember<NamedTypeSymbol>("A").Constructors.ToTestDisplayStrings());
Assert.IsType<ParameterSyntax>(comp.GetMember<NamedTypeSymbol>("A").Constructors[0].Parameters[1].DeclaringSyntaxReferences.Single().GetSyntax());
}
[Fact, WorkItem(46123, "https://github.com/dotnet/roslyn/issues/46123")]
public void IncompletePositionalRecord_WithTwoTypes_WithTrivia()
{
string source = @"
public record A(int // A
// B
, int /* C */) { }
";
var comp = CreateCompilation(source);
comp.VerifyEmitDiagnostics(
// (2,20): error CS1001: Identifier expected
// public record A(int // A
Diagnostic(ErrorCode.ERR_IdentifierExpected, "").WithLocation(2, 20),
// (4,18): error CS1001: Identifier expected
// , int /* C */) { }
Diagnostic(ErrorCode.ERR_IdentifierExpected, ")").WithLocation(4, 18),
// (4,18): error CS0102: The type 'A' already contains a definition for ''
// , int /* C */) { }
Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "").WithArguments("A", "").WithLocation(4, 18)
);
var ctor = comp.GetMember<NamedTypeSymbol>("A").Constructors[0];
Assert.IsType<ParameterSyntax>(ctor.Parameters[0].DeclaringSyntaxReferences.Single().GetSyntax());
Assert.IsType<ParameterSyntax>(ctor.Parameters[1].DeclaringSyntaxReferences.Single().GetSyntax());
}
[Fact, WorkItem(46083, "https://github.com/dotnet/roslyn/issues/46083")]
public void IncompletePositionalRecord_SingleParameter()
{
string source = @"
record A(x)
";
var comp = CreateCompilation(source);
comp.VerifyEmitDiagnostics(
// (2,10): error CS0246: The type or namespace name 'x' could not be found (are you missing a using directive or an assembly reference?)
// record A(x)
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "x").WithArguments("x").WithLocation(2, 10),
// (2,11): error CS1001: Identifier expected
// record A(x)
Diagnostic(ErrorCode.ERR_IdentifierExpected, ")").WithLocation(2, 11),
// (2,12): error CS1514: { expected
// record A(x)
Diagnostic(ErrorCode.ERR_LbraceExpected, "").WithLocation(2, 12),
// (2,12): error CS1513: } expected
// record A(x)
Diagnostic(ErrorCode.ERR_RbraceExpected, "").WithLocation(2, 12)
);
}
[Fact]
public void TestInExpressionTree()
{
...
...
@@ -320,6 +557,15 @@ record C(int X, int X)
// record C(int X, int X)
Diagnostic(ErrorCode.ERR_DuplicateNameInClass, "X").WithArguments("C", "X").WithLocation(2, 21)
);
var expectedMembers = new[]
{
"System.Type C.EqualityContract { get; }",
"System.Int32 C.X { get; init; }",
"System.Int32 C.X { get; init; }"
};
AssertEx.Equal(expectedMembers,
comp.GetMember<NamedTypeSymbol>("C").GetMembers().OfType<PropertySymbol>().ToTestDisplayStrings());
}
[Fact]
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录