Index: src/GenPoly/Lvalue.cc
===================================================================
--- src/GenPoly/Lvalue.cc	(revision aa19ccf5f75077e36bab7894ba1178cf77c35693)
+++ src/GenPoly/Lvalue.cc	(revision 02ad3f51cdab9d6d538fe31edff58e8eb08defc1)
@@ -17,4 +17,6 @@
 
 #include "Lvalue.h"
+
+#include "GenPoly.h"
 
 #include "SynTree/Declaration.h"
@@ -63,10 +65,8 @@
 
 	namespace {
-		bool isLvalueRet( FunctionType *function ) {
-			if ( ! function->get_returnVals().empty() ) {
-				return function->get_returnVals().front()->get_type()->get_isLvalue();
-			} else {
-				return false;
-			} // if
+		Type* isLvalueRet( FunctionType *function ) {
+			if ( function->get_returnVals().empty() ) return 0;
+			Type *ty = function->get_returnVals().front()->get_type();
+			return ty->get_isLvalue() ? ty : 0;
 		}
 
@@ -107,10 +107,16 @@
 			assert( function );
 
-			std::string typeName;
-			if ( isLvalueRet( function ) && ! isIntrinsicApp( appExpr ) ) {
+			Type *funType = isLvalueRet( function );
+			if ( funType && ! isIntrinsicApp( appExpr ) ) {
+				Expression *expr = appExpr;
+				Type *appType = appExpr->get_results().front();
+				if ( isPolyType( funType ) ) {
+					// make sure cast for polymorphic type is inside dereference
+					expr = new CastExpr( appExpr, new PointerType( Type::Qualifiers(), appType->clone() ) );
+				}
 				UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
-				deref->get_results().push_back( appExpr->get_results().front() );
-				appExpr->get_results().front() = new PointerType( Type::Qualifiers(), deref->get_results().front()->clone() );
-				deref->get_args().push_back( appExpr );
+				deref->get_results().push_back( appType->clone() );
+				appExpr->get_results().front() = new PointerType( Type::Qualifiers(), appType );
+				deref->get_args().push_back( expr );
 				return deref;
 			} else {
