@@ -261,29 +261,40 @@ static auto BuildImplDecl(Context& context, Parse::AnyImplDeclId node_id,
261261}
262262
263263auto HandleParseNode (Context& context, Parse::ImplDeclId node_id) -> bool {
264- BuildImplDecl (context, node_id, /* is_definition=*/ false );
264+ auto [impl_id, impl_decl_id] =
265+ BuildImplDecl (context, node_id, /* is_definition=*/ false );
266+ auto & impl = context.impls ().Get (impl_id);
267+
265268 context.decl_name_stack ().PopScope ();
269+
270+ // Impl definitions are required in the same file as the declaration. We skip
271+ // this requirement if we've already issued an invalid redeclaration error, or
272+ // there is an error that would prevent the impl from being legal to define.
273+ if (impl.witness_id != SemIR::ErrorInst::InstId) {
274+ context.definitions_required_by_decl ().push_back (impl_decl_id);
275+ }
276+
266277 return true ;
267278}
268279
269280auto HandleParseNode (Context& context, Parse::ImplDefinitionStartId node_id)
270281 -> bool {
271282 auto [impl_id, impl_decl_id] =
272283 BuildImplDecl (context, node_id, /* is_definition=*/ true );
273- auto & impl_info = context.impls ().Get (impl_id);
284+ auto & impl = context.impls ().Get (impl_id);
274285
275- CARBON_CHECK (!impl_info .has_definition_started ());
276- impl_info .definition_id = impl_decl_id;
277- impl_info .scope_id =
286+ CARBON_CHECK (!impl .has_definition_started ());
287+ impl .definition_id = impl_decl_id;
288+ impl .scope_id =
278289 context.name_scopes ().Add (impl_decl_id, SemIR::NameId::None,
279290 context.decl_name_stack ().PeekParentScopeId ());
280291
281292 context.scope_stack ().PushForEntity (
282- impl_decl_id, impl_info .scope_id ,
283- context.generics ().GetSelfSpecific (impl_info .generic_id ));
284- StartGenericDefinition (context, impl_info .generic_id );
293+ impl_decl_id, impl .scope_id ,
294+ context.generics ().GetSelfSpecific (impl .generic_id ));
295+ StartGenericDefinition (context, impl .generic_id );
285296 // This requires that the facet type is complete.
286- ImplWitnessStartDefinition (context, impl_info );
297+ ImplWitnessStartDefinition (context, impl );
287298 context.inst_block_stack ().Push ();
288299 context.node_stack ().Push (node_id, impl_id);
289300
@@ -296,7 +307,7 @@ auto HandleParseNode(Context& context, Parse::ImplDefinitionStartId node_id)
296307 //
297308 // We may need to track a list of instruction blocks here, as we do for a
298309 // function.
299- impl_info .body_block_id = context.inst_block_stack ().PeekOrAdd ();
310+ impl .body_block_id = context.inst_block_stack ().PeekOrAdd ();
300311 return true ;
301312}
302313
@@ -307,9 +318,9 @@ auto HandleParseNode(Context& context, Parse::ImplDefinitionId /*node_id*/)
307318
308319 FinishImplWitness (context, impl_id);
309320
310- auto & impl_info = context.impls ().Get (impl_id);
311- impl_info .defined = true ;
312- FinishGenericDefinition (context, impl_info .generic_id );
321+ auto & impl = context.impls ().Get (impl_id);
322+ impl .defined = true ;
323+ FinishGenericDefinition (context, impl .generic_id );
313324
314325 context.inst_block_stack ().Pop ();
315326 // The decl_name_stack and scopes are popped by `ProcessNodeIds`.
0 commit comments