From defaa1b691b34e7338576afabaad60eb61a3b4ea Mon Sep 17 00:00:00 2001 From: Neal Gafter Date: Sun, 14 Jun 2015 13:27:10 -0700 Subject: [PATCH] Regularize how we handle "impossible" conditions in internal APIs. In a switch's default clause that is unexpected, for example, we should throw ExceptionUtilities.UnexpectedValue(value); Long ago this was a standard but we've drifted from it. Closes #3495 --- docs/compilers/Design/README.md | 4 + .../compilers/Design/Unexpected Conditions.md | 9 + .../BinderFactory.BinderFactoryVisitor.cs | 3 +- .../Portable/Binder/Binder_Constraints.cs | 3 +- .../Portable/Binder/Binder_Conversions.cs | 33 ++-- .../CSharp/Portable/Binder/Binder_Crefs.cs | 9 +- .../Portable/Binder/Binder_Expressions.cs | 6 +- .../Binder/Binder_InterpolatedString.cs | 2 +- .../Portable/Binder/Binder_Invocation.cs | 157 ------------------ .../Portable/Binder/Binder_Operators.cs | 17 +- .../Portable/Binder/Binder_QueryErrors.cs | 4 +- .../Portable/Binder/Binder_Statements.cs | 10 +- .../Portable/Binder/LookupResultKind.cs | 4 +- .../Semantics/Conversions/Conversion.cs | 3 +- .../Conversions/ConversionKindExtensions.cs | 4 +- .../Semantics/Conversions/ConversionsBase.cs | 4 +- .../UserDefinedImplicitConversions.cs | 2 +- .../Semantics/Operators/OperatorFacts.cs | 7 +- .../Operators/OperatorKindExtensions.cs | 6 +- .../MemberAnalysisResult.cs | 3 +- .../Binder/WithCrefTypeParametersBinder.cs | 6 +- .../BoundTree/BoundStatementExtensions.cs | 7 +- .../Compilation/CSharpSemanticModel.cs | 3 +- .../Compilation/InitializerSemanticModel.cs | 3 +- .../Compilation/SyntaxTreeSemanticModel.cs | 9 +- .../Compiler/DocumentationCommentCompiler.cs | 4 +- .../Portable/Emitter/Model/PEModuleBuilder.cs | 7 +- .../Portable/FlowAnalysis/ExitPointsWalker.cs | 3 +- .../FlowAnalysis/PreciseAbstractFlowPass.cs | 3 +- .../Portable/Lowering/InitializerRewriter.cs | 2 +- .../LocalRewriter_BinaryOperator.cs | 3 +- .../LocalRewriter/LocalRewriter_Call.cs | 4 +- .../LocalRewriter_ConditionalAccess.cs | 2 +- .../LocalRewriter_SequencePoints.cs | 2 +- .../LocalRewriter_UnaryOperator.cs | 6 +- .../IteratorAndAsyncCaptureWalker.cs | 3 +- .../CSharp/Portable/Parser/LanguageParser.cs | 3 +- src/Compilers/CSharp/Portable/Parser/Lexer.cs | 5 +- .../Symbols/MemberSignatureComparer.cs | 4 +- .../Symbols/MemberSymbolExtensions.cs | 29 +--- .../Symbols/Metadata/PE/PEFieldSymbol.cs | 3 +- .../Symbols/Metadata/PE/PENamedTypeSymbol.cs | 3 +- .../CSharp/Portable/Symbols/MethodSymbol.cs | 3 +- .../Symbols/MethodSymbolExtensions.cs | 4 +- .../OverriddenOrHiddenMembersHelpers.cs | 10 +- .../Symbols/Source/SourceAssemblySymbol.cs | 3 +- .../Symbols/Source/SourceEventSymbol.cs | 3 +- ...berContainerSymbol_ImplementationChecks.cs | 7 +- .../Portable/Symbols/SymbolDistinguisher.cs | 2 - .../Portable/Symbols/SymbolExtensions.cs | 3 +- .../CSharp/Portable/Symbols/TypeSymbol.cs | 7 +- .../CSharp/Portable/Symbols/VarianceSafety.cs | 7 +- .../Portable/Syntax/CSharpSyntaxTree.cs | 2 +- .../CSharp/Portable/Syntax/SyntaxFacts.cs | 4 +- .../CodeGen/SwitchIntegralJumpTableEmitter.cs | 3 +- .../Portable/Diagnostic/DiagnosticInfo.cs | 2 +- .../Emit/EditAndContinue/SymbolChanges.cs | 10 +- .../Core/Portable/MetadataReader/PEModule.cs | 16 +- .../Core/Portable/RuleSet/RuleSetProcessor.cs | 5 +- .../Analysis/FlowAnalysis/AbstractFlowPass.vb | 2 +- .../Analysis/FlowAnalysis/DataFlowPass.vb | 2 +- .../Analysis/FlowAnalysis/ExitPointsWalker.vb | 2 +- .../Analysis/FlowAnalysis/ReadWriteWalker.vb | 6 +- .../Portable/Binding/Binder_Expressions.vb | 6 +- .../Portable/Binding/Binder_Lambda.vb | 6 +- .../Portable/Compilation/SemanticModel.vb | 2 +- .../Compilation/SyntaxTreeSemanticModel.vb | 2 +- .../LocalRewriter_BinaryOperators.vb | 2 +- .../LocalRewriter/LocalRewriter_Conversion.vb | 3 +- .../Portable/Semantics/TypeInference/Graph.vb | 2 +- .../TypeInference/TypeArgumentInference.vb | 4 +- ...EmbeddedSymbolManager.SymbolsCollection.vb | 2 +- .../Portable/Symbols/MergedNamespaceSymbol.vb | 4 +- .../Source/SourceMemberContainerTypeSymbol.vb | 6 +- .../Source/SourceMemberMethodSymbol.vb | 2 +- .../EditAndContinue/SyntaxUtilities.vb | 2 +- 76 files changed, 155 insertions(+), 395 deletions(-) create mode 100644 docs/compilers/Design/README.md create mode 100644 docs/compilers/Design/Unexpected Conditions.md diff --git a/docs/compilers/Design/README.md b/docs/compilers/Design/README.md new file mode 100644 index 0000000000000..9f6a01a1bfd33 --- /dev/null +++ b/docs/compilers/Design/README.md @@ -0,0 +1,4 @@ +Design Docs +=========== + +This directory is a place for design documents for the Roslyn compilers. diff --git a/docs/compilers/Design/Unexpected Conditions.md b/docs/compilers/Design/Unexpected Conditions.md new file mode 100644 index 0000000000000..1d6a203fd1375 --- /dev/null +++ b/docs/compilers/Design/Unexpected Conditions.md @@ -0,0 +1,9 @@ +These are [guidelines](https://www.youtube.com/watch?v=6GMkuPiIZ2k) for writing code that may encounter "impossible" and unexpected program conditions. Like other *coding guidelines*, there is value in being somewhat uniform across the code base even though we may have opinions that differ. When you have a good reason to vary from these guidelines, please add comments that explain the variance for those who come after you. + +- Use `Debug.Assert(Condition)` to document invariants in the code. Although these dissolve away into nothing in non-Debug builds, as an open-source project we have little control over whether our customers are running our code with Debug enabled. Therefore such assertions should not consume excessive execution time. We may consider having such assertions run in production builds in the future. If we find they are too expensive we may create work items to improve their performance. +- If you write a switch statement that is intended to be exhaustive of the possibilities, add a default branch with `throw ExceptionUtilities.UnexpectedValue(switchExpression);`. +- Similarly, if you have a sequence of `if-then-else if` statements that is intended to be exhaustive of the possibilities, add a final "impossible" else branch with `throw ExceptionUtilities.Unreachable;` or, if is convenient to provide interesting, easy to obtain data for the diagnostic, use `ExceptionUtilities.UnexpectedValue`. Do this also for other situations where the code reaches a point that is "impossible". +- Validation of preconditions in public APIs should be done with plain code. Report a diagnostic if one exists for the situation (e.g. a syntax error), or throw an appropriate exception such as `ArgumentNullException` or `ArgumentException`. +- If you run into some other weird error case that would be fatal, throw `InvalidOperationException` with interesting, straightforward to get, data in the message. These should be rare and should be accompanied by a comment explaining why an `Assert` is not sufficient. + +By following these guidelines, you should find no reason to write `Debug.Assert(false)`. If you find that in your code consider if one of these bullets suggests a different way to handle the situation. diff --git a/src/Compilers/CSharp/Portable/Binder/BinderFactory.BinderFactoryVisitor.cs b/src/Compilers/CSharp/Portable/Binder/BinderFactory.BinderFactoryVisitor.cs index 32f54b4db1d5c..855e321067ac9 100644 --- a/src/Compilers/CSharp/Portable/Binder/BinderFactory.BinderFactoryVisitor.cs +++ b/src/Compilers/CSharp/Portable/Binder/BinderFactory.BinderFactoryVisitor.cs @@ -262,8 +262,7 @@ public override Binder VisitAccessorDeclaration(AccessorDeclarationSyntax parent break; } default: - Debug.Assert(false, "Accessor unexpectedly attached to " + propertyOrEventDecl.Kind()); - break; + throw ExceptionUtilities.UnexpectedValue(propertyOrEventDecl.Kind()); } if ((object)accessor != null) diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Constraints.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Constraints.cs index bc0ad92c57bd6..a7611e41d9e4e 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Constraints.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Constraints.cs @@ -219,8 +219,7 @@ private static bool IsValidConstraintType(TypeConstraintSyntax syntax, TypeSymbo // script class is synthetized, never used as a constraint default: - Debug.Assert(false, "Unexpected type kind: " + type.TypeKind); - return false; + throw ExceptionUtilities.UnexpectedValue(type.TypeKind); } if (type.ContainsDynamic()) diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Conversions.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Conversions.cs index 84c16bed2a6a3..32fd42268b53e 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Conversions.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Conversions.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Diagnostics; using Microsoft.CodeAnalysis.CSharp.Symbols; +using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.CSharp { @@ -890,8 +891,8 @@ private static object DoUncheckedConversion(SpecialType destinationType, Constan case SpecialType.System_Single: case SpecialType.System_Double: return (double)byteValue; case SpecialType.System_Decimal: return (decimal)byteValue; + default: throw ExceptionUtilities.UnexpectedValue(destinationType); } - break; case ConstantValueTypeDiscriminator.Char: char charValue = value.CharValue; switch (destinationType) @@ -908,8 +909,8 @@ private static object DoUncheckedConversion(SpecialType destinationType, Constan case SpecialType.System_Single: case SpecialType.System_Double: return (double)charValue; case SpecialType.System_Decimal: return (decimal)charValue; + default: throw ExceptionUtilities.UnexpectedValue(destinationType); } - break; case ConstantValueTypeDiscriminator.UInt16: ushort uint16Value = value.UInt16Value; switch (destinationType) @@ -926,8 +927,8 @@ private static object DoUncheckedConversion(SpecialType destinationType, Constan case SpecialType.System_Single: case SpecialType.System_Double: return (double)uint16Value; case SpecialType.System_Decimal: return (decimal)uint16Value; + default: throw ExceptionUtilities.UnexpectedValue(destinationType); } - break; case ConstantValueTypeDiscriminator.UInt32: uint uint32Value = value.UInt32Value; switch (destinationType) @@ -944,8 +945,8 @@ private static object DoUncheckedConversion(SpecialType destinationType, Constan case SpecialType.System_Single: return (double)(float)uint32Value; case SpecialType.System_Double: return (double)uint32Value; case SpecialType.System_Decimal: return (decimal)uint32Value; + default: throw ExceptionUtilities.UnexpectedValue(destinationType); } - break; case ConstantValueTypeDiscriminator.UInt64: ulong uint64Value = value.UInt64Value; switch (destinationType) @@ -962,8 +963,8 @@ private static object DoUncheckedConversion(SpecialType destinationType, Constan case SpecialType.System_Single: return (double)(float)uint64Value; case SpecialType.System_Double: return (double)uint64Value; case SpecialType.System_Decimal: return (decimal)uint64Value; + default: throw ExceptionUtilities.UnexpectedValue(destinationType); } - break; case ConstantValueTypeDiscriminator.SByte: sbyte sbyteValue = value.SByteValue; switch (destinationType) @@ -980,8 +981,8 @@ private static object DoUncheckedConversion(SpecialType destinationType, Constan case SpecialType.System_Single: case SpecialType.System_Double: return (double)sbyteValue; case SpecialType.System_Decimal: return (decimal)sbyteValue; + default: throw ExceptionUtilities.UnexpectedValue(destinationType); } - break; case ConstantValueTypeDiscriminator.Int16: short int16Value = value.Int16Value; switch (destinationType) @@ -998,8 +999,8 @@ private static object DoUncheckedConversion(SpecialType destinationType, Constan case SpecialType.System_Single: case SpecialType.System_Double: return (double)int16Value; case SpecialType.System_Decimal: return (decimal)int16Value; + default: throw ExceptionUtilities.UnexpectedValue(destinationType); } - break; case ConstantValueTypeDiscriminator.Int32: int int32Value = value.Int32Value; switch (destinationType) @@ -1016,8 +1017,8 @@ private static object DoUncheckedConversion(SpecialType destinationType, Constan case SpecialType.System_Single: return (double)(float)int32Value; case SpecialType.System_Double: return (double)int32Value; case SpecialType.System_Decimal: return (decimal)int32Value; + default: throw ExceptionUtilities.UnexpectedValue(destinationType); } - break; case ConstantValueTypeDiscriminator.Int64: long int64Value = value.Int64Value; switch (destinationType) @@ -1034,8 +1035,8 @@ private static object DoUncheckedConversion(SpecialType destinationType, Constan case SpecialType.System_Single: return (double)(float)int64Value; case SpecialType.System_Double: return (double)int64Value; case SpecialType.System_Decimal: return (decimal)int64Value; + default: throw ExceptionUtilities.UnexpectedValue(destinationType); } - break; case ConstantValueTypeDiscriminator.Single: case ConstantValueTypeDiscriminator.Double: // This code used to invoke CheckConstantBounds and return constant zero if the value is not within the target type. @@ -1060,8 +1061,8 @@ private static object DoUncheckedConversion(SpecialType destinationType, Constan case SpecialType.System_Single: return (double)(float)doubleValue; case SpecialType.System_Double: return (double)doubleValue; case SpecialType.System_Decimal: return (value.Discriminator == ConstantValueTypeDiscriminator.Single) ? (decimal)(float)doubleValue : (decimal)doubleValue; + default: throw ExceptionUtilities.UnexpectedValue(destinationType); } - break; case ConstantValueTypeDiscriminator.Decimal: decimal decimalValue = CheckConstantBounds(destinationType, value.DecimalValue) ? value.DecimalValue : 0m; switch (destinationType) @@ -1078,13 +1079,15 @@ private static object DoUncheckedConversion(SpecialType destinationType, Constan case SpecialType.System_Single: return (double)(float)decimalValue; case SpecialType.System_Double: return (double)decimalValue; case SpecialType.System_Decimal: return (decimal)decimalValue; + default: throw ExceptionUtilities.UnexpectedValue(destinationType); } - break; + default: + throw ExceptionUtilities.UnexpectedValue(value.Discriminator); } } - Debug.Assert(false, "Unexpected case in constant folding"); - return value.Value; + // all cases should have been handled in the switch above. + // return value.Value; } public static bool CheckConstantBounds(SpecialType destinationType, ConstantValue value) @@ -1163,10 +1166,10 @@ private static object CanonicalizeConstant(ConstantValue value) case ConstantValueTypeDiscriminator.Single: case ConstantValueTypeDiscriminator.Double: return value.DoubleValue; case ConstantValueTypeDiscriminator.Decimal: return value.DecimalValue; + default: throw ExceptionUtilities.UnexpectedValue(value.Discriminator); } - Debug.Assert(false, "unexpected constant in CanonicalizeConstant"); - return value.Value; + // all cases handled in the switch, above. } } } diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Crefs.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Crefs.cs index 0a72245ad5d0d..4a7605b6e8aac 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Crefs.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Crefs.cs @@ -34,9 +34,7 @@ private ImmutableArray BindCrefInternal(CrefSyntax syntax, out Symbol am case SyntaxKind.ConversionOperatorMemberCref: return BindMemberCref((MemberCrefSyntax)syntax, containerOpt: null, ambiguityWinner: out ambiguityWinner, diagnostics: diagnostics); default: - Debug.Assert(false, "Unexpected cref kind " + syntax.Kind()); - ambiguityWinner = null; - return ImmutableArray.Empty; + throw ExceptionUtilities.UnexpectedValue(syntax.Kind()); } } @@ -127,10 +125,7 @@ private ImmutableArray BindMemberCref(MemberCrefSyntax syntax, Namespace result = BindConversionOperatorMemberCref((ConversionOperatorMemberCrefSyntax)syntax, containerOpt, out ambiguityWinner, diagnostics); break; default: - Debug.Assert(false, "Unexpected member cref kind " + syntax.Kind()); - ambiguityWinner = null; - result = ImmutableArray.Empty; - break; + throw ExceptionUtilities.UnexpectedValue(syntax.Kind()); } if (!result.Any()) diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs index c57edc8574738..de4109a0c4169 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs @@ -1399,11 +1399,7 @@ public BoundExpression BindLabel(ExpressionSyntax node, DiagnosticBag diagnostic return BadExpression(node, result.Kind); } - if (!result.IsSingleViable) - { - Debug.Assert(false, "If this happens, we need to deal with multiple label definitions."); - } - + Debug.Assert(result.IsSingleViable, "If this happens, we need to deal with multiple label definitions."); var symbol = (LabelSymbol)result.Symbols.First(); result.Free(); return new BoundLabel(node, symbol, null); diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_InterpolatedString.cs b/src/Compilers/CSharp/Portable/Binder/Binder_InterpolatedString.cs index 34dad32181253..cf9e156c6ce62 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_InterpolatedString.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_InterpolatedString.cs @@ -87,7 +87,7 @@ private BoundExpression BindInterpolatedString(InterpolatedStringExpressionSynta continue; } default: - throw ExceptionUtilities.Unreachable; + throw ExceptionUtilities.UnexpectedValue(content.Kind()); } } diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Invocation.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Invocation.cs index c2a0ad6be484c..b764993aec876 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Invocation.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Invocation.cs @@ -1195,105 +1195,6 @@ private bool CheckSyntaxForNameofArgument(ExpressionSyntax argument, out string } } - - //private BoundExpression BindNameOf(NameOfExpressionSyntax node, DiagnosticBag diagnostics) - //{ - // var argument = node.Argument; - // if (InvocableNameofInScope()) - // { - // // If there is an invocable nameof symbol, bind the NameOfExpressionSyntax as a regular method invocation. - // return BindNameOfAsInvocation(node, diagnostics); - // } - // // We now bind it as a built-in nameof operator. - - // CheckFeatureAvailability(node.GetLocation(), MessageID.IDS_FeatureNameof, diagnostics); - - // // We divide the argument (TypeSyntax) into two pieces: left and right. - // // It makes easier (i) to filter out invalid nameof arguments (see CheckSyntaxErrorsForNameOf) and (ii) to lookup the symbols (see LookupForNameofArgument). - // ExpressionSyntax left, right; - // bool isAliasQualified = false; - - // switch (argument.Kind) - // { - // // nameof(identifier) - // case SyntaxKind.IdentifierName: - // left = null; - // right = argument; - // break; - - // // nameof(unbound-type-name . identifier) - // case SyntaxKind.QualifiedName: - // var qualifiedName = (QualifiedNameSyntax)argument; - // left = qualifiedName.Left; - // right = qualifiedName.Right; - // Debug.Assert(left.Kind == SyntaxKind.IdentifierName || left.Kind == SyntaxKind.QualifiedName || left.Kind == SyntaxKind.AliasQualifiedName || left.Kind == SyntaxKind.GenericName); - // break; - - // // nameof(identifier :: identifier) - // case SyntaxKind.AliasQualifiedName: - // var aliasQualifiedName = (AliasQualifiedNameSyntax)argument; - // left = aliasQualifiedName.Alias; - // right = aliasQualifiedName.Name; - // isAliasQualified = true; - // break; - - // default: - // left = null; - // right = argument; - // break; - // } - - // // We are still not sure that it is a valid nameof operator. - // // At this point, we only know that (i) nameof has one argument which is a kind of TypeSyntax - // // and (ii) there is no invocable nameof symbol. - // if (CheckSyntaxErrorsForNameOf(left, right, diagnostics)) - // { - // bool hasErrors; - // // CheckSyntaxErrorsForNameOf method guarantees that the rightmost part is a IdendifierNameSyntax - // Debug.Assert(right.Kind == SyntaxKind.IdentifierName); - // string rightmostIdentifier = ((IdentifierNameSyntax)right).Identifier.ValueText; - - // // We use TypeofBinder in order to resolve unbound generic names without any error. - // var typeofBinder = new TypeofBinder(argument, this); - // var symbols = typeofBinder.LookupForNameofArgument(left, (IdentifierNameSyntax)right, rightmostIdentifier, diagnostics, isAliasQualified, out hasErrors); - // return new BoundNameOfOperator(node, symbols, ConstantValue.Create(rightmostIdentifier), this.GetSpecialType(SpecialType.System_String, diagnostics, node), hasErrors: hasErrors); - // } - // else - // { - // return BadExpression(node); - // } - //} - - //private BoundExpression BindNameOfAsInvocation(NameOfExpressionSyntax node, DiagnosticBag diagnostics) - //{ - // var argument = node.Argument; - // var nameOfIdentifier = node.NameOfIdentifier; - // string nameofString = nameOfIdentifier.Identifier.ValueText; - // AnalyzedArguments analyzedArguments = AnalyzedArguments.GetInstance(); - // var boundArgument = this.BindValue(argument, diagnostics, BindValueKind.RValue); - // analyzedArguments.Arguments.Add(boundArgument); - // BoundExpression boundExpression = BindMethodGroup(nameOfIdentifier, invoked: true, indexed: false, diagnostics: diagnostics); - // boundExpression = CheckValue(boundExpression, BindValueKind.RValueOrMethodGroup, diagnostics); - // var result = BindInvocationExpression(node, nameOfIdentifier, nameofString, boundExpression, analyzedArguments, diagnostics); - // analyzedArguments.Free(); - // return result; - //} - - //protected bool CheckUsedBeforeDeclarationIfLocal(ArrayBuilder symbols, ExpressionSyntax node) - //{ - // if (symbols.Count > 0) - // { - // var localSymbol = symbols.First() as LocalSymbol; - // if ((object)localSymbol != null) - // { - // Location localSymbolLocation = localSymbol.Locations[0]; - // return node.SyntaxTree == localSymbolLocation.SourceTree && - // node.SpanStart < localSymbolLocation.SourceSpan.Start; - // } - // } - // return false; - //} - /// /// Helper method that checks whether there is an invocable 'nameof' in scope. /// @@ -1309,63 +1210,5 @@ private bool InvocableNameofInScope() return result; } - //private bool CheckSyntaxErrorsForNameOf(ExpressionSyntax left, ExpressionSyntax right, DiagnosticBag diagnostics) - //{ - // // Filter out the TypeSyntax nodes whose rightmost part is not an identifier such as nameof(int), nameof(Collections.List<>). - // if (right.Kind != SyntaxKind.IdentifierName) - // { - // Error(diagnostics, ErrorCode.ERR_IdentifierExpected, right); - // return false; - // } - // // If there is a left part, let's also filter out the cases such as nameof(List.Equals) - // // Specifying the type parameters is not allowed in the argument of the nameof operator. - // return left == null || CheckTypeParametersForNameOf((NameSyntax)left, diagnostics); - //} - - //private static bool CheckTypeParametersForNameOf(NameSyntax node, DiagnosticBag diagnostics) - //{ - // NameSyntax temp; - // // if we are analyzing nameof(a.b.c.d.e.f.g), this method will get the left part as an argument, which is 'a.b.c.d.e.f'. - // // the loop below visits the nodes in the 'a.b.c.d.e.f' in order from rightmost to leftmost. - // while (node != null) - // { - // switch (node.Kind) - // { - // case SyntaxKind.QualifiedName: - // temp = ((QualifiedNameSyntax)node).Right; - // node = ((QualifiedNameSyntax)node).Left; - // break; - // case SyntaxKind.AliasQualifiedName: - // temp = ((AliasQualifiedNameSyntax)node).Name; - // node = null; - // break; - // case SyntaxKind.GenericName: - // temp = node; - // node = null; - // break; - // default: - // return true; - // } - // // if the current node is a generic name, let's analyze the type parameters if they are omitted or not. - // if (temp.Kind == SyntaxKind.GenericName && !AreTypeParametersOmitted(((GenericNameSyntax)temp).TypeArgumentList, diagnostics)) - // { - // return false; - // } - // } - // return true; - //} - - //private static bool AreTypeParametersOmitted(TypeArgumentListSyntax list, DiagnosticBag diagnostics) - //{ - // foreach (var arg in list.Arguments) - // { - // if (arg.Kind != SyntaxKind.OmittedTypeArgument) - // { - // Error(diagnostics, ErrorCode.ERR_UnexpectedBoundGenericName, arg); - // return false; - // } - // } - // return true; - //} } } diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Operators.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Operators.cs index 095dec8ce81c8..91b91bb14b861 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Operators.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Operators.cs @@ -1250,8 +1250,7 @@ internal static TypeSymbol GetEnumType(BinaryOperatorKind kind, BoundExpression case BinaryOperatorKind.UnderlyingAndEnumSubtraction: return right.Type; default: - Debug.Assert(false, "Unexpected non-enum operation in GetEnumType"); - return null; + throw ExceptionUtilities.UnexpectedValue(kind); } } @@ -1272,8 +1271,7 @@ internal static SpecialType GetEnumPromotedType(SpecialType underlyingType) return underlyingType; default: - Debug.Assert(false, "Unexpected underlying enum type."); - return underlyingType; + throw ExceptionUtilities.UnexpectedValue(underlyingType); } } @@ -1337,8 +1335,7 @@ private ConstantValue FoldEnumBinaryOperator( break; default: - Debug.Assert(false, "Unexpected operator kind"); - break; + throw ExceptionUtilities.UnexpectedValue(newKind.Operator()); } var constantValue = FoldBinaryOperator(syntax, newKind, newLeftOperand, newRightOperand, operatorType, diagnostics); @@ -1806,10 +1803,8 @@ private static BinaryOperatorKind SyntaxKindToBinaryOperatorKind(SyntaxKind kind case SyntaxKind.ExclusiveOrExpression: return BinaryOperatorKind.Xor; case SyntaxKind.LogicalAndExpression: return BinaryOperatorKind.LogicalAnd; case SyntaxKind.LogicalOrExpression: return BinaryOperatorKind.LogicalOr; + default: throw ExceptionUtilities.UnexpectedValue(kind); } - - Debug.Assert(false, "Bad syntax kind in binary operator binding"); - return BinaryOperatorKind.Error; } private BoundExpression BindIncrementOperator(CSharpSyntaxNode node, ExpressionSyntax operandSyntax, SyntaxToken operatorToken, DiagnosticBag diagnostics) @@ -2375,10 +2370,8 @@ private static UnaryOperatorKind SyntaxKindToUnaryOperatorKind(SyntaxKind kind) case SyntaxKind.UnaryMinusExpression: return UnaryOperatorKind.UnaryMinus; case SyntaxKind.LogicalNotExpression: return UnaryOperatorKind.LogicalNegation; case SyntaxKind.BitwiseNotExpression: return UnaryOperatorKind.BitwiseComplement; + default: throw ExceptionUtilities.UnexpectedValue(kind); } - - Debug.Assert(false, "Bad syntax kind in unary operator binding"); - return UnaryOperatorKind.Error; } private static BindValueKind GetBinaryAssignmentKind(SyntaxKind kind) diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_QueryErrors.cs b/src/Compilers/CSharp/Portable/Binder/Binder_QueryErrors.cs index 996783a726764..692528e6c3ec4 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_QueryErrors.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_QueryErrors.cs @@ -196,9 +196,7 @@ internal static void ReportQueryInferenceFailed(CSharpSyntaxNode queryClause, st clauseKind = SyntaxFacts.GetText(SyntaxKind.FromKeyword); break; default: - clauseKind = "unknown"; - Debug.Assert(false, "invalid query clause kind " + queryClause.Kind()); - break; + throw ExceptionUtilities.UnexpectedValue(queryClause.Kind()); } diagnostics.Add(new DiagnosticInfoWithSymbols( diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs index 6786d4f4b07cb..6d7b292bba7b8 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs @@ -1015,8 +1015,7 @@ static private ErrorCode GetThisLvalueError(BindValueKind kind) switch (kind) { default: - Debug.Assert(false, "bad BindValueKind"); - goto case BindValueKind.Assignment; + throw ExceptionUtilities.UnexpectedValue(kind); case BindValueKind.CompoundAssignment: case BindValueKind.Assignment: return ErrorCode.ERR_AssgReadonlyLocal; @@ -1042,8 +1041,7 @@ private static ErrorCode GetRangeLvalueError(BindValueKind kind) case BindValueKind.AddressOf: return ErrorCode.ERR_InvalidAddrOp; default: - Debug.Assert(false, "bad BindValueKind"); - goto case BindValueKind.Assignment; + throw ExceptionUtilities.UnexpectedValue(kind); } } @@ -1600,9 +1598,7 @@ private static EventSymbol GetEventSymbol(BoundExpression expr, out BoundExpress eventSyntax = syntax; break; default: - Debug.Assert(false, "Unexpected syntax: " + syntax.Kind()); - eventSyntax = syntax; - break; + throw ExceptionUtilities.UnexpectedValue(syntax.Kind()); } BoundEventAccess eventAccess = (BoundEventAccess)expr; diff --git a/src/Compilers/CSharp/Portable/Binder/LookupResultKind.cs b/src/Compilers/CSharp/Portable/Binder/LookupResultKind.cs index b6f894495516c..b0dd29402b504 100644 --- a/src/Compilers/CSharp/Portable/Binder/LookupResultKind.cs +++ b/src/Compilers/CSharp/Portable/Binder/LookupResultKind.cs @@ -4,6 +4,7 @@ using Microsoft.CodeAnalysis.CSharp.Symbols; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Text; +using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.CSharp { @@ -90,8 +91,7 @@ public static CandidateReason ToCandidateReason(this LookupResultKind resultKind return CandidateReason.None; default: - Debug.Assert(false, "Unknown or unexpected LookupResultKind."); - return CandidateReason.NotReferencable; // most generic one. + throw ExceptionUtilities.UnexpectedValue(resultKind); } } diff --git a/src/Compilers/CSharp/Portable/Binder/Semantics/Conversions/Conversion.cs b/src/Compilers/CSharp/Portable/Binder/Semantics/Conversions/Conversion.cs index 111a6dd9eadec..c39686d7d68a4 100644 --- a/src/Compilers/CSharp/Portable/Binder/Semantics/Conversions/Conversion.cs +++ b/src/Compilers/CSharp/Portable/Binder/Semantics/Conversions/Conversion.cs @@ -462,8 +462,7 @@ internal LookupResultKind ResultKind return LookupResultKind.OverloadResolutionFailure; } default: - Debug.Assert(false, "Unknown UserDefinedConversionResultKind " + _conversionResult.Kind); - return LookupResultKind.OverloadResolutionFailure; + throw ExceptionUtilities.UnexpectedValue(_conversionResult.Kind); } } } diff --git a/src/Compilers/CSharp/Portable/Binder/Semantics/Conversions/ConversionKindExtensions.cs b/src/Compilers/CSharp/Portable/Binder/Semantics/Conversions/ConversionKindExtensions.cs index b8d1c5965d851..a51a036c0e6a4 100644 --- a/src/Compilers/CSharp/Portable/Binder/Semantics/Conversions/ConversionKindExtensions.cs +++ b/src/Compilers/CSharp/Portable/Binder/Semantics/Conversions/ConversionKindExtensions.cs @@ -4,6 +4,7 @@ using Microsoft.CodeAnalysis.CSharp.Symbols; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Text; +using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.CSharp { @@ -53,8 +54,7 @@ public static bool IsImplicitConversion(this ConversionKind conversionKind) return false; default: - Debug.Assert(false, "Unexpected conversion kind"); - return false; + throw ExceptionUtilities.UnexpectedValue(conversionKind); } } diff --git a/src/Compilers/CSharp/Portable/Binder/Semantics/Conversions/ConversionsBase.cs b/src/Compilers/CSharp/Portable/Binder/Semantics/Conversions/ConversionsBase.cs index dfc606787fa05..7299175966ee1 100644 --- a/src/Compilers/CSharp/Portable/Binder/Semantics/Conversions/ConversionsBase.cs +++ b/src/Compilers/CSharp/Portable/Binder/Semantics/Conversions/ConversionsBase.cs @@ -6,6 +6,7 @@ using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Text; using System.Collections.Generic; +using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.CSharp { @@ -281,8 +282,7 @@ public Conversion ClassifyStandardConversion(BoundExpression sourceExpression, T return Conversion.PointerToPointer; default: - Debug.Assert(false, "Unexpected conversion kind returned by ClassifyStandardImplicitConversion"); - return Conversion.NoConversion; + throw ExceptionUtilities.UnexpectedValue(conversion.Kind); } } diff --git a/src/Compilers/CSharp/Portable/Binder/Semantics/Conversions/UserDefinedImplicitConversions.cs b/src/Compilers/CSharp/Portable/Binder/Semantics/Conversions/UserDefinedImplicitConversions.cs index 30902e023928a..cbaa31ad6f360 100644 --- a/src/Compilers/CSharp/Portable/Binder/Semantics/Conversions/UserDefinedImplicitConversions.cs +++ b/src/Compilers/CSharp/Portable/Binder/Semantics/Conversions/UserDefinedImplicitConversions.cs @@ -596,7 +596,7 @@ private static bool IsEncompassingImplicitConversionKind(ConversionKind kind) return true; default: - throw ExceptionUtilities.Unreachable; + throw ExceptionUtilities.UnexpectedValue(kind); } } diff --git a/src/Compilers/CSharp/Portable/Binder/Semantics/Operators/OperatorFacts.cs b/src/Compilers/CSharp/Portable/Binder/Semantics/Operators/OperatorFacts.cs index 4dfc6d6c79539..1b16bed698fa0 100644 --- a/src/Compilers/CSharp/Portable/Binder/Semantics/Operators/OperatorFacts.cs +++ b/src/Compilers/CSharp/Portable/Binder/Semantics/Operators/OperatorFacts.cs @@ -4,6 +4,7 @@ using Microsoft.CodeAnalysis.CSharp.Symbols; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Text; +using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.CSharp { @@ -151,8 +152,7 @@ public static string UnaryOperatorNameFromOperatorKind(UnaryOperatorKind kind) case UnaryOperatorKind.True: return WellKnownMemberNames.TrueOperatorName; case UnaryOperatorKind.False: return WellKnownMemberNames.FalseOperatorName; default: - Debug.Assert(false, "Unexpected postfix operator kind for user-defined unary operator"); - return WellKnownMemberNames.UnaryPlusOperatorName; + throw ExceptionUtilities.UnexpectedValue(kind & UnaryOperatorKind.OpMask); } } @@ -177,8 +177,7 @@ public static string BinaryOperatorNameFromOperatorKind(BinaryOperatorKind kind) case BinaryOperatorKind.Subtraction: return WellKnownMemberNames.SubtractionOperatorName; case BinaryOperatorKind.Xor: return WellKnownMemberNames.ExclusiveOrOperatorName; default: - Debug.Assert(false, "Unexpected postfix operator kind for user-defined binary operator"); - return WellKnownMemberNames.AdditionOperatorName; + throw ExceptionUtilities.UnexpectedValue(kind & BinaryOperatorKind.OpMask); } } } diff --git a/src/Compilers/CSharp/Portable/Binder/Semantics/Operators/OperatorKindExtensions.cs b/src/Compilers/CSharp/Portable/Binder/Semantics/Operators/OperatorKindExtensions.cs index 139d4f50b2e63..caebbda2dff09 100644 --- a/src/Compilers/CSharp/Portable/Binder/Semantics/Operators/OperatorKindExtensions.cs +++ b/src/Compilers/CSharp/Portable/Binder/Semantics/Operators/OperatorKindExtensions.cs @@ -152,8 +152,7 @@ public static BinaryOperatorKind WithType(this BinaryOperatorKind kind, SpecialT case SpecialType.System_UInt64: return kind | BinaryOperatorKind.ULong; default: - Debug.Assert(false, "Unexpected binary operator type."); - return kind; + throw ExceptionUtilities.UnexpectedValue(type); } } @@ -171,8 +170,7 @@ public static UnaryOperatorKind WithType(this UnaryOperatorKind kind, SpecialTyp case SpecialType.System_UInt64: return kind | UnaryOperatorKind.ULong; default: - Debug.Assert(false, "Unexpected unary operator type."); - return kind; + throw ExceptionUtilities.UnexpectedValue(type); } } diff --git a/src/Compilers/CSharp/Portable/Binder/Semantics/OverloadResolution/MemberAnalysisResult.cs b/src/Compilers/CSharp/Portable/Binder/Semantics/OverloadResolution/MemberAnalysisResult.cs index 57d6502b18bfd..1f67e66307db4 100644 --- a/src/Compilers/CSharp/Portable/Binder/Semantics/OverloadResolution/MemberAnalysisResult.cs +++ b/src/Compilers/CSharp/Portable/Binder/Semantics/OverloadResolution/MemberAnalysisResult.cs @@ -154,8 +154,7 @@ public static MemberAnalysisResult ArgumentParameterMismatch(ArgumentAnalysisRes case ArgumentAnalysisResultKind.NameUsedForPositional: return NameUsedForPositional(argAnalysis.ArgumentPosition); default: - Debug.Assert(false, "Missing case in argument parameter mismatch analysis."); - goto case ArgumentAnalysisResultKind.NoCorrespondingParameter; + throw ExceptionUtilities.UnexpectedValue(argAnalysis.Kind); } } diff --git a/src/Compilers/CSharp/Portable/Binder/WithCrefTypeParametersBinder.cs b/src/Compilers/CSharp/Portable/Binder/WithCrefTypeParametersBinder.cs index 13a920c377e20..047b097dd5d98 100644 --- a/src/Compilers/CSharp/Portable/Binder/WithCrefTypeParametersBinder.cs +++ b/src/Compilers/CSharp/Portable/Binder/WithCrefTypeParametersBinder.cs @@ -65,8 +65,7 @@ private MultiDictionary CreateTypeParameterMap() } default: { - Debug.Assert(false, "Unexpected cref syntax kind " + _crefSyntax.Kind()); - break; + throw ExceptionUtilities.UnexpectedValue(_crefSyntax.Kind()); } } return map; @@ -93,8 +92,7 @@ private void AddTypeParameters(TypeSyntax typeSyntax, MultiDictionary 0 ? Cci.CallingConvention.Generic : 0); + throw ExceptionUtilities.UnexpectedValue(member.Kind); } } diff --git a/src/Compilers/CSharp/Portable/Symbols/MemberSymbolExtensions.cs b/src/Compilers/CSharp/Portable/Symbols/MemberSymbolExtensions.cs index 2fb2862dfde2b..8d6cf08844b57 100644 --- a/src/Compilers/CSharp/Portable/Symbols/MemberSymbolExtensions.cs +++ b/src/Compilers/CSharp/Portable/Symbols/MemberSymbolExtensions.cs @@ -37,8 +37,7 @@ internal static ImmutableArray GetParameters(this Symbol member case SymbolKind.Event: return ImmutableArray.Empty; default: - Debug.Assert(false, "Unexpected member kind " + member.Kind); - return ImmutableArray.Empty; + throw ExceptionUtilities.UnexpectedValue(member.Kind); } } @@ -56,8 +55,7 @@ internal static ImmutableArray GetParameterTypes(this Symbol member) case SymbolKind.Event: return ImmutableArray.Empty; default: - Debug.Assert(false, "Unexpected member kind " + member.Kind); - return ImmutableArray.Empty; + throw ExceptionUtilities.UnexpectedValue(member.Kind); } } @@ -71,8 +69,7 @@ internal static bool GetIsVararg(this Symbol member) case SymbolKind.Event: return false; default: - Debug.Assert(false, "Unexpected member kind " + member.Kind); - return false; + throw ExceptionUtilities.UnexpectedValue(member.Kind); } } @@ -90,8 +87,7 @@ internal static ImmutableArray GetParameterRefKinds(this Symbol member) case SymbolKind.Event: return ImmutableArray.Empty; default: - Debug.Assert(false, "Unexpected member kind " + member.Kind); - return ImmutableArray.Empty; + throw ExceptionUtilities.UnexpectedValue(member.Kind); } } @@ -106,8 +102,7 @@ internal static int GetParameterCount(this Symbol member) case SymbolKind.Event: return 0; default: - Debug.Assert(false, "Unexpected member kind " + member.Kind); - return 0; + throw ExceptionUtilities.UnexpectedValue(member.Kind); } } @@ -202,8 +197,7 @@ public static int CustomModifierCount(this Symbol m) case SymbolKind.Property: return ((PropertySymbol)m).CustomModifierCount(); default: - Debug.Assert(false); - return 0; + throw ExceptionUtilities.UnexpectedValue(m.Kind); } } @@ -325,8 +319,7 @@ internal static ImmutableArray GetMemberTypeParameters(this case SymbolKind.Event: return ImmutableArray.Empty; default: - Debug.Assert(false, String.Format("{0} is not a kind of member", symbol.Kind)); - return ImmutableArray.Empty; + throw ExceptionUtilities.UnexpectedValue(symbol.Kind); } } @@ -344,8 +337,7 @@ internal static ImmutableArray GetMemberTypeArgumentsNoUseSiteDiagno case SymbolKind.Event: return ImmutableArray.Empty; default: - Debug.Assert(false, String.Format("{0} is not a kind of member", symbol.Kind)); - return ImmutableArray.Empty; + throw ExceptionUtilities.UnexpectedValue(symbol.Kind); } } @@ -518,10 +510,7 @@ internal static void GetTypeOrReturnType(this Symbol member, out TypeSymbol retu returnTypeCustomModifiers = ImmutableArray.Empty; break; default: - Debug.Assert(false, "Unexpected member kind " + member.Kind); - returnType = null; - returnTypeCustomModifiers = ImmutableArray.Empty; - break; + throw ExceptionUtilities.UnexpectedValue(member.Kind); } } diff --git a/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PEFieldSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PEFieldSymbol.cs index 151263831e7fe..1c17db167dfce 100644 --- a/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PEFieldSymbol.cs +++ b/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PEFieldSymbol.cs @@ -413,8 +413,7 @@ public override Accessibility DeclaredAccessibility break; default: - Debug.Assert(false, "Unexpected!!!"); - break; + throw ExceptionUtilities.UnexpectedValue(_flags & FieldAttributes.FieldAccessMask); } return access; diff --git a/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PENamedTypeSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PENamedTypeSymbol.cs index dae2e742970bc..c2fe2aa6913cd 100644 --- a/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PENamedTypeSymbol.cs +++ b/src/Compilers/CSharp/Portable/Symbols/Metadata/PE/PENamedTypeSymbol.cs @@ -538,8 +538,7 @@ public override Accessibility DeclaredAccessibility break; default: - Debug.Assert(false, "Unexpected!!!"); - break; + throw ExceptionUtilities.UnexpectedValue(_flags & TypeAttributes.VisibilityMask); } return access; diff --git a/src/Compilers/CSharp/Portable/Symbols/MethodSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/MethodSymbol.cs index d9099187dbeab..37db5b71a566d 100644 --- a/src/Compilers/CSharp/Portable/Symbols/MethodSymbol.cs +++ b/src/Compilers/CSharp/Portable/Symbols/MethodSymbol.cs @@ -476,8 +476,7 @@ internal static bool CanOverrideOrHide(MethodKind kind) case MethodKind.PropertySet: return true; default: - Debug.Assert(false, $"Unexpected method kind '{kind}'"); - return false; + throw ExceptionUtilities.UnexpectedValue(kind); } } diff --git a/src/Compilers/CSharp/Portable/Symbols/MethodSymbolExtensions.cs b/src/Compilers/CSharp/Portable/Symbols/MethodSymbolExtensions.cs index 518f951e7760f..3d4747efc9bbe 100644 --- a/src/Compilers/CSharp/Portable/Symbols/MethodSymbolExtensions.cs +++ b/src/Compilers/CSharp/Portable/Symbols/MethodSymbolExtensions.cs @@ -1,5 +1,6 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using Roslyn.Utilities; using System.Collections.Generic; using System.Collections.Immutable; using System.Diagnostics; @@ -235,8 +236,7 @@ public static bool CanBeHiddenByMemberKind(this MethodSymbol hiddenMethod, Symbo case SymbolKind.Event: // Events are not covered by CSemanticChecker::FindSymHiddenByMethPropAgg. return true; default: - Debug.Assert(false, "Expected a member kind, found " + hidingMemberKind); - return false; + throw ExceptionUtilities.UnexpectedValue(hidingMemberKind); } } diff --git a/src/Compilers/CSharp/Portable/Symbols/OverriddenOrHiddenMembersHelpers.cs b/src/Compilers/CSharp/Portable/Symbols/OverriddenOrHiddenMembersHelpers.cs index 27621bfe8be4b..eedc04f106fb6 100644 --- a/src/Compilers/CSharp/Portable/Symbols/OverriddenOrHiddenMembersHelpers.cs +++ b/src/Compilers/CSharp/Portable/Symbols/OverriddenOrHiddenMembersHelpers.cs @@ -8,6 +8,7 @@ using Microsoft.CodeAnalysis.CSharp.Symbols; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Text; +using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.CSharp.Symbols { @@ -812,8 +813,7 @@ private static bool CanOverrideOrHide(Symbol member) MethodSymbol methodSymbol = (MethodSymbol)member; return MethodSymbol.CanOverrideOrHide(methodSymbol.MethodKind) && ReferenceEquals(methodSymbol, methodSymbol.ConstructedFrom); default: - Debug.Assert(false, "Unexpected member kind " + member.Kind); - return false; + throw ExceptionUtilities.UnexpectedValue(member.Kind); } } @@ -831,8 +831,7 @@ private static bool TypeOrReturnTypeHasCustomModifiers(Symbol member) EventSymbol @event = (EventSymbol)member; return @event.Type.HasCustomModifiers(); //can't have custom modifiers on (vs in) type default: - Debug.Assert(false, "Unexpected member kind " + member.Kind); - return false; + throw ExceptionUtilities.UnexpectedValue(member.Kind); } } @@ -850,8 +849,7 @@ private static int CustomModifierCount(Symbol member) EventSymbol @event = (EventSymbol)member; return @event.Type.CustomModifierCount(); default: - Debug.Assert(false, "Unexpected member kind " + member.Kind); - return 0; + throw ExceptionUtilities.UnexpectedValue(member.Kind); } } diff --git a/src/Compilers/CSharp/Portable/Symbols/Source/SourceAssemblySymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Source/SourceAssemblySymbol.cs index eb1cd169b8b7a..17a1b8dfe7a5c 100644 --- a/src/Compilers/CSharp/Portable/Symbols/Source/SourceAssemblySymbol.cs +++ b/src/Compilers/CSharp/Portable/Symbols/Source/SourceAssemblySymbol.cs @@ -1773,8 +1773,7 @@ private static bool ContainsExtensionMethods(NamespaceSymbol ns) } break; default: - Debug.Assert(false, "Unexpected member kind: " + member.Kind); - break; + throw ExceptionUtilities.UnexpectedValue(member.Kind); } } return false; diff --git a/src/Compilers/CSharp/Portable/Symbols/Source/SourceEventSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/Source/SourceEventSymbol.cs index baa429f5f5d54..285c975f9fc22 100644 --- a/src/Compilers/CSharp/Portable/Symbols/Source/SourceEventSymbol.cs +++ b/src/Compilers/CSharp/Portable/Symbols/Source/SourceEventSymbol.cs @@ -134,8 +134,7 @@ internal SyntaxList AttributeDeclarationSyntaxList case SyntaxKind.VariableDeclarator: return ((EventFieldDeclarationSyntax)syntax.Parent.Parent).AttributeLists; default: - Debug.Assert(false, "Unknown event syntax kind " + syntax.Kind()); - break; + throw ExceptionUtilities.UnexpectedValue(syntax.Kind()); } } } diff --git a/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberContainerSymbol_ImplementationChecks.cs b/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberContainerSymbol_ImplementationChecks.cs index 5fa88a2babdc4..1de73dd499cab 100644 --- a/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberContainerSymbol_ImplementationChecks.cs +++ b/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberContainerSymbol_ImplementationChecks.cs @@ -6,6 +6,7 @@ using System.Linq; using System.Threading; using Microsoft.CodeAnalysis.CSharp.Symbols; +using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.CSharp.Symbols { @@ -354,8 +355,7 @@ private void CheckMembersAgainstBaseType( break; default: - Debug.Assert(false, "Unexpected named type kind " + this.TypeKind); - break; + throw ExceptionUtilities.UnexpectedValue(this.TypeKind); } foreach (var member in this.GetMembersUnordered()) @@ -919,8 +919,7 @@ private static bool AddHidingAbstractDiagnostic(Symbol hidingMember, Location hi return true; } default: - Debug.Assert(false, $"Unexpected accessibility {hidingMember.DeclaredAccessibility}"); - break; + throw ExceptionUtilities.UnexpectedValue(hidingMember.DeclaredAccessibility); } return false; } diff --git a/src/Compilers/CSharp/Portable/Symbols/SymbolDistinguisher.cs b/src/Compilers/CSharp/Portable/Symbols/SymbolDistinguisher.cs index 692de945e15f1..789f74f0fa857 100644 --- a/src/Compilers/CSharp/Portable/Symbols/SymbolDistinguisher.cs +++ b/src/Compilers/CSharp/Portable/Symbols/SymbolDistinguisher.cs @@ -74,8 +74,6 @@ private static void CheckSymbolKind(Symbol symbol) case SymbolKind.Local: case SymbolKind.RangeVariable: case SymbolKind.Preprocessing: - Debug.Assert(false, "Unsupported symbol kind " + symbol.Kind); - break; default: throw ExceptionUtilities.UnexpectedValue(symbol.Kind); } diff --git a/src/Compilers/CSharp/Portable/Symbols/SymbolExtensions.cs b/src/Compilers/CSharp/Portable/Symbols/SymbolExtensions.cs index 27e5e4fc9ed15..3e086ecdd5c98 100644 --- a/src/Compilers/CSharp/Portable/Symbols/SymbolExtensions.cs +++ b/src/Compilers/CSharp/Portable/Symbols/SymbolExtensions.cs @@ -183,8 +183,7 @@ public static Symbol ConstructedFrom(this Symbol symbol) return ((MethodSymbol)symbol).ConstructedFrom; default: - Debug.Assert(false, "Unexpected symbol: " + symbol.Kind); - return symbol; + throw ExceptionUtilities.UnexpectedValue(symbol.Kind); } } diff --git a/src/Compilers/CSharp/Portable/Symbols/TypeSymbol.cs b/src/Compilers/CSharp/Portable/Symbols/TypeSymbol.cs index 279df1f614b74..e0cda13bde351 100644 --- a/src/Compilers/CSharp/Portable/Symbols/TypeSymbol.cs +++ b/src/Compilers/CSharp/Portable/Symbols/TypeSymbol.cs @@ -921,8 +921,7 @@ private static void CheckForImplementationOfCorrespondingPropertyOrEvent(MethodS correspondingImplementingAccessor = ((EventSymbol)implementingPropertyOrEvent).GetOwnOrInheritedRemoveMethod(); break; default: - Debug.Assert(false, "Expected property or event accessor"); - break; + throw ExceptionUtilities.UnexpectedValue(interfaceMethod.MethodKind); } } @@ -1062,9 +1061,7 @@ private static void ReportImplicitImplementationMismatchDiagnostics(Symbol inter interfaceMemberReturnType = ((EventSymbol)interfaceMember).Type; break; default: - Debug.Assert(false, "Unexpected interface member kind " + interfaceMember.Kind); - interfaceMemberReturnType = null; - break; + throw ExceptionUtilities.UnexpectedValue(interfaceMember.Kind); } DiagnosticInfo useSiteDiagnostic; diff --git a/src/Compilers/CSharp/Portable/Symbols/VarianceSafety.cs b/src/Compilers/CSharp/Portable/Symbols/VarianceSafety.cs index a00e366b82e74..241c33cc7963c 100644 --- a/src/Compilers/CSharp/Portable/Symbols/VarianceSafety.cs +++ b/src/Compilers/CSharp/Portable/Symbols/VarianceSafety.cs @@ -4,6 +4,7 @@ using System.Diagnostics; using Microsoft.CodeAnalysis.CSharp.Symbols; using Microsoft.CodeAnalysis.CSharp.Syntax; +using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.CSharp.Symbols { @@ -318,8 +319,7 @@ private static bool IsVarianceUnsafe( requireOut = true; break; default: - Debug.Assert(false, "Unknown variance kind " + typeParam.Variance); - goto case VarianceKind.None; + throw ExceptionUtilities.UnexpectedValue(typeParam.Variance); } if (IsVarianceUnsafe(typeArg, requireOut, requireIn, context, locationProvider, locationArg, diagnostics)) @@ -368,8 +368,7 @@ private static void AddVarianceError( actualVariance = MessageID.IDS_Covariant; break; default: - Debug.Assert(false, "Unexpected variance " + unsafeTypeParameter.Variance); - return; + throw ExceptionUtilities.UnexpectedValue(unsafeTypeParameter.Variance); } // Get a location that roughly represents the unsafe type parameter use. diff --git a/src/Compilers/CSharp/Portable/Syntax/CSharpSyntaxTree.cs b/src/Compilers/CSharp/Portable/Syntax/CSharpSyntaxTree.cs index 11f6c65abb260..e6250699fde3c 100644 --- a/src/Compilers/CSharp/Portable/Syntax/CSharpSyntaxTree.cs +++ b/src/Compilers/CSharp/Portable/Syntax/CSharpSyntaxTree.cs @@ -262,7 +262,7 @@ private void BuildPreprocessorStateChangeMap() break; default: - throw ExceptionUtilities.Unreachable; + throw ExceptionUtilities.UnexpectedValue(directive.Kind()); } } diff --git a/src/Compilers/CSharp/Portable/Syntax/SyntaxFacts.cs b/src/Compilers/CSharp/Portable/Syntax/SyntaxFacts.cs index 54e874c1eae03..4083b48d8c937 100644 --- a/src/Compilers/CSharp/Portable/Syntax/SyntaxFacts.cs +++ b/src/Compilers/CSharp/Portable/Syntax/SyntaxFacts.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using Microsoft.CodeAnalysis.CSharp.Syntax; +using Roslyn.Utilities; using static Microsoft.CodeAnalysis.CSharp.SyntaxKind; namespace Microsoft.CodeAnalysis.CSharp @@ -301,8 +302,7 @@ public static string GetText(Accessibility accessibility) case Accessibility.Public: return SyntaxFacts.GetText(PublicKeyword); default: - System.Diagnostics.Debug.Assert(false, $"Unknown accessibility '{accessibility}'"); - return null; + throw ExceptionUtilities.UnexpectedValue(accessibility); } } diff --git a/src/Compilers/Core/Portable/CodeGen/SwitchIntegralJumpTableEmitter.cs b/src/Compilers/Core/Portable/CodeGen/SwitchIntegralJumpTableEmitter.cs index 2444067e2dfa2..f8ccf35a948e3 100644 --- a/src/Compilers/Core/Portable/CodeGen/SwitchIntegralJumpTableEmitter.cs +++ b/src/Compilers/Core/Portable/CodeGen/SwitchIntegralJumpTableEmitter.cs @@ -449,8 +449,7 @@ private static ILOpCode GetReverseBranchCode(ILOpCode branchCode) return ILOpCode.Ble_un; default: - Debug.Assert(false, "Unhandled branch opcode for switch emitter"); - return ILOpCode.Nop; + throw ExceptionUtilities.UnexpectedValue(branchCode); } } diff --git a/src/Compilers/Core/Portable/Diagnostic/DiagnosticInfo.cs b/src/Compilers/Core/Portable/Diagnostic/DiagnosticInfo.cs index 544dcad03dee8..547df15a4ae4d 100644 --- a/src/Compilers/Core/Portable/Diagnostic/DiagnosticInfo.cs +++ b/src/Compilers/Core/Portable/Diagnostic/DiagnosticInfo.cs @@ -109,7 +109,7 @@ internal static void AssertMessageSerializable(object[] args) continue; } - Debug.Assert(false, "Unexpected type: " + type); + throw ExceptionUtilities.UnexpectedValue(type); } } diff --git a/src/Compilers/Core/Portable/Emit/EditAndContinue/SymbolChanges.cs b/src/Compilers/Core/Portable/Emit/EditAndContinue/SymbolChanges.cs index 597fb05219bca..1cbacb1b0cb9f 100644 --- a/src/Compilers/Core/Portable/Emit/EditAndContinue/SymbolChanges.cs +++ b/src/Compilers/Core/Portable/Emit/EditAndContinue/SymbolChanges.cs @@ -52,7 +52,8 @@ public SymbolChange GetChange(IDefinition def) var generator = synthesizedDef.Method; var synthesizedSymbol = (ISymbol)synthesizedDef; - switch (GetChange((IDefinition)generator)) + var change = GetChange((IDefinition)generator); + switch (change) { case SymbolChange.Updated: // The generator has been updated. Some synthesized members should be reused, others updated or added. @@ -123,7 +124,7 @@ public SymbolChange GetChange(IDefinition def) default: // The method had to change, otherwise the synthesized symbol wouldn't be generated - throw ExceptionUtilities.Unreachable; + throw ExceptionUtilities.UnexpectedValue(change); } } @@ -158,7 +159,8 @@ private SymbolChange GetChange(ISymbol symbol) return SymbolChange.None; } - switch (this.GetChange(container)) + change = this.GetChange(container); + switch (change) { case SymbolChange.Added: return SymbolChange.Added; @@ -179,7 +181,7 @@ private SymbolChange GetChange(ISymbol symbol) return SymbolChange.None; default: - throw ExceptionUtilities.Unreachable; + throw ExceptionUtilities.UnexpectedValue(change); } } diff --git a/src/Compilers/Core/Portable/MetadataReader/PEModule.cs b/src/Compilers/Core/Portable/MetadataReader/PEModule.cs index 335b3d06d20a5..f0dca52fcae6b 100644 --- a/src/Compilers/Core/Portable/MetadataReader/PEModule.cs +++ b/src/Compilers/Core/Portable/MetadataReader/PEModule.cs @@ -1125,9 +1125,7 @@ private bool TryExtractObsoleteDataFromAttribute(AttributeInfo attributeInfo, ou return TryExtractValueFromAttribute(attributeInfo.Handle, out obsoleteData, s_attributeObsoleteDataExtractor); default: - Debug.Assert(false, "unexpected ObsoleteAttribute signature"); - obsoleteData = null; - return false; + throw ExceptionUtilities.UnexpectedValue(attributeInfo.SignatureIndex); } } @@ -1143,9 +1141,7 @@ private bool TryExtractDeprecatedDataFromAttribute(AttributeInfo attributeInfo, return TryExtractValueFromAttribute(attributeInfo.Handle, out obsoleteData, s_attributeDeprecatedDataExtractor); default: - Debug.Assert(false, "unexpected DeprecatedAttribute signature"); - obsoleteData = null; - return false; + throw ExceptionUtilities.UnexpectedValue(attributeInfo.SignatureIndex); } } @@ -1178,9 +1174,7 @@ private bool TryExtractInterfaceTypeFromAttribute(AttributeInfo attributeInfo, o break; default: - Debug.Assert(false, "unexpected InterfaceTypeAttribute signature"); - interfaceType = 0; - return false; + throw ExceptionUtilities.UnexpectedValue(attributeInfo.SignatureIndex); } interfaceType = default(ComInterfaceType); @@ -1229,9 +1223,7 @@ private bool TryExtractTypeLibTypeFromAttribute(AttributeInfo info, out Cci.Type break; default: - Debug.Assert(false, "unexpected TypeLibAttribute signature"); - flags = 0; - return false; + throw ExceptionUtilities.UnexpectedValue(info.SignatureIndex); } flags = default(Cci.TypeLibTypeFlags); diff --git a/src/Compilers/Core/Portable/RuleSet/RuleSetProcessor.cs b/src/Compilers/Core/Portable/RuleSet/RuleSetProcessor.cs index 52cc714c49e4c..55d2be3c6c3d2 100644 --- a/src/Compilers/Core/Portable/RuleSet/RuleSetProcessor.cs +++ b/src/Compilers/Core/Portable/RuleSet/RuleSetProcessor.cs @@ -81,10 +81,7 @@ public static RuleSet LoadFromFile(string filePath) // Find the top level rule set node List nodeList = ruleSetDocument.Elements(RuleSetNodeName).ToList(); - if (nodeList.Count != 1) - { - Debug.Assert(false, "Multiple top-level nodes!"); - } + Debug.Assert(nodeList.Count == 1, "Multiple top-level nodes!"); Debug.Assert(nodeList[0].Name == RuleSetNodeName); ruleSetNode = nodeList[0]; } diff --git a/src/Compilers/VisualBasic/Portable/Analysis/FlowAnalysis/AbstractFlowPass.vb b/src/Compilers/VisualBasic/Portable/Analysis/FlowAnalysis/AbstractFlowPass.vb index a2269fe7b36e2..0c83609579182 100644 --- a/src/Compilers/VisualBasic/Portable/Analysis/FlowAnalysis/AbstractFlowPass.vb +++ b/src/Compilers/VisualBasic/Portable/Analysis/FlowAnalysis/AbstractFlowPass.vb @@ -468,7 +468,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic Case BoundKind.YieldStatement Return Nothing Case Else - Debug.Assert(False) + Throw ExceptionUtilities.UnexpectedValue(branch.Kind) End Select Return Nothing End Function diff --git a/src/Compilers/VisualBasic/Portable/Analysis/FlowAnalysis/DataFlowPass.vb b/src/Compilers/VisualBasic/Portable/Analysis/FlowAnalysis/DataFlowPass.vb index cad83d0a3178c..7074a442ba43c 100644 --- a/src/Compilers/VisualBasic/Portable/Analysis/FlowAnalysis/DataFlowPass.vb +++ b/src/Compilers/VisualBasic/Portable/Analysis/FlowAnalysis/DataFlowPass.vb @@ -1884,7 +1884,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic End If Case BoundKind.EventAccess - Debug.Assert(False) ' TODO: is this reachable at all? + Throw ExceptionUtilities.UnexpectedValue(expr.Kind) ' TODO: is this reachable at all? Case BoundKind.MeReference, BoundKind.MyClassReference, diff --git a/src/Compilers/VisualBasic/Portable/Analysis/FlowAnalysis/ExitPointsWalker.vb b/src/Compilers/VisualBasic/Portable/Analysis/FlowAnalysis/ExitPointsWalker.vb index daada4dba2582..00d682a63617a 100644 --- a/src/Compilers/VisualBasic/Portable/Analysis/FlowAnalysis/ExitPointsWalker.vb +++ b/src/Compilers/VisualBasic/Portable/Analysis/FlowAnalysis/ExitPointsWalker.vb @@ -114,7 +114,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic Case BoundKind.ReturnStatement ' These are always included (we don't dive into lambda expressions) Case Else - Debug.Assert(False) ' there are no other branch statements + Throw ExceptionUtilities.UnexpectedValue(pending.Branch.Kind) ' there are no other branch statements End Select _branchesOutOf.Add(DirectCast(pending.Branch.Syntax, StatementSyntax)) End If diff --git a/src/Compilers/VisualBasic/Portable/Analysis/FlowAnalysis/ReadWriteWalker.vb b/src/Compilers/VisualBasic/Portable/Analysis/FlowAnalysis/ReadWriteWalker.vb index bf6ab819d4ebd..4c8dccc5fd0e6 100644 --- a/src/Compilers/VisualBasic/Portable/Analysis/FlowAnalysis/ReadWriteWalker.vb +++ b/src/Compilers/VisualBasic/Portable/Analysis/FlowAnalysis/ReadWriteWalker.vb @@ -59,7 +59,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic Case RegionPlace.Inside _readInside.Add(variable) Case Else - Debug.Assert(False) + Throw ExceptionUtilities.UnexpectedValue(Me._regionPlace) End Select MyBase.NoteRead(variable) CheckCaptured(variable) @@ -76,7 +76,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic Case RegionPlace.Inside _writtenInside.Add(variable) Case Else - Debug.Assert(False) + Throw ExceptionUtilities.UnexpectedValue(Me._regionPlace) End Select MyBase.NoteWrite(variable, value) CheckCaptured(variable) @@ -93,7 +93,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic Case RegionPlace.Inside _captured.Add(variable) Case Else - Debug.Assert(False) + Throw ExceptionUtilities.UnexpectedValue(Me._regionPlace) End Select End If End Sub diff --git a/src/Compilers/VisualBasic/Portable/Binding/Binder_Expressions.vb b/src/Compilers/VisualBasic/Portable/Binding/Binder_Expressions.vb index de05c651412b2..47f0e542e26ec 100644 --- a/src/Compilers/VisualBasic/Portable/Binding/Binder_Expressions.vb +++ b/src/Compilers/VisualBasic/Portable/Binding/Binder_Expressions.vb @@ -856,7 +856,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic [call].ConstantValueOpt, [call].SuppressObjectClone, [call].Type, [call].HasErrors) Case Else - Debug.Assert(False) + Throw ExceptionUtilities.UnexpectedValue(result.Kind) End Select Return result @@ -968,7 +968,6 @@ Namespace Microsoft.CodeAnalysis.VisualBasic Select Case propertyAccess.AccessKind Case PropertyAccessKind.Set - Debug.Assert(False) ReportDiagnostic(diagnostics, syntax, ERRID.ERR_VoidValue) Return BadExpression(syntax, expr, LookupResultKind.NotAValue, ErrorTypeSymbol.UnknownResultType) @@ -1004,7 +1003,6 @@ Namespace Microsoft.CodeAnalysis.VisualBasic ElseIf expr.IsLateBound() Then If (expr.GetLateBoundAccessKind() And (LateBoundAccessKind.Set Or LateBoundAccessKind.Call)) <> 0 Then - Debug.Assert(False) ReportDiagnostic(diagnostics, syntax, ERRID.ERR_VoidValue) Return BadExpression(syntax, expr, LookupResultKind.NotAValue, ErrorTypeSymbol.UnknownResultType) End If @@ -1605,7 +1603,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic Case Else ' What else can it be? - Debug.Assert(False) + Throw ExceptionUtilities.UnexpectedValue(containingMember.Kind) End Select End If diff --git a/src/Compilers/VisualBasic/Portable/Binding/Binder_Lambda.vb b/src/Compilers/VisualBasic/Portable/Binding/Binder_Lambda.vb index 1b6bfed75ab23..d891b8ff9b4a4 100644 --- a/src/Compilers/VisualBasic/Portable/Binding/Binder_Lambda.vb +++ b/src/Compilers/VisualBasic/Portable/Binding/Binder_Lambda.vb @@ -358,8 +358,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic End Function Public Overrides Function VisitLambda(node As BoundLambda) As BoundNode - Debug.Assert(False) - Return Nothing + Throw ExceptionUtilities.Unreachable End Function Public Overrides Function VisitReturnStatement(node As BoundReturnStatement) As BoundNode @@ -1059,8 +1058,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic End Function Public Overrides Function VisitLambda(node As BoundLambda) As BoundNode - Debug.Assert(False) - Return Nothing + Throw ExceptionUtilities.Unreachable End Function Public Overrides Function VisitReturnStatement(node As BoundReturnStatement) As BoundNode diff --git a/src/Compilers/VisualBasic/Portable/Compilation/SemanticModel.vb b/src/Compilers/VisualBasic/Portable/Compilation/SemanticModel.vb index ccb16cbbbc280..fa2f4e862a187 100644 --- a/src/Compilers/VisualBasic/Portable/Compilation/SemanticModel.vb +++ b/src/Compilers/VisualBasic/Portable/Compilation/SemanticModel.vb @@ -1442,7 +1442,7 @@ _Default: resultKind = LookupResult.WorseResultKind(resultKind, boundBadExpression.ResultKind) Case Else - Debug.Assert(False) + Throw ExceptionUtilities.UnexpectedValue(boundNodeOfSyntacticParent.Kind) End Select AdjustSymbolsForObjectCreation(lowestBoundNode, namedTypeSymbol, constructor, binderOpt, bindingSymbols, memberGroupBuilder, resultKind) diff --git a/src/Compilers/VisualBasic/Portable/Compilation/SyntaxTreeSemanticModel.vb b/src/Compilers/VisualBasic/Portable/Compilation/SyntaxTreeSemanticModel.vb index ed66ecaa56564..80953a1f3ee0e 100644 --- a/src/Compilers/VisualBasic/Portable/Compilation/SyntaxTreeSemanticModel.vb +++ b/src/Compilers/VisualBasic/Portable/Compilation/SyntaxTreeSemanticModel.vb @@ -1577,7 +1577,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic End If End If - Debug.Assert(False) + Throw ExceptionUtilities.Unreachable End Sub ''' diff --git a/src/Compilers/VisualBasic/Portable/Lowering/LocalRewriter/LocalRewriter_BinaryOperators.vb b/src/Compilers/VisualBasic/Portable/Lowering/LocalRewriter/LocalRewriter_BinaryOperators.vb index 4194ff0b8dcfa..9fb40379c8e02 100644 --- a/src/Compilers/VisualBasic/Portable/Lowering/LocalRewriter/LocalRewriter_BinaryOperators.vb +++ b/src/Compilers/VisualBasic/Portable/Lowering/LocalRewriter/LocalRewriter_BinaryOperators.vb @@ -533,7 +533,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic End If Else - Debug.Assert(False) + Throw ExceptionUtilities.Unreachable End If Return result diff --git a/src/Compilers/VisualBasic/Portable/Lowering/LocalRewriter/LocalRewriter_Conversion.vb b/src/Compilers/VisualBasic/Portable/Lowering/LocalRewriter/LocalRewriter_Conversion.vb index c4e97c23f764f..26a464d478398 100644 --- a/src/Compilers/VisualBasic/Portable/Lowering/LocalRewriter/LocalRewriter_Conversion.vb +++ b/src/Compilers/VisualBasic/Portable/Lowering/LocalRewriter/LocalRewriter_Conversion.vb @@ -709,8 +709,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic ' This point should not be reachable, because if there is no constructor in the ' loaded value type, we should have generated a synthesized constructor. - Debug.Assert(False) - Return node + Throw ExceptionUtilities.Unreachable End Function Private Function RewriteReferenceTypeToCharArrayRankOneConversion(node As BoundConversion, typeFrom As TypeSymbol, typeTo As TypeSymbol) As BoundExpression diff --git a/src/Compilers/VisualBasic/Portable/Semantics/TypeInference/Graph.vb b/src/Compilers/VisualBasic/Portable/Semantics/TypeInference/Graph.vb index 836d450e395d7..4731bd0a60ac4 100644 --- a/src/Compilers/VisualBasic/Portable/Semantics/TypeInference/Graph.vb +++ b/src/Compilers/VisualBasic/Portable/Semantics/TypeInference/Graph.vb @@ -113,7 +113,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic End If Next - Debug.Assert(False) + Throw ExceptionUtilities.Unreachable End Sub diff --git a/src/Compilers/VisualBasic/Portable/Semantics/TypeInference/TypeArgumentInference.vb b/src/Compilers/VisualBasic/Portable/Semantics/TypeInference/TypeArgumentInference.vb index fd8068df9d943..eac5b4b4abec4 100644 --- a/src/Compilers/VisualBasic/Portable/Semantics/TypeInference/TypeArgumentInference.vb +++ b/src/Compilers/VisualBasic/Portable/Semantics/TypeInference/TypeArgumentInference.vb @@ -799,9 +799,7 @@ HandleAsAGeneralExpression: ' modifications in the future. However, we still need an assert to guard ' against graph traversal bugs, and in the event that such changes are ' made, leave it to the modifier to remove the assert if necessary. - Debug.Assert(False) - restartAlgorithm = True - Exit For + Throw ExceptionUtilities.Unreachable End If Else diff --git a/src/Compilers/VisualBasic/Portable/Symbols/EmbeddedSymbols/EmbeddedSymbolManager.SymbolsCollection.vb b/src/Compilers/VisualBasic/Portable/Symbols/EmbeddedSymbols/EmbeddedSymbolManager.SymbolsCollection.vb index 65c28046ef5dd..c43efbe471a88 100644 --- a/src/Compilers/VisualBasic/Portable/Symbols/EmbeddedSymbols/EmbeddedSymbolManager.SymbolsCollection.vb +++ b/src/Compilers/VisualBasic/Portable/Symbols/EmbeddedSymbols/EmbeddedSymbolManager.SymbolsCollection.vb @@ -350,7 +350,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols Case Else ' No other symbol kinds are allowed - Debug.Assert(False) + Throw ExceptionUtilities.UnexpectedValue(member.Kind) End Select diff --git a/src/Compilers/VisualBasic/Portable/Symbols/MergedNamespaceSymbol.vb b/src/Compilers/VisualBasic/Portable/Symbols/MergedNamespaceSymbol.vb index 98347ef44c67a..b66546725e742 100644 --- a/src/Compilers/VisualBasic/Portable/Symbols/MergedNamespaceSymbol.vb +++ b/src/Compilers/VisualBasic/Portable/Symbols/MergedNamespaceSymbol.vb @@ -650,7 +650,6 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols ' of memory overhead) unless there is actual merging going on. Debug.Assert(namespaceArray.Count <> 0) If namespaceArray.Count = 0 Then - Debug.Assert(False) namespaceArray.Free() Return Me End If @@ -665,8 +664,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols Return result End If - Debug.Assert(False) - Return Me + Throw ExceptionUtilities.Unreachable End If Dim lookup = New SmallDictionary(Of NamespaceSymbol, Boolean)() diff --git a/src/Compilers/VisualBasic/Portable/Symbols/Source/SourceMemberContainerTypeSymbol.vb b/src/Compilers/VisualBasic/Portable/Symbols/Source/SourceMemberContainerTypeSymbol.vb index 046f7a151b20a..7928a7c2ffce1 100644 --- a/src/Compilers/VisualBasic/Portable/Symbols/Source/SourceMemberContainerTypeSymbol.vb +++ b/src/Compilers/VisualBasic/Portable/Symbols/Source/SourceMemberContainerTypeSymbol.vb @@ -3256,8 +3256,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols Next ' This point should not be reachable. - Debug.Assert(False) - Return -1 + Throw ExceptionUtilities.Unreachable End If Dim syntaxOffset As Integer @@ -3267,8 +3266,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols ' This point should not be reachable. An implicit constructor has no body and no initializer, ' so the variable has to be declared in a member initializer. - Debug.Assert(False) - Return -1 + Throw ExceptionUtilities.Unreachable End Function ' Calculates a syntax offset of a syntax position that is contained in a property or field initializer (if it is in fact contained in one). diff --git a/src/Compilers/VisualBasic/Portable/Symbols/Source/SourceMemberMethodSymbol.vb b/src/Compilers/VisualBasic/Portable/Symbols/Source/SourceMemberMethodSymbol.vb index 16a7703b757ce..7a0c80bb598c4 100644 --- a/src/Compilers/VisualBasic/Portable/Symbols/Source/SourceMemberMethodSymbol.vb +++ b/src/Compilers/VisualBasic/Portable/Symbols/Source/SourceMemberMethodSymbol.vb @@ -752,7 +752,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols ' Valid context Case Else - Debug.Assert(False) + Throw ExceptionUtilities.UnexpectedValue(ContainingType.TypeKind) End Select Dim receiverOpt As BoundExpression = Nothing diff --git a/src/Features/VisualBasic/EditAndContinue/SyntaxUtilities.vb b/src/Features/VisualBasic/EditAndContinue/SyntaxUtilities.vb index 282796affcf0c..bcf7e4a84293b 100644 --- a/src/Features/VisualBasic/EditAndContinue/SyntaxUtilities.vb +++ b/src/Features/VisualBasic/EditAndContinue/SyntaxUtilities.vb @@ -50,7 +50,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.EditAndContinue Return End If - Debug.Assert(False) + Throw ExceptionUtilities.Unreachable End Sub Public Shared Function GetBody(node As LambdaExpressionSyntax) As SyntaxList(Of SyntaxNode)