@@ -1129,28 +1129,42 @@ public JSExpression TranslateNode (ILExpression expression) {
1129
1129
( expression . InferredType != null ) &&
1130
1130
! TypeUtil . TypesAreAssignable ( TypeInfo , expression . ExpectedType , expression . InferredType )
1131
1131
) {
1132
+ var expectedType = expression . ExpectedType ;
1133
+
1132
1134
// HACK: Expected types inside of comparison expressions are wrong, so we need to suppress
1133
1135
// the casts they would normally generate sometimes.
1134
1136
bool shouldAutoCast = (
1135
1137
AutoCastingState . Peek ( ) ||
1136
1138
(
1137
1139
// Comparisons between value types still need a cast.
1138
- ! TypeUtil . IsReferenceType ( expression . ExpectedType ) ||
1140
+ ! TypeUtil . IsReferenceType ( expectedType ) ||
1139
1141
! TypeUtil . IsReferenceType ( expression . InferredType )
1140
1142
)
1141
1143
) ;
1142
1144
1145
+ // HACK: ILSpy improperly decompiles sequences like:
1146
+ // byte * px = ...;
1147
+ // *((A*)px) = new A()
1148
+ // into a cast of the form ((A&)px) = new A()
1149
+ if (
1150
+ ( expectedType is ByReferenceType ) &&
1151
+ ( expression . InferredType is PointerType ) &&
1152
+ ! TypeUtil . TypesAreEqual ( expectedType . GetElementType ( ) , expression . InferredType . GetElementType ( ) )
1153
+ ) {
1154
+ expectedType = new PointerType ( expectedType . GetElementType ( ) ) ;
1155
+ }
1156
+
1143
1157
bool specialNullableCast = (
1144
- TypeUtil . IsNullable ( expression . ExpectedType ) ||
1158
+ TypeUtil . IsNullable ( expectedType ) ||
1145
1159
TypeUtil . IsNullable ( expression . InferredType ) ||
1146
1160
( expression . Code == ILCode . ValueOf )
1147
1161
) ;
1148
1162
1149
1163
if ( shouldAutoCast ) {
1150
1164
if ( specialNullableCast )
1151
- return new JSNullableCastExpression ( result , new JSType ( expression . ExpectedType ) ) ;
1165
+ return new JSNullableCastExpression ( result , new JSType ( expectedType ) ) ;
1152
1166
else
1153
- return JSCastExpression . New ( result , expression . ExpectedType , TypeSystem , isCoercion : true ) ;
1167
+ return JSCastExpression . New ( result , expectedType , TypeSystem , isCoercion : true ) ;
1154
1168
} else {
1155
1169
// FIXME: Should this be JSChangeTypeExpression to preserve type information?
1156
1170
// I think not, because a lot of these ExpectedTypes are wrong.
0 commit comments