diff --git i/gcc/cp/module.cc w/gcc/cp/module.cc
index 8f9c7940ef8..6741ae03ee7 100644
--- i/gcc/cp/module.cc
+++ w/gcc/cp/module.cc
@@ -2567,7 +2567,7 @@ public:
     void add_class_entities (vec<tree, va_gc> *);
 
   public:    
-    void find_dependencies ();
+    void find_dependencies (module_state *);
     bool finalize_dependencies ();
     vec<depset *> connect ();
   };
@@ -5898,7 +5898,8 @@ trees_out::core_vals (tree t)
       if (!DECL_TEMPLATE_PARM_P (t))
 	WT (t->decl_minimal.context);
 
-      state->write_location (*this, t->decl_minimal.locus);
+      if (state)
+	state->write_location (*this, t->decl_minimal.locus);
     }
 
   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
@@ -6001,7 +6002,8 @@ trees_out::core_vals (tree t)
 
   if (CODE_CONTAINS_STRUCT (code, TS_EXP))
     {
-      state->write_location (*this, t->exp.locus);
+      if (state)
+	state->write_location (*this, t->exp.locus);
 
       /* Walk in forward order, as (for instance) REQUIRES_EXPR has a
          bunch of unscoped parms on its first operand.  It's safer to
@@ -6140,9 +6142,12 @@ trees_out::core_vals (tree t)
 
       /* Miscellaneous common nodes.  */
     case BLOCK:
-      state->write_location (*this, t->block.locus);
-      state->write_location (*this, t->block.end_locus);
-      
+      if (state)
+	{
+	  state->write_location (*this, t->block.locus);
+	  state->write_location (*this, t->block.end_locus);
+	}
+
       /* DECL_LOCAL_DECL_P decls are first encountered here and
          streamed by value.  */
       chained_decls (t->block.vars);
@@ -6183,7 +6188,8 @@ trees_out::core_vals (tree t)
 	/* The ompcode is serialized in start.  */
 	if (streaming_p ())
 	  WU (t->omp_clause.subcode.map_kind);
-	state->write_location (*this, t->omp_clause.locus);
+	if (state)
+	  state->write_location (*this, t->omp_clause.locus);
 
 	unsigned len = omp_clause_num_ops[OMP_CLAUSE_CODE (t)];
 	for (unsigned ix = 0; ix != len; ix++)
@@ -6270,8 +6276,9 @@ trees_out::core_vals (tree t)
       WT (((lang_tree_node *)t)->lambda_expression.extra_scope);
       /* pending_proxies is a parse-time thing.  */
       gcc_assert (!((lang_tree_node *)t)->lambda_expression.pending_proxies);
-      state->write_location
-	(*this, ((lang_tree_node *)t)->lambda_expression.locus);
+      if (state)
+	state->write_location
+	  (*this, ((lang_tree_node *)t)->lambda_expression.locus);
       if (streaming_p ())
 	{
 	  WU (((lang_tree_node *)t)->lambda_expression.default_capture_mode);
@@ -6291,8 +6298,9 @@ trees_out::core_vals (tree t)
     case STATIC_ASSERT:
       WT (((lang_tree_node *)t)->static_assertion.condition);
       WT (((lang_tree_node *)t)->static_assertion.message);
-      state->write_location
-	(*this, ((lang_tree_node *)t)->static_assertion.location);
+      if (state)
+	state->write_location
+	  (*this, ((lang_tree_node *)t)->static_assertion.location);
       break;
 
     case TEMPLATE_DECL:
@@ -6324,7 +6332,8 @@ trees_out::core_vals (tree t)
 		WT (m.binfo);
 		WT (m.decl);
 		WT (m.diag_decl);
-		state->write_location (*this, m.loc);
+		if (state)
+		  state->write_location (*this, m.loc);
 	      }
 	  }
       }
@@ -13159,9 +13168,9 @@ depset::hash::add_mergeable (depset *mergeable)
    entries on the same binding that need walking.  */
 
 void
-depset::hash::find_dependencies ()
+depset::hash::find_dependencies (module_state *module)
 {
-  trees_out walker (NULL, NULL, *this);
+  trees_out walker (NULL, module, *this);
   vec<depset *> unreached;
   unreached.create (worklist.length ());
 
@@ -13547,7 +13556,7 @@ sort_cluster (depset::hash *original, depset *scc[], unsigned size)
   gcc_checking_assert (use_lwm <= bind_lwm);
   dump (dumper::MERGE) && dump ("Ordering %u/%u depsets", use_lwm, size);
 
-  table.find_dependencies ();
+  table.find_dependencies (nullptr);
 
   vec<depset *> order = table.connect ();
   gcc_checking_assert (order.length () == use_lwm);
@@ -17571,7 +17580,7 @@ module_state::write (elf_out *to, cpp_reader *reader)
     }
 
   /* Now join everything up.  */
-  table.find_dependencies ();
+  table.find_dependencies (this);
 
   if (!table.finalize_dependencies ())
     {
