Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
97c66864
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,发现更多精彩内容 >>
提交
97c66864
编写于
3月 04, 2016
作者:
A
AlekseyTs
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #9442 from AlekseyTs/ctor_initializer
Test/fix scoping rules for pattern locals
上级
a3aee8c1
9d0b3a86
变更
13
展开全部
显示空白变更内容
内联
并排
Showing
13 changed file
with
987 addition
and
38 deletion
+987
-38
src/Compilers/CSharp/Portable/Binder/Binder.cs
src/Compilers/CSharp/Portable/Binder/Binder.cs
+19
-0
src/Compilers/CSharp/Portable/Binder/Binder_Attributes.cs
src/Compilers/CSharp/Portable/Binder/Binder_Attributes.cs
+1
-1
src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
+5
-8
src/Compilers/CSharp/Portable/Binder/Binder_Initializers.cs
src/Compilers/CSharp/Portable/Binder/Binder_Initializers.cs
+1
-1
src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs
src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs
+1
-1
src/Compilers/CSharp/Portable/Binder/PatternVariableBinder.cs
...Compilers/CSharp/Portable/Binder/PatternVariableBinder.cs
+17
-7
src/Compilers/CSharp/Portable/Binder/PatternVariableFinder.cs
...Compilers/CSharp/Portable/Binder/PatternVariableFinder.cs
+3
-0
src/Compilers/CSharp/Portable/Compilation/AttributeSemanticModel.cs
...ers/CSharp/Portable/Compilation/AttributeSemanticModel.cs
+1
-1
src/Compilers/CSharp/Portable/Compilation/InitializerSemanticModel.cs
...s/CSharp/Portable/Compilation/InitializerSemanticModel.cs
+2
-0
src/Compilers/CSharp/Portable/Compilation/SyntaxTreeSemanticModel.cs
...rs/CSharp/Portable/Compilation/SyntaxTreeSemanticModel.cs
+14
-6
src/Compilers/CSharp/Portable/Compiler/MethodCompiler.cs
src/Compilers/CSharp/Portable/Compiler/MethodCompiler.cs
+3
-1
src/Compilers/CSharp/Portable/Symbols/ConstantValueUtils.cs
src/Compilers/CSharp/Portable/Symbols/ConstantValueUtils.cs
+9
-3
src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests.cs
...rs/CSharp/Test/Semantic/Semantics/PatternMatchingTests.cs
+911
-9
未找到文件。
src/Compilers/CSharp/Portable/Binder/Binder.cs
浏览文件 @
97c66864
...
...
@@ -760,5 +760,24 @@ internal Binder WithPatternVariablesIfAny(ExpressionSyntax scopeOpt)
Debug
.
Assert
(
Locals
.
Length
==
0
);
return
new
PatternVariableBinder
(
scopeOpt
,
scopeOpt
,
this
);
}
internal
Binder
WithPatternVariablesIfAny
(
ArgumentListSyntax
initializerArgumentListOpt
)
{
Debug
.
Assert
(
Locals
.
Length
==
0
);
if
(
initializerArgumentListOpt
==
null
||
initializerArgumentListOpt
.
Arguments
.
Count
==
0
)
{
return
this
;
}
return
new
PatternVariableBinder
(
initializerArgumentListOpt
,
initializerArgumentListOpt
.
Arguments
,
this
);
}
internal
BoundExpression
WrapWithVariablesIfAny
(
BoundExpression
expression
)
{
return
(
Locals
.
Length
==
0
)
?
expression
:
new
BoundSequence
(
expression
.
Syntax
,
Locals
,
ImmutableArray
<
BoundExpression
>.
Empty
,
expression
,
expression
.
Type
)
{
WasCompilerGenerated
=
true
};
}
}
}
src/Compilers/CSharp/Portable/Binder/Binder_Attributes.cs
浏览文件 @
97c66864
...
...
@@ -94,7 +94,7 @@ internal static void BindAttributeTypes(ImmutableArray<Binder> binders, Immutabl
internal
CSharpAttributeData
GetAttribute
(
AttributeSyntax
node
,
NamedTypeSymbol
boundAttributeType
,
DiagnosticBag
diagnostics
)
{
var
boundAttribute
=
BindAttribute
(
node
,
boundAttributeType
,
diagnostics
);
var
boundAttribute
=
new
PatternVariableBinder
(
node
,
this
).
BindAttribute
(
node
,
boundAttributeType
,
diagnostics
);
return
GetAttribute
(
boundAttribute
,
diagnostics
);
}
...
...
src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
浏览文件 @
97c66864
...
...
@@ -339,7 +339,8 @@ internal BoundExpression BindValueAllowArgList(ExpressionSyntax node, Diagnostic
ParameterSymbol
parameter
,
EqualsValueClauseSyntax
defaultValueSyntax
)
{
return
new
LocalScopeBinder
(
this
.
WithContainingMemberOrLambda
(
parameter
.
ContainingSymbol
).
WithAdditionalFlags
(
BinderFlags
.
ParameterDefaultValue
));
return
new
LocalScopeBinder
(
this
.
WithContainingMemberOrLambda
(
parameter
.
ContainingSymbol
).
WithAdditionalFlags
(
BinderFlags
.
ParameterDefaultValue
)).
WithPatternVariablesIfAny
(
defaultValueSyntax
.
Value
);
}
internal
BoundExpression
BindParameterDefaultValue
(
...
...
@@ -357,7 +358,7 @@ internal BoundExpression BindValueAllowArgList(ExpressionSyntax node, Diagnostic
// Always generate the conversion, even if the expression is not convertible to the given type.
// We want the erroneous conversion in the tree.
return
GenerateConversionForAssignment
(
parameterType
,
valueBeforeConversion
,
diagnostics
,
isDefaultParameter
:
true
);
return
WrapWithVariablesIfAny
(
GenerateConversionForAssignment
(
parameterType
,
valueBeforeConversion
,
diagnostics
,
isDefaultParameter
:
true
)
);
}
internal
BoundExpression
BindEnumConstantInitializer
(
...
...
@@ -2644,12 +2645,8 @@ private static bool IsNegativeConstantForArraySize(BoundExpression expression)
MethodSymbol
constructor
,
DiagnosticBag
diagnostics
)
{
// Handle scoping for possible pattern variables declared in the initializer
PatternVariableBinder
patBinder
=
(
initializerArgumentListOpt
!=
null
)
?
new
PatternVariableBinder
(
initializerArgumentListOpt
,
initializerArgumentListOpt
.
Arguments
,
this
)
:
null
;
var
result
=
(
patBinder
??
this
).
BindConstructorInitializerCore
(
initializerArgumentListOpt
,
constructor
,
diagnostics
);
return
patBinder
?.
WrapWithPatternVariables
(
result
)
??
result
;
var
result
=
BindConstructorInitializerCore
(
initializerArgumentListOpt
,
constructor
,
diagnostics
);
return
WrapWithVariablesIfAny
(
result
);
}
private
BoundExpression
BindConstructorInitializerCore
(
...
...
src/Compilers/CSharp/Portable/Binder/Binder_Initializers.cs
浏览文件 @
97c66864
...
...
@@ -270,7 +270,7 @@ internal struct ProcessedFieldInitializers
var
collisionDetector
=
new
LocalScopeBinder
(
binder
);
var
patternBinder
=
new
PatternVariableBinder
(
equalsValueClauseNode
,
equalsValueClauseNode
.
Value
,
collisionDetector
);
var
boundInitValue
=
patternBinder
.
BindVariableOrAutoPropInitializer
(
equalsValueClauseNode
,
RefKind
.
None
,
fieldSymbol
.
GetFieldType
(
fieldsBeingBound
),
initializerDiagnostics
);
boundInitValue
=
patternBinder
.
WrapWith
PatternVariables
(
boundInitValue
);
boundInitValue
=
patternBinder
.
WrapWith
VariablesIfAny
(
boundInitValue
);
if
(
isImplicitlyTypedField
)
{
...
...
src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs
浏览文件 @
97c66864
...
...
@@ -3450,7 +3450,7 @@ private BoundExpression BindCatchFilter(CatchFilterClauseSyntax filter, Diagnost
Error
(
diagnostics
,
ErrorCode
.
WRN_FilterIsConstant
,
filter
.
FilterExpression
);
}
boundFilter
=
patternBinder
.
WrapWith
PatternVariables
(
boundFilter
);
boundFilter
=
patternBinder
.
WrapWith
VariablesIfAny
(
boundFilter
);
boundFilter
=
new
BoundSequencePointExpression
(
filter
,
boundFilter
,
boundFilter
.
Type
);
return
boundFilter
;
}
...
...
src/Compilers/CSharp/Portable/Binder/PatternVariableBinder.cs
浏览文件 @
97c66864
...
...
@@ -100,6 +100,23 @@ internal PatternVariableBinder(SyntaxNode syntax, ExpressionSyntax expression, B
this
.
Syntax
=
syntax
;
}
internal
PatternVariableBinder
(
AttributeSyntax
syntax
,
Binder
next
)
:
base
(
next
)
{
this
.
Syntax
=
syntax
;
if
(
syntax
.
ArgumentList
?.
Arguments
.
Count
>
0
)
{
var
expressions
=
ArrayBuilder
<
ExpressionSyntax
>.
GetInstance
(
syntax
.
ArgumentList
.
Arguments
.
Count
);
foreach
(
var
argument
in
syntax
.
ArgumentList
.
Arguments
)
{
expressions
.
Add
(
argument
.
Expression
);
}
this
.
Expressions
=
expressions
.
ToImmutableAndFree
();
}
}
protected
override
ImmutableArray
<
LocalSymbol
>
BuildLocals
()
{
var
patterns
=
PatternVariableFinder
.
FindPatternVariables
(
Expression
,
Expressions
,
this
.
Patterns
);
...
...
@@ -111,12 +128,5 @@ protected override ImmutableArray<LocalSymbol> BuildLocals()
patterns
.
Free
();
return
builder
.
ToImmutableAndFree
();
}
internal
BoundExpression
WrapWithPatternVariables
(
BoundExpression
expression
)
{
return
(
Locals
.
Length
==
0
)
?
expression
:
new
BoundSequence
(
expression
.
Syntax
,
Locals
,
ImmutableArray
<
BoundExpression
>.
Empty
,
expression
,
expression
.
Type
);
}
}
}
src/Compilers/CSharp/Portable/Binder/PatternVariableFinder.cs
浏览文件 @
97c66864
...
...
@@ -32,6 +32,9 @@ class PatternVariableFinder : CSharpSyntaxWalker
if
(
subExpression
!=
null
)
expressionsToVisit
.
Add
(
subExpression
);
}
}
expressionsToVisit
.
ReverseContents
();
finder
.
VisitExpressions
();
if
(!
patterns
.
IsDefaultOrEmpty
)
...
...
src/Compilers/CSharp/Portable/Compilation/AttributeSemanticModel.cs
浏览文件 @
97c66864
...
...
@@ -28,7 +28,7 @@ private AttributeSemanticModel(CSharpCompilation compilation, AttributeSyntax sy
public
static
AttributeSemanticModel
Create
(
CSharpCompilation
compilation
,
AttributeSyntax
syntax
,
NamedTypeSymbol
attributeType
,
AliasSymbol
aliasOpt
,
Binder
rootBinder
)
{
var
executableBinder
=
new
ExecutableCodeBinder
(
syntax
,
attributeType
,
rootBinder
);
return
new
AttributeSemanticModel
(
compilation
,
syntax
,
attributeType
,
aliasOpt
,
new
LocalScopeBinder
(
executableBinder
));
return
new
AttributeSemanticModel
(
compilation
,
syntax
,
attributeType
,
aliasOpt
,
new
PatternVariableBinder
(
syntax
,
executableBinder
));
}
/// <summary>
...
...
src/Compilers/CSharp/Portable/Compilation/InitializerSemanticModel.cs
浏览文件 @
97c66864
...
...
@@ -194,6 +194,7 @@ private BoundEqualsValue BindEqualsValue(Binder binder, EqualsValueClauseSyntax
}
if
(
result
!=
null
)
{
result
=
binder
.
WrapWithVariablesIfAny
(
result
);
return
new
BoundFieldEqualsValue
(
equalsValue
,
field
,
result
);
}
break
;
...
...
@@ -205,6 +206,7 @@ private BoundEqualsValue BindEqualsValue(Binder binder, EqualsValueClauseSyntax
BoundExpression
result
=
binder
.
BindVariableOrAutoPropInitializer
(
equalsValue
,
RefKind
.
None
,
property
.
Type
,
diagnostics
);
if
(
result
!=
null
)
{
result
=
binder
.
WrapWithVariablesIfAny
(
result
);
return
new
BoundPropertyEqualsValue
(
equalsValue
,
property
,
result
);
}
break
;
...
...
src/Compilers/CSharp/Portable/Compilation/SyntaxTreeSemanticModel.cs
浏览文件 @
97c66864
...
...
@@ -926,7 +926,7 @@ private MemberSemanticModel CreateMemberModel(CSharpSyntaxNode node)
variableDecl
,
//pass in the entire field initializer to permit region analysis.
fieldSymbol
,
//if we're in regular C#, then insert an extra binder to perform field initialization checks
GetFieldOrPropertyInitializerBinder
(
fieldSymbol
,
outer
));
GetFieldOrPropertyInitializerBinder
(
fieldSymbol
,
outer
,
variableDecl
.
Initializer
?.
Value
));
}
case
SyntaxKind
.
PropertyDeclaration
:
...
...
@@ -937,7 +937,7 @@ private MemberSemanticModel CreateMemberModel(CSharpSyntaxNode node)
this
.
Compilation
,
propertyDecl
,
propertySymbol
,
GetFieldOrPropertyInitializerBinder
(
propertySymbol
.
BackingField
,
outer
));
GetFieldOrPropertyInitializerBinder
(
propertySymbol
.
BackingField
,
outer
,
propertyDecl
.
Initializer
?.
Value
));
}
case
SyntaxKind
.
Parameter
:
...
...
@@ -968,7 +968,7 @@ private MemberSemanticModel CreateMemberModel(CSharpSyntaxNode node)
this
.
Compilation
,
enumDecl
,
enumSymbol
,
GetFieldOrPropertyInitializerBinder
(
enumSymbol
,
outer
));
GetFieldOrPropertyInitializerBinder
(
enumSymbol
,
outer
,
enumDecl
.
EqualsValue
?.
Value
));
}
default
:
throw
ExceptionUtilities
.
UnexpectedValue
(
node
.
Parent
.
Kind
());
...
...
@@ -1042,7 +1042,9 @@ private MemberSemanticModel CreateMemberModel(CSharpSyntaxNode node)
(
ConstructorInitializerSyntax
)
node
,
constructorSymbol
,
//insert an extra binder to perform constructor initialization checks
outer
.
WithAdditionalFlagsAndContainingMemberOrLambda
(
BinderFlags
.
ConstructorInitializer
,
constructorSymbol
));
// Handle scoping for possible pattern variables declared in the initializer
outer
.
WithAdditionalFlagsAndContainingMemberOrLambda
(
BinderFlags
.
ConstructorInitializer
,
constructorSymbol
).
WithPatternVariablesIfAny
(((
ConstructorInitializerSyntax
)
node
).
ArgumentList
));
}
case
SyntaxKind
.
Attribute
:
...
...
@@ -1084,7 +1086,7 @@ private SourceMemberFieldSymbol GetDeclaredFieldSymbol(VariableDeclaratorSyntax
return
null
;
}
private
Binder
GetFieldOrPropertyInitializerBinder
(
FieldSymbol
symbol
,
Binder
outer
)
private
Binder
GetFieldOrPropertyInitializerBinder
(
FieldSymbol
symbol
,
Binder
outer
,
ExpressionSyntax
valueSyntaxOpt
)
{
BinderFlags
flags
=
BinderFlags
.
None
;
...
...
@@ -1094,7 +1096,13 @@ private Binder GetFieldOrPropertyInitializerBinder(FieldSymbol symbol, Binder ou
flags
|=
BinderFlags
.
FieldInitializer
;
}
return
new
LocalScopeBinder
(
outer
).
WithAdditionalFlagsAndContainingMemberOrLambda
(
flags
,
symbol
);
Binder
result
=
new
LocalScopeBinder
(
outer
).
WithAdditionalFlagsAndContainingMemberOrLambda
(
flags
,
symbol
);
if
(
valueSyntaxOpt
!=
null
)
{
result
=
result
.
WithPatternVariablesIfAny
(
valueSyntaxOpt
);
}
return
result
;
}
private
static
bool
IsMemberDeclaration
(
CSharpSyntaxNode
node
)
...
...
src/Compilers/CSharp/Portable/Compiler/MethodCompiler.cs
浏览文件 @
97c66864
...
...
@@ -1726,7 +1726,9 @@ internal static BoundExpression BindConstructorInitializer(MethodSymbol construc
}
// wrap in ConstructorInitializerBinder for appropriate errors
Binder
initializerBinder
=
outerBinder
.
WithAdditionalFlagsAndContainingMemberOrLambda
(
BinderFlags
.
ConstructorInitializer
,
constructor
);
// Handle scoping for possible pattern variables declared in the initializer
Binder
initializerBinder
=
outerBinder
.
WithAdditionalFlagsAndContainingMemberOrLambda
(
BinderFlags
.
ConstructorInitializer
,
constructor
).
WithPatternVariablesIfAny
(
initializerArgumentListOpt
);
return
initializerBinder
.
BindConstructorInitializer
(
initializerArgumentListOpt
,
constructor
,
diagnostics
);
}
...
...
src/Compilers/CSharp/Portable/Symbols/ConstantValueUtils.cs
浏览文件 @
97c66864
...
...
@@ -52,15 +52,21 @@ internal static class ConstantValueUtils
DiagnosticBag
diagnostics
)
{
var
enumConstant
=
fieldSymbol
as
SourceEnumConstantSymbol
;
var
collisionDetector
=
new
LocalScopeBinder
(
binder
);
Binder
collisionDetector
=
new
LocalScopeBinder
(
binder
);
collisionDetector
=
collisionDetector
.
WithPatternVariablesIfAny
(
initializer
.
Value
);
BoundExpression
result
;
if
((
object
)
enumConstant
!=
null
)
{
re
turn
collisionDetector
.
BindEnumConstantInitializer
(
enumConstant
,
initializer
.
Value
,
diagnostics
);
re
sult
=
collisionDetector
.
BindEnumConstantInitializer
(
enumConstant
,
initializer
.
Value
,
diagnostics
);
}
else
{
re
turn
collisionDetector
.
BindVariableOrAutoPropInitializer
(
initializer
,
RefKind
.
None
,
fieldSymbol
.
Type
,
diagnostics
);
re
sult
=
collisionDetector
.
BindVariableOrAutoPropInitializer
(
initializer
,
RefKind
.
None
,
fieldSymbol
.
Type
,
diagnostics
);
}
result
=
collisionDetector
.
WrapWithVariablesIfAny
(
result
);
return
result
;
}
internal
static
ConstantValue
GetAndValidateConstantValue
(
...
...
src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests.cs
浏览文件 @
97c66864
此差异已折叠。
点击以展开。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录