@@ -279,6 +279,15 @@ void Outdent()
279279 WriteNextLine ( "}" ) ;
280280 }
281281
282+ DebugInfoExpression OutdentWithDebugInfo ( )
283+ {
284+ _indentLevel -- ;
285+ WriteLine ( ) ;
286+ var position = GetPosition ( ) ;
287+ Write ( "}" ) ;
288+ return CreateDebugInfo ( position ) ;
289+ }
290+
282291 Expression VisitGroup ( Expression node , ExpressionType parentNodeType , bool isRightNode = false )
283292 {
284293 Expression result ;
@@ -520,6 +529,9 @@ IEnumerable<Expression> VisitBlockBody(IList<Expression> exprs, bool shouldRetur
520529 for ( int i = 0 ; i < exprs . Count ; i ++ )
521530 {
522531 var expr = exprs [ i ] ;
532+ if ( expr . NodeType == ExpressionType . Default && expr . Type == typeof ( void ) )
533+ continue ;
534+
523535 var isInline = IsInline ( expr ) ;
524536 if ( isInline || i > 0 )
525537 WriteLine ( ) ;
@@ -638,25 +650,25 @@ Expression VisitConditionalBlock(ConditionalExpression node, bool shouldReturn,
638650 Write ( ")" ) ;
639651 Indent ( ) ;
640652 Expression ifTrue = VisitBody ( node . IfTrue , shouldReturn ) ;
641- Outdent ( ) ;
642653 Expression ifFalse = node . IfFalse ;
643- if ( node . Type == typeof ( void ) && node . IfFalse . NodeType != ExpressionType . Default )
654+ if ( node . IfFalse . NodeType != ExpressionType . Default )
644655 {
656+ Outdent ( ) ;
645657 if ( node . IfFalse . NodeType == ExpressionType . Conditional )
646658 ifFalse = VisitConditionalBlock ( ( ConditionalExpression ) node . IfFalse , shouldReturn , true ) ;
647659 else
648660 {
649661 WriteNextLine ( "else" ) ;
650662 Indent ( ) ;
651663 ifFalse = VisitBody ( node . IfFalse , shouldReturn ) ;
652- Outdent ( ) ;
664+ OutdentWithDebugInfo ( ) ;
653665 }
654666 }
667+ else
668+ ifFalse = OutdentWithDebugInfo ( ) ?? ifFalse ;
655669
656670 Expression condition = Expression . Condition ( test , ifTrue , ifFalse , typeof ( void ) ) ;
657- if ( debug != null )
658- condition = Expression . Block ( debug , condition ) ;
659- return condition ;
671+ return CreateBlock ( debug , condition ) ;
660672 }
661673
662674 protected override Expression VisitConditional ( ConditionalExpression node )
@@ -911,7 +923,7 @@ Expression VisitMember(Expression instance, Expression node, MemberInfo member)
911923
912924 protected override Expression VisitIndex ( IndexExpression node )
913925 {
914- var obj = node . Indexer != null
926+ var obj = node . Indexer != null && node . Indexer . DeclaringType . GetCustomAttribute < DefaultMemberAttribute > ( ) ? . MemberName != node . Indexer . Name
915927 ? VisitMember ( node . Object , node , node . Indexer )
916928 : VisitGroup ( node . Object , node . NodeType ) ;
917929
@@ -1043,11 +1055,11 @@ protected override Expression VisitLoop(LoopExpression node)
10431055 Write ( ")" ) ;
10441056 Indent ( ) ;
10451057 body = VisitBody ( condExpr . IfTrue ) ;
1046- Outdent ( ) ;
1058+ var outDebug = OutdentWithDebugInfo ( ) ;
1059+ var outBreak = CreateBlock ( outDebug , @break ) ;
10471060
1048- Expression condition = Expression . Condition ( test , body , @break , typeof ( void ) ) ;
1049- if ( debug != null )
1050- condition = Expression . Block ( debug , condition ) ;
1061+ Expression condition = Expression . Condition ( test , body , outBreak , typeof ( void ) ) ;
1062+ condition = CreateBlock ( debug , condition ) ;
10511063 return Expression . Loop (
10521064 condition ,
10531065 node . BreakLabel ,
@@ -1303,7 +1315,11 @@ protected override Expression VisitRuntimeVariables(RuntimeVariablesExpression n
13031315
13041316 Expression VisitSwitch ( SwitchExpression node , bool shouldReturn )
13051317 {
1306- var value = VisitNextLine ( "switch (" , node . SwitchValue , ")" ) ;
1318+ WriteNextLine ( "switch (" ) ;
1319+ var position = GetPosition ( ) ;
1320+ var value = Visit ( node . SwitchValue ) ;
1321+ var debug = CreateDebugInfo ( position ) ;
1322+ Write ( ")" ) ;
13071323 Indent ( ) ;
13081324
13091325 var cases = node . Cases . Select ( c => VisitSwitchCase ( c , shouldReturn ) ) . ToList ( ) ;
@@ -1313,12 +1329,27 @@ Expression VisitSwitch(SwitchExpression node, bool shouldReturn)
13131329 WriteNextLine ( "default:" ) ;
13141330 _indentLevel ++ ;
13151331 @default = VisitBody ( node . DefaultBody , shouldReturn ) ;
1332+ Expression innerDebug = null ;
13161333 if ( ! shouldReturn )
1317- WriteNextLine ( "break;" ) ;
1334+ {
1335+ WriteLine ( ) ;
1336+ var innerPos = GetPosition ( ) ;
1337+ Write ( "break;" ) ;
1338+ innerDebug = CreateDebugInfo ( innerPos ) ;
1339+ }
13181340 _indentLevel -- ;
1341+ var outDebug = OutdentWithDebugInfo ( ) ;
1342+ @default = CreateBlock ( @default , innerDebug , outDebug ) ;
13191343 }
1320- Outdent ( ) ;
1321- return node . Update ( value , cases , @default ) ;
1344+ else
1345+ @default = OutdentWithDebugInfo ( ) ;
1346+ node = node . Update ( value , cases , @default ) ;
1347+ return CreateBlock ( debug , node ) ;
1348+ }
1349+
1350+ private static BlockExpression CreateBlock ( params Expression [ ] exprs )
1351+ {
1352+ return Expression . Block ( exprs . Where ( expr => expr != null ) ) ;
13221353 }
13231354
13241355 protected override Expression VisitSwitch ( SwitchExpression node )
@@ -1332,7 +1363,13 @@ SwitchCase VisitSwitchCase(SwitchCase node, bool shouldReturn)
13321363 _indentLevel ++ ;
13331364 var body = VisitBody ( node . Body , shouldReturn ) ;
13341365 if ( ! shouldReturn )
1335- WriteNextLine ( "break;" ) ;
1366+ {
1367+ WriteLine ( ) ;
1368+ var position = GetPosition ( ) ;
1369+ Write ( "break;" ) ;
1370+ var debug = CreateDebugInfo ( position ) ;
1371+ body = CreateBlock ( body , debug ) ;
1372+ }
13361373 _indentLevel -- ;
13371374 return node . Update ( values , body ) ;
13381375 }
0 commit comments