Index: src/ResolvExpr/Resolver.cc
===================================================================
--- src/ResolvExpr/Resolver.cc	(revision bb9d8e8868e5e7d1abbe4d4374fe3474c6ddc850)
+++ src/ResolvExpr/Resolver.cc	(revision 7664fad95e0fb246e1ee21e04c5c05d4225425a8)
@@ -109,6 +109,6 @@
 
 	namespace {
-		void finishExpr( Expression *expr, const TypeEnvironment &env ) {
-			expr->set_env( new TypeSubstitution );
+		void finishExpr( Expression *expr, const TypeEnvironment &env, TypeSubstitution * oldenv = nullptr ) {
+			expr->env = oldenv ? oldenv->clone() : new TypeSubstitution;
 			env.makeSubstitution( *expr->get_env() );
 		}
@@ -119,5 +119,5 @@
 		TypeEnvironment env;
 		Expression *newExpr = resolveInVoidContext( untyped, indexer, env );
-		finishExpr( newExpr, env );
+		finishExpr( newExpr, env, untyped->env );
 		return newExpr;
 	}
@@ -129,9 +129,9 @@
 		#if 0
 		if ( finder.get_alternatives().size() != 1 ) {
-			std::cout << "untyped expr is ";
-			untyped->print( std::cout );
-			std::cout << std::endl << "alternatives are:";
-			for ( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) {
-				i->print( std::cout );
+			std::cerr << "untyped expr is ";
+			untyped->print( std::cerr );
+			std::cerr << std::endl << "alternatives are:";
+			for ( const Alternative & alt : finder.get_alternatives() ) {
+				alt.print( std::cerr );
 			} // for
 		} // if
@@ -140,5 +140,5 @@
 		Alternative &choice = finder.get_alternatives().front();
 		Expression *newExpr = choice.expr->clone();
-		finishExpr( newExpr, choice.env );
+		finishExpr( newExpr, choice.env, untyped->env );
 		return newExpr;
 	}
@@ -186,5 +186,5 @@
 				throw SemanticError( "No interpretations for case control expression", untyped );
 			} // if
-			finishExpr( newExpr, *newEnv );
+			finishExpr( newExpr, *newEnv, untyped->env );
 			return newExpr;
 		}
Index: src/SymTab/Validate.cc
===================================================================
--- src/SymTab/Validate.cc	(revision bb9d8e8868e5e7d1abbe4d4374fe3474c6ddc850)
+++ src/SymTab/Validate.cc	(revision 7664fad95e0fb246e1ee21e04c5c05d4225425a8)
@@ -276,5 +276,5 @@
 		ReturnChecker::checkFunctionReturns( translationUnit );
 		mutateAll( translationUnit, compoundliteral );
-		acceptAll( translationUnit, fpd );
+		acceptAll( translationUnit, fpd ); // must happen before autogenerateRoutines
 		ArrayLength::computeLength( translationUnit );
 		acceptAll( translationUnit, finder );
