This project is read-only.
1
Vote

GetConversion on CastExpressionSyntax returns Identity

description

Perhaps I am making some bad assumptions, but this seems wrong to me:

If I have a statement such as
var x = (char)65;
the RHS is a CastExpressionSyntax. But if I try to use SemanticModel.GetConversion to find out what type of cast is being performed here (on both the node and its child expression), it reports back Identity:
var conversionOnNode = semanticModel.GetConversion(castExpression);
// conversionOnNode is 'Identity'

var conversionOnChild = semanticModel.GetConversion(castExpression.Expression);
// conversionOnChild is 'Identity'
Also if I ask for the TypeInfo of both these nodes, they both report the same types for Type and ConvertedType:
var typeInfoOnNode = semanticModel.GetTypeInfo(castExpression);
// Both .Type and .ConvertedType are Char

var typeInfoOnChild = semanticModel.GetTypeInfo(castExpression.Expression);
// Both .Type and .ConvertedType are Int32
Both of these situations seem wrong to me. It seems like the only way to determine the type of conversion being done here is to get the TypeInfo of both the cast node and its expression and compare them, or use ClassifyConversion on the parts of the cast node:
var type = semanticModel.GetTypeInfo(castExpression.Type).Type;
var conversionClassified = semanticModel.ClassifyConversion(castExpression.Expression, type);
// conversionClassified is 'ExplicitNumeric'

comments

erikkallen wrote Nov 19, 2014 at 10:13 PM