@@ -1950,6 +1950,9 @@ void Good(object o)
1950
1950
if (o is 1 and int x3) { }
1951
1951
if (o is (1 or 2) and int x4) { }
1952
1952
if (o is not (1 or 2) and int x5) { }
1953
+
1954
+ if (o is not int x6) { }
1955
+ if (o is not (1 and int x7)) { }
1953
1956
}
1954
1957
1955
1958
void Bad(object o)
@@ -1958,11 +1961,9 @@ void Bad(object o)
1958
1961
if (o is int y2 or (1 or 2)) { }
1959
1962
if (o is 1 or int y3) { }
1960
1963
if (o is (1 or 2) or int y4) { }
1961
- if (o is not int y5) { }
1962
- if (o is not (1 and int y6)) { }
1963
- if (o is Point { X: var y7 } or Animal _) { }
1964
- if (o is Point(var y8, _) or Animal _) { }
1965
- if (o is object or (1 or var y9)) { }
1964
+ if (o is Point { X: var y5 } or Animal _) { }
1965
+ if (o is Point(var y6, _) or Animal _) { }
1966
+ if (o is object or (1 or var y7)) { }
1966
1967
}
1967
1968
1968
1969
void NotBad(object o)
@@ -1984,36 +1985,30 @@ class Animal { }
1984
1985
" ;
1985
1986
var compilation = CreateCompilation ( source , parseOptions : TestOptions . RegularPreview ) ;
1986
1987
compilation . VerifyDiagnostics (
1987
- // (16 ,22): error CS8780: A variable may not be declared within a 'not' or 'or' pattern.
1988
+ // (19 ,22): error CS8780: A variable may not be declared within a 'not' or 'or' pattern.
1988
1989
// if (o is int y1 or 1) { }
1989
- Diagnostic ( ErrorCode . ERR_DesignatorBeneathPatternCombinator , "y1" ) . WithLocation ( 16 , 22 ) ,
1990
- // (17 ,22): error CS8780: A variable may not be declared within a 'not' or 'or' pattern.
1990
+ Diagnostic ( ErrorCode . ERR_DesignatorBeneathPatternCombinator , "y1" ) . WithLocation ( 19 , 22 ) ,
1991
+ // (20 ,22): error CS8780: A variable may not be declared within a 'not' or 'or' pattern.
1991
1992
// if (o is int y2 or (1 or 2)) { }
1992
- Diagnostic ( ErrorCode . ERR_DesignatorBeneathPatternCombinator , "y2" ) . WithLocation ( 17 , 22 ) ,
1993
- // (18 ,27): error CS8780: A variable may not be declared within a 'not' or 'or' pattern.
1993
+ Diagnostic ( ErrorCode . ERR_DesignatorBeneathPatternCombinator , "y2" ) . WithLocation ( 20 , 22 ) ,
1994
+ // (21 ,27): error CS8780: A variable may not be declared within a 'not' or 'or' pattern.
1994
1995
// if (o is 1 or int y3) { }
1995
- Diagnostic ( ErrorCode . ERR_DesignatorBeneathPatternCombinator , "y3" ) . WithLocation ( 18 , 27 ) ,
1996
- // (19 ,34): error CS8780: A variable may not be declared within a 'not' or 'or' pattern.
1996
+ Diagnostic ( ErrorCode . ERR_DesignatorBeneathPatternCombinator , "y3" ) . WithLocation ( 21 , 27 ) ,
1997
+ // (22 ,34): error CS8780: A variable may not be declared within a 'not' or 'or' pattern.
1997
1998
// if (o is (1 or 2) or int y4) { }
1998
- Diagnostic ( ErrorCode . ERR_DesignatorBeneathPatternCombinator , "y4" ) . WithLocation ( 19 , 34 ) ,
1999
- // (20,26): error CS8780: A variable may not be declared within a 'not' or 'or' pattern.
2000
- // if (o is not int y5) { }
2001
- Diagnostic ( ErrorCode . ERR_DesignatorBeneathPatternCombinator , "y5" ) . WithLocation ( 20 , 26 ) ,
2002
- // (21,33): error CS8780: A variable may not be declared within a 'not' or 'or' pattern.
2003
- // if (o is not (1 and int y6)) { }
2004
- Diagnostic ( ErrorCode . ERR_DesignatorBeneathPatternCombinator , "y6" ) . WithLocation ( 21 , 33 ) ,
2005
- // (22,33): error CS8780: A variable may not be declared within a 'not' or 'or' pattern.
2006
- // if (o is Point { X: var y7 } or Animal _) { }
2007
- Diagnostic ( ErrorCode . ERR_DesignatorBeneathPatternCombinator , "y7" ) . WithLocation ( 22 , 33 ) ,
2008
- // (23,28): error CS8780: A variable may not be declared within a 'not' or 'or' pattern.
2009
- // if (o is Point(var y8, _) or Animal _) { }
2010
- Diagnostic ( ErrorCode . ERR_DesignatorBeneathPatternCombinator , "y8" ) . WithLocation ( 23 , 28 ) ,
2011
- // (24,13): warning CS8794: An expression of type 'object' always matches the provided pattern.
2012
- // if (o is object or (1 or var y9)) { }
2013
- Diagnostic ( ErrorCode . WRN_IsPatternAlways , "o is object or (1 or var y9)" ) . WithArguments ( "object" ) . WithLocation ( 24 , 13 ) ,
2014
- // (24,38): error CS8780: A variable may not be declared within a 'not' or 'or' pattern.
2015
- // if (o is object or (1 or var y9)) { }
2016
- Diagnostic ( ErrorCode . ERR_DesignatorBeneathPatternCombinator , "y9" ) . WithLocation ( 24 , 38 )
1999
+ Diagnostic ( ErrorCode . ERR_DesignatorBeneathPatternCombinator , "y4" ) . WithLocation ( 22 , 34 ) ,
2000
+ // (23,33): error CS8780: A variable may not be declared within a 'not' or 'or' pattern.
2001
+ // if (o is Point { X: var y5 } or Animal _) { }
2002
+ Diagnostic ( ErrorCode . ERR_DesignatorBeneathPatternCombinator , "y5" ) . WithLocation ( 23 , 33 ) ,
2003
+ // (24,28): error CS8780: A variable may not be declared within a 'not' or 'or' pattern.
2004
+ // if (o is Point(var y6, _) or Animal _) { }
2005
+ Diagnostic ( ErrorCode . ERR_DesignatorBeneathPatternCombinator , "y6" ) . WithLocation ( 24 , 28 ) ,
2006
+ // (25,13): warning CS8794: An expression of type 'object' always matches the provided pattern.
2007
+ // if (o is object or (1 or var y7)) { }
2008
+ Diagnostic ( ErrorCode . WRN_IsPatternAlways , "o is object or (1 or var y7)" ) . WithArguments ( "object" ) . WithLocation ( 25 , 13 ) ,
2009
+ // (25,38): error CS8780: A variable may not be declared within a 'not' or 'or' pattern.
2010
+ // if (o is object or (1 or var y7)) { }
2011
+ Diagnostic ( ErrorCode . ERR_DesignatorBeneathPatternCombinator , "y7" ) . WithLocation ( 25 , 38 )
2017
2012
) ;
2018
2013
}
2019
2014
@@ -5641,5 +5636,203 @@ public void M() {
5641
5636
Diagnostic ( ErrorCode . ERR_IntDivByZero , "0/0" ) . WithLocation ( 4 , 22 )
5642
5637
) ;
5643
5638
}
5639
+
5640
+ [ Fact ]
5641
+ public void IsNot_01 ( )
5642
+ {
5643
+ var source =
5644
+ @"using System;
5645
+ class C
5646
+ {
5647
+ static void Main()
5648
+ {
5649
+ object o = ""s"";
5650
+ if (o is not string s) return;
5651
+ Console.WriteLine(s);
5652
+ }
5653
+ }" ;
5654
+ string expectedOutput = "s" ;
5655
+ var compilation = CreateCompilation ( source , options : TestOptions . ReleaseExe , parseOptions : TestOptions . RegularWithPatternCombinators ) ;
5656
+ compilation . VerifyDiagnostics (
5657
+ ) ;
5658
+ var compVerifier = CompileAndVerify ( compilation , expectedOutput : expectedOutput ) ;
5659
+ }
5660
+
5661
+ [ Fact ]
5662
+ public void IsNot_02 ( )
5663
+ {
5664
+ var source =
5665
+ @"using System;
5666
+ class C
5667
+ {
5668
+ static void Main()
5669
+ {
5670
+ object o = ""s"";
5671
+ if (o is (not (string s))) return;
5672
+ Console.WriteLine(s);
5673
+ }
5674
+ }" ;
5675
+ string expectedOutput = "s" ;
5676
+ var compilation = CreateCompilation ( source , options : TestOptions . ReleaseExe , parseOptions : TestOptions . RegularWithPatternCombinators ) ;
5677
+ compilation . VerifyDiagnostics (
5678
+ ) ;
5679
+ var compVerifier = CompileAndVerify ( compilation , expectedOutput : expectedOutput ) ;
5680
+ }
5681
+
5682
+ [ Fact ]
5683
+ public void IsNot_03 ( )
5684
+ {
5685
+ var source =
5686
+ @"class C
5687
+ {
5688
+ static void Main()
5689
+ {
5690
+ object o = ""s"";
5691
+ {
5692
+ if (o is string s)
5693
+ _ = s;
5694
+ else
5695
+ _ = s; // 1
5696
+ }
5697
+ {
5698
+ if (o is not string s)
5699
+ _ = s; // 2
5700
+ else
5701
+ _ = s;
5702
+ }
5703
+ {
5704
+ if (o is not not string s)
5705
+ _ = s;
5706
+ else
5707
+ _ = s; // 3
5708
+ }
5709
+ {
5710
+ if (o is not not not string s)
5711
+ _ = s; // 4
5712
+ else
5713
+ _ = s;
5714
+ }
5715
+ }
5716
+ }" ;
5717
+ var compilation = CreateCompilation ( source , options : TestOptions . ReleaseExe , parseOptions : TestOptions . RegularWithPatternCombinators ) ;
5718
+ compilation . VerifyDiagnostics (
5719
+ // (10,21): error CS0165: Use of unassigned local variable 's'
5720
+ // _ = s; // 1
5721
+ Diagnostic ( ErrorCode . ERR_UseDefViolation , "s" ) . WithArguments ( "s" ) . WithLocation ( 10 , 21 ) ,
5722
+ // (14,21): error CS0165: Use of unassigned local variable 's'
5723
+ // _ = s; // 2
5724
+ Diagnostic ( ErrorCode . ERR_UseDefViolation , "s" ) . WithArguments ( "s" ) . WithLocation ( 14 , 21 ) ,
5725
+ // (22,21): error CS0165: Use of unassigned local variable 's'
5726
+ // _ = s; // 3
5727
+ Diagnostic ( ErrorCode . ERR_UseDefViolation , "s" ) . WithArguments ( "s" ) . WithLocation ( 22 , 21 ) ,
5728
+ // (26,21): error CS0165: Use of unassigned local variable 's'
5729
+ // _ = s; // 4
5730
+ Diagnostic ( ErrorCode . ERR_UseDefViolation , "s" ) . WithArguments ( "s" ) . WithLocation ( 26 , 21 )
5731
+ ) ;
5732
+ }
5733
+
5734
+ [ Fact ]
5735
+ public void IsNot_04 ( )
5736
+ {
5737
+ var source =
5738
+ @"class C
5739
+ {
5740
+ static void Main()
5741
+ {
5742
+ object o = ""s"";
5743
+ {
5744
+ if (o is (string s))
5745
+ _ = s;
5746
+ else
5747
+ _ = s; // 1
5748
+ }
5749
+ {
5750
+ if (o is (not (string s)))
5751
+ _ = s; // 2
5752
+ else
5753
+ _ = s;
5754
+ }
5755
+ {
5756
+ if (o is (not (not (string s))))
5757
+ _ = s;
5758
+ else
5759
+ _ = s; // 3
5760
+ }
5761
+ {
5762
+ if (o is (not (not (not (string s)))))
5763
+ _ = s; // 4
5764
+ else
5765
+ _ = s;
5766
+ }
5767
+ }
5768
+ }" ;
5769
+ var compilation = CreateCompilation ( source , options : TestOptions . ReleaseExe , parseOptions : TestOptions . RegularWithPatternCombinators ) ;
5770
+ compilation . VerifyDiagnostics (
5771
+ // (10,21): error CS0165: Use of unassigned local variable 's'
5772
+ // _ = s; // 1
5773
+ Diagnostic ( ErrorCode . ERR_UseDefViolation , "s" ) . WithArguments ( "s" ) . WithLocation ( 10 , 21 ) ,
5774
+ // (14,21): error CS0165: Use of unassigned local variable 's'
5775
+ // _ = s; // 2
5776
+ Diagnostic ( ErrorCode . ERR_UseDefViolation , "s" ) . WithArguments ( "s" ) . WithLocation ( 14 , 21 ) ,
5777
+ // (22,21): error CS0165: Use of unassigned local variable 's'
5778
+ // _ = s; // 3
5779
+ Diagnostic ( ErrorCode . ERR_UseDefViolation , "s" ) . WithArguments ( "s" ) . WithLocation ( 22 , 21 ) ,
5780
+ // (26,21): error CS0165: Use of unassigned local variable 's'
5781
+ // _ = s; // 4
5782
+ Diagnostic ( ErrorCode . ERR_UseDefViolation , "s" ) . WithArguments ( "s" ) . WithLocation ( 26 , 21 )
5783
+ ) ;
5784
+ }
5785
+
5786
+ [ Fact ]
5787
+ public void IsNot_05 ( )
5788
+ {
5789
+ var source =
5790
+ @"class C
5791
+ {
5792
+ static void Main()
5793
+ {
5794
+ (object, object) o = (1, 2);
5795
+ {
5796
+ if (o is (1, string s))
5797
+ _ = s;
5798
+ else
5799
+ _ = s; // 1
5800
+ }
5801
+ {
5802
+ if (o is (not (1, string s)))
5803
+ _ = s; // 2
5804
+ else
5805
+ _ = s;
5806
+ }
5807
+ {
5808
+ if (o is (not (not (1, string s))))
5809
+ _ = s;
5810
+ else
5811
+ _ = s; // 3
5812
+ }
5813
+ {
5814
+ if (o is (not (not (not (1, string s)))))
5815
+ _ = s; // 4
5816
+ else
5817
+ _ = s;
5818
+ }
5819
+ }
5820
+ }" ;
5821
+ var compilation = CreateCompilation ( source , options : TestOptions . ReleaseExe , parseOptions : TestOptions . RegularWithPatternCombinators ) ;
5822
+ compilation . VerifyDiagnostics (
5823
+ // (10,21): error CS0165: Use of unassigned local variable 's'
5824
+ // _ = s; // 1
5825
+ Diagnostic ( ErrorCode . ERR_UseDefViolation , "s" ) . WithArguments ( "s" ) . WithLocation ( 10 , 21 ) ,
5826
+ // (14,21): error CS0165: Use of unassigned local variable 's'
5827
+ // _ = s; // 2
5828
+ Diagnostic ( ErrorCode . ERR_UseDefViolation , "s" ) . WithArguments ( "s" ) . WithLocation ( 14 , 21 ) ,
5829
+ // (22,21): error CS0165: Use of unassigned local variable 's'
5830
+ // _ = s; // 3
5831
+ Diagnostic ( ErrorCode . ERR_UseDefViolation , "s" ) . WithArguments ( "s" ) . WithLocation ( 22 , 21 ) ,
5832
+ // (26,21): error CS0165: Use of unassigned local variable 's'
5833
+ // _ = s; // 4
5834
+ Diagnostic ( ErrorCode . ERR_UseDefViolation , "s" ) . WithArguments ( "s" ) . WithLocation ( 26 , 21 )
5835
+ ) ;
5836
+ }
5644
5837
}
5645
5838
}
0 commit comments