Skip to content

Commit

Permalink
More tests from feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
jcouv committed Feb 14, 2017
1 parent 89b5d07 commit 28dba9e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/Compilers/CSharp/Portable/Binder/ForEachLoopBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -394,12 +394,13 @@ private bool GetEnumeratorInfo(ref ForEachEnumeratorInfo.Builder builder, BoundE
{
// Spec seems to refer to null literals, but Dev10 reports anything known to be null.
diagnostics.Add(ErrorCode.ERR_NullNotValid, _syntax.Expression.Location);
return false;
}
else if (collectionExpr.ConstantValue.IsDefaultLiteral)
{
diagnostics.Add(ErrorCode.ERR_DefaultNotValid, _syntax.Expression.Location);
return false;
}
return false;
}

if ((object)collectionExprType == null) // There's no way to enumerate something without a type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ static void M<T>()
{
const T x = default(T);
const T y = (T)default;
System.Console.Write($""{x} {y}"");
const object z = (T)default;
System.Console.Write($""{x} {y} {z}"");
}
}
";
Expand All @@ -262,7 +263,10 @@ static void M<T>()
Diagnostic(ErrorCode.ERR_BadConstType, "T").WithArguments("T").WithLocation(6, 15),
// (7,15): error CS0283: The type 'T' cannot be declared const
// const T y = (T)default;
Diagnostic(ErrorCode.ERR_BadConstType, "T").WithArguments("T").WithLocation(7, 15)
Diagnostic(ErrorCode.ERR_BadConstType, "T").WithArguments("T").WithLocation(7, 15),
// (8,26): error CS0134: 'z' is of type 'object'. A const field of a reference type other than string can only be initialized with null.
// const object z = (T)default;
Diagnostic(ErrorCode.ERR_NotNullConstRefField, "(T)default").WithArguments("z", "object").WithLocation(8, 26)
);
}

Expand All @@ -277,18 +281,22 @@ static void M()
{
const S x = default(S);
const S y = (S)default;
System.Console.Write($""{x} {y}"");
const object z = (S)default;
System.Console.Write($""{x} {y} {z}"");
}
}
";
var comp = CreateCompilationWithMscorlib(source, parseOptions: TestOptions.ExperimentalParseOptions);
comp.VerifyDiagnostics(
// (7,15): error CS0283: The type 'S' cannot be declared const
// (7,15): error CS0283: The type 'S' cannot be declared const
// const S x = default(S);
Diagnostic(ErrorCode.ERR_BadConstType, "S").WithArguments("S").WithLocation(7, 15),
// (8,15): error CS0283: The type 'S' cannot be declared const
// const S y = (S)default;
Diagnostic(ErrorCode.ERR_BadConstType, "S").WithArguments("S").WithLocation(8, 15)
Diagnostic(ErrorCode.ERR_BadConstType, "S").WithArguments("S").WithLocation(8, 15),
// (9,26): error CS0134: 'z' is of type 'object'. A const field of a reference type other than string can only be initialized with null.
// const object z = (S)default;
Diagnostic(ErrorCode.ERR_NotNullConstRefField, "(S)default").WithArguments("z", "object").WithLocation(9, 26)
);
}

Expand Down Expand Up @@ -425,13 +433,18 @@ static int M()
public void YieldReturn()
{
string source = @"
using System.Collections;
using System.Collections.Generic;
class C
{
static IEnumerable<int> M()
{
yield return default;
}
static IEnumerable M2()
{
yield return default;
}
}
";
var comp = CreateCompilationWithMscorlib(source, parseOptions: TestOptions.ExperimentalParseOptions);
Expand Down Expand Up @@ -682,7 +695,7 @@ static void Main()
}

[Fact]
public void CS0403ERR_TypeVarCanBeDefault()
public void TypeVarCanBeDefault()
{
var source =
@"interface I { }
Expand Down

0 comments on commit 28dba9e

Please sign in to comment.