diff --git a/frontends/ast/ast.cc b/frontends/ast/ast.cc
index c8183580b..1c0a8b34d 100644
--- a/frontends/ast/ast.cc
+++ b/frontends/ast/ast.cc
@@ -1511,6 +1511,7 @@ RTLIL::IdString AstModule::derive(RTLIL::Design *design, const dict<RTLIL::IdStr
 		}
 
 	} else {
+		modname = new_modname;
 		log("Found cached RTLIL representation for module `%s'.\n", modname.c_str());
 	}