Index: src/ResolvExpr/Unify.cc
===================================================================
--- src/ResolvExpr/Unify.cc	(revision 43ffef1bb4f4f9fa7b91108113708cad3f6e2a61)
+++ src/ResolvExpr/Unify.cc	(revision 13ca524c4f24b4cdbee394cc254c6958f4487581)
@@ -508,30 +508,21 @@
 		handleRefType( inst, other );
 		if ( ! result ) return;
-		// Check that parameters of type unify, if any
+		// Check that parameters of types unify, if any
 		std::list< Expression* > params = inst->get_parameters();
-		if ( ! params.empty() ) {
-			std::list< TypeDecl* > *baseParams = inst->get_baseParameters();
-			if ( ! baseParams ) {
+		std::list< Expression* > otherParams = ((RefType*)other)->get_parameters();
+
+		std::list< Expression* >::const_iterator it = params.begin(), jt = otherParams.begin();
+		for ( ; it != params.end() && jt != otherParams.end(); ++it, ++jt ) {
+			TypeExpr *param = dynamic_cast< TypeExpr* >(*it);
+			assert(param && "Aggregate parameters should be type expressions");
+			TypeExpr *otherParam = dynamic_cast< TypeExpr* >(*jt);
+			assert(otherParam && "Aggregate parameters should be type expressions");
+
+			if ( ! unifyExact( param->get_type(), otherParam->get_type(), env, needAssertions, haveAssertions, openVars, WidenMode(false, false), indexer ) ) {
 				result = false;
 				return;
 			}
-			std::list< Expression* >::const_iterator it = params.begin();
-			std::list< TypeDecl* >::const_iterator baseIt = baseParams->begin();
-			while ( it != params.end() && baseIt != baseParams->end()) {
-				TypeExpr *param = dynamic_cast< TypeExpr* >(*it);
-				assert(param && "Aggregate parameters should be type expressions");
-				TypeInstType baseType(Type::Qualifiers(), (*baseIt)->get_name(), *baseIt);
-				if ( ! unifyExact( param->get_type(), &baseType, env, needAssertions, haveAssertions, openVars, WidenMode(false, false), indexer ) ) {
-					result = false;
-					return;
-				}
-				
-				++it;
-				++baseIt;
-			}
-			if ( it != params.end() || baseIt != baseParams->end() ) {
-				result = false;
-			}
 		}
+		result = ( it == params.end() && jt == otherParams.end() );
 	}
 
Index: src/SymTab/Validate.cc
===================================================================
--- src/SymTab/Validate.cc	(revision 43ffef1bb4f4f9fa7b91108113708cad3f6e2a61)
+++ src/SymTab/Validate.cc	(revision 13ca524c4f24b4cdbee394cc254c6958f4487581)
@@ -625,4 +625,12 @@
 	}
 
+	/// Clones a reference type, replacing any parameters it may have with a clone of the provided list
+	template< typename GenericInstType >
+	GenericInstType *cloneWithParams( GenericInstType *refType, const std::list< Expression* >& params ) {
+		GenericInstType *clone = refType->clone();
+		clone->get_parameters().clear();
+		cloneAll( params, clone->get_parameters() );
+		return clone;
+	}
 
 	Declaration *makeStructAssignment( StructDecl *aggregateDecl, StructInstType *refType, unsigned int functionNesting ) {
@@ -631,15 +639,18 @@
 		// Make function polymorphic in same parameters as generic struct, if applicable
 		std::list< TypeDecl* >& genericParams = aggregateDecl->get_parameters();
+		std::list< Expression* > structParams;  // List of matching parameters to put on types
 		for ( std::list< TypeDecl* >::const_iterator param = genericParams.begin(); param != genericParams.end(); ++param ) {
-			assignType->get_forall().push_back( (*param)->clone() );
+			TypeDecl *typeParam = (*param)->clone();
+			assignType->get_forall().push_back( typeParam );
+			structParams.push_back( new TypeExpr( new TypeInstType( Type::Qualifiers(), typeParam->get_name(), typeParam ) ) );
 		}
   
-		ObjectDecl *returnVal = new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, refType->clone(), 0 );
+		ObjectDecl *returnVal = new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, structParams ), 0 );
 		assignType->get_returnVals().push_back( returnVal );
   
-		ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), refType->clone() ), 0 );
+		ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), cloneWithParams( refType, structParams ) ), 0 );
 		assignType->get_parameters().push_back( dstParam );
   
-		ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, refType, 0 );
+		ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, structParams ), 0 );
 		assignType->get_parameters().push_back( srcParam );
 
@@ -677,12 +688,21 @@
 	Declaration *makeUnionAssignment( UnionDecl *aggregateDecl, UnionInstType *refType, unsigned int functionNesting ) {
 		FunctionType *assignType = new FunctionType( Type::Qualifiers(), false );
-  
-		ObjectDecl *returnVal = new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, refType->clone(), 0 );
+
+		// Make function polymorphic in same parameters as generic union, if applicable
+		std::list< TypeDecl* >& genericParams = aggregateDecl->get_parameters();
+		std::list< Expression* > unionParams;  // List of matching parameters to put on types
+		for ( std::list< TypeDecl* >::const_iterator param = genericParams.begin(); param != genericParams.end(); ++param ) {
+			TypeDecl *typeParam = (*param)->clone();
+			assignType->get_forall().push_back( typeParam );
+			unionParams.push_back( new TypeExpr( new TypeInstType( Type::Qualifiers(), typeParam->get_name(), typeParam ) ) );
+		}
+  
+		ObjectDecl *returnVal = new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, unionParams ), 0 );
 		assignType->get_returnVals().push_back( returnVal );
   
-		ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), refType->clone() ), 0 );
+		ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), cloneWithParams( refType, unionParams ) ), 0 );
 		assignType->get_parameters().push_back( dstParam );
   
-		ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, refType, 0 );
+		ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, unionParams ), 0 );
 		assignType->get_parameters().push_back( srcParam );
   
@@ -695,5 +715,5 @@
 		copy->get_args().push_back( new VariableExpr( dstParam ) );
 		copy->get_args().push_back( new AddressExpr( new VariableExpr( srcParam ) ) );
-		copy->get_args().push_back( new SizeofExpr( refType->clone() ) );
+		copy->get_args().push_back( new SizeofExpr( cloneWithParams( refType, unionParams ) ) );
 
 		assignDecl->get_statements()->get_kids().push_back( new ExprStmt( noLabels, copy ) );
@@ -714,7 +734,7 @@
 	void AutogenerateRoutines::visit( StructDecl *structDecl ) {
 		if ( ! structDecl->get_members().empty() && structsDone.find( structDecl->get_name() ) == structsDone.end() ) {
-			StructInstType *structInst = new StructInstType( Type::Qualifiers(), structDecl->get_name() );
-			structInst->set_baseStruct( structDecl );
-			declsToAdd.push_back( makeStructAssignment( structDecl, structInst, functionNesting ) );
+			StructInstType structInst( Type::Qualifiers(), structDecl->get_name() );
+			structInst.set_baseStruct( structDecl );
+			declsToAdd.push_back( makeStructAssignment( structDecl, &structInst, functionNesting ) );
 			structsDone.insert( structDecl->get_name() );
 		} // if
@@ -723,7 +743,7 @@
 	void AutogenerateRoutines::visit( UnionDecl *unionDecl ) {
 		if ( ! unionDecl->get_members().empty() ) {
-			UnionInstType *unionInst = new UnionInstType( Type::Qualifiers(), unionDecl->get_name() );
-			unionInst->set_baseUnion( unionDecl );
-			declsToAdd.push_back( makeUnionAssignment( unionDecl, unionInst, functionNesting ) );
+			UnionInstType unionInst( Type::Qualifiers(), unionDecl->get_name() );
+			unionInst.set_baseUnion( unionDecl );
+			declsToAdd.push_back( makeUnionAssignment( unionDecl, &unionInst, functionNesting ) );
 		} // if
 	}
