Index: src/GenPoly/Box.cc
===================================================================
--- src/GenPoly/Box.cc	(revision 4b8f918cba0878620f93c5453ef29a5abc15547a)
+++ src/GenPoly/Box.cc	(revision aa19ccf5f75077e36bab7894ba1178cf77c35693)
@@ -292,4 +292,9 @@
 			/// adds type parameters to the layout call; will generate the appropriate parameters if needed
 			void addOtypeParamsToLayoutCall( UntypedExpr *layoutCall, const std::list< Type* > &otypeParams );
+
+			/// Enters a new scope for type-variables, adding the type variables from ty
+			void beginTypeScope( Type *ty );
+			/// Exits the type-variable scope
+			void endTypeScope();
 			
 			ScopedSet< std::string > knownLayouts;          ///< Set of generic type layouts known in the current scope, indexed by sizeofName
@@ -1821,12 +1826,24 @@
 ////////////////////////////////////////// PolyGenericCalculator ////////////////////////////////////////////////////
 
+		void PolyGenericCalculator::beginTypeScope( Type *ty ) {
+			scopeTyVars.beginScope();
+			makeTyVarMap( ty, scopeTyVars );
+		}
+
+		void PolyGenericCalculator::endTypeScope() {
+			scopeTyVars.endScope();
+		}
+
 		template< typename DeclClass >
 		DeclClass * PolyGenericCalculator::handleDecl( DeclClass *decl, Type *type ) {
-			scopeTyVars.beginScope();
-			makeTyVarMap( type, scopeTyVars );
+			beginTypeScope( type );
+			knownLayouts.beginScope();
+			knownOffsets.beginScope();
 
 			DeclClass *ret = static_cast< DeclClass *>( Mutator::mutate( decl ) );
 
-			scopeTyVars.endScope();
+			knownOffsets.endScope();
+			knownLayouts.endScope();
+			endTypeScope();
 			return ret;
 		}
@@ -1850,16 +1867,14 @@
 
 		Type * PolyGenericCalculator::mutate( PointerType *pointerType ) {
-			scopeTyVars.beginScope();
-			makeTyVarMap( pointerType, scopeTyVars );
+			beginTypeScope( pointerType );
 
 			Type *ret = Mutator::mutate( pointerType );
 
-			scopeTyVars.endScope();
+			endTypeScope();
 			return ret;
 		}
 
 		Type * PolyGenericCalculator::mutate( FunctionType *funcType ) {
-			scopeTyVars.beginScope();
-			makeTyVarMap( funcType, scopeTyVars );
+			beginTypeScope( funcType );
 
 			// make sure that any type information passed into the function is accounted for
@@ -1874,5 +1889,5 @@
 			Type *ret = Mutator::mutate( funcType );
 
-			scopeTyVars.endScope();
+			endTypeScope();
 			return ret;
 		}
