Skip to content

Commit c713931

Browse files
committed
early-out
1 parent 06634ad commit c713931

File tree

1 file changed

+57
-56
lines changed

1 file changed

+57
-56
lines changed

toolchain/check/impl.cpp

Lines changed: 57 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -508,71 +508,72 @@ auto GetOrAddImpl(Context& context, SemIR::LocId loc_id,
508508
// This is a redeclaration of another impl, now held in `impl_id`.
509509
auto& prev_impl = context.impls().Get(impl_id);
510510
FinishGenericRedecl(context, prev_impl.generic_id);
511-
} else {
512-
// This is a new declaration (possibly with an attached definition). Create
513-
// a new `impl_id`, filling the missing generic and witness in the provided
514-
// `Impl`.
515-
impl.generic_id = BuildGeneric(context, impl.latest_decl_id());
516-
517-
// Due to lack of an instruction to set to ErrorInst, an `InterfaceId::None`
518-
// indicates that the interface could not be identified and an error was
519-
// diagnosed. If there's any error in the construction of the impl, then the
520-
// witness can't be constructed. We set it to ErrorInt to make the impl
521-
// unusable for impl lookup.
522-
if (!impl.interface.interface_id.has_value() || invalid_redecl ||
523-
impl.self_id == SemIR::ErrorInst::TypeInstId ||
524-
impl.constraint_id == SemIR::ErrorInst::TypeInstId) {
511+
return impl_id;
512+
}
513+
514+
// This is a new declaration (possibly with an attached definition). Create a
515+
// new `impl_id`, filling the missing generic and witness in the provided
516+
// `Impl`.
517+
518+
impl.generic_id = BuildGeneric(context, impl.latest_decl_id());
519+
520+
// Due to lack of an instruction to set to ErrorInst, an `InterfaceId::None`
521+
// indicates that the interface could not be identified and an error was
522+
// diagnosed. If there's any error in the construction of the impl, then the
523+
// witness can't be constructed. We set it to ErrorInt to make the impl
524+
// unusable for impl lookup.
525+
if (!impl.interface.interface_id.has_value() || invalid_redecl ||
526+
impl.self_id == SemIR::ErrorInst::TypeInstId ||
527+
impl.constraint_id == SemIR::ErrorInst::TypeInstId) {
528+
impl.witness_id = SemIR::ErrorInst::InstId;
529+
// TODO: We might also want to mark that the name scope for the impl has an
530+
// error -- at least once we start making name lookups within the impl also
531+
// look into the facet (eg, so you can name associated constants from within
532+
// the impl).
533+
}
534+
535+
if (is_definition && impl.witness_id != SemIR::ErrorInst::InstId) {
536+
if (!RequireCompleteFacetTypeForImplDefinition(
537+
context, SemIR::LocId(impl.latest_decl_id()), impl.constraint_id)) {
525538
impl.witness_id = SemIR::ErrorInst::InstId;
526-
// TODO: We might also want to mark that the name scope for the impl has
527-
// an error -- at least once we start making name lookups within the
528-
// impl also look into the facet (eg, so you can name associated
529-
// constants from within the impl).
530539
}
540+
}
531541

532-
if (is_definition && impl.witness_id != SemIR::ErrorInst::InstId) {
533-
if (!RequireCompleteFacetTypeForImplDefinition(
534-
context, SemIR::LocId(impl.latest_decl_id()),
535-
impl.constraint_id)) {
536-
impl.witness_id = SemIR::ErrorInst::InstId;
537-
}
538-
}
542+
if (impl.witness_id != SemIR::ErrorInst::InstId) {
543+
// This makes either a placeholder witness or a full witness table. The full
544+
// witness table is deferred to the impl definition unless the declaration
545+
// uses rewrite constraints to set values of associated constants in the
546+
// interface.
547+
impl.witness_id = ImplWitnessForDeclaration(context, impl, is_definition);
548+
}
539549

540-
if (impl.witness_id != SemIR::ErrorInst::InstId) {
541-
// This makes either a placeholder witness or a full witness table. The
542-
// full witness table is deferred to the impl definition unless the
543-
// declaration uses rewrite constraints to set values of associated
544-
// constants in the interface.
545-
impl.witness_id = ImplWitnessForDeclaration(context, impl, is_definition);
546-
}
550+
FinishGenericDecl(context, SemIR::LocId(impl.latest_decl_id()),
551+
impl.generic_id);
552+
// From here on, use the `Impl` from the `ImplStore` instead of `impl` in
553+
// order to make and see any changes to the `Impl`.
554+
impl_id = context.impls().Add(impl);
555+
lookup_bucket_ref.push_back(impl_id);
556+
if (impl.witness_id != SemIR::ErrorInst::InstId) {
557+
AssignImplIdInWitness(context, impl_id, impl.witness_id);
558+
}
547559

548-
FinishGenericDecl(context, SemIR::LocId(impl.latest_decl_id()),
549-
impl.generic_id);
550-
// From here on, use the `Impl` from the `ImplStore` instead of `impl`
551-
// in order to make and see any changes to the `Impl`.
552-
impl_id = context.impls().Add(impl);
553-
lookup_bucket_ref.push_back(impl_id);
554-
if (impl.witness_id != SemIR::ErrorInst::InstId) {
555-
AssignImplIdInWitness(context, impl_id, impl.witness_id);
556-
}
560+
auto& stored_impl = context.impls().Get(impl_id);
557561

558-
auto& stored_impl = context.impls().Get(impl_id);
562+
// Look to see if there are any generic bindings on the `impl` declaration
563+
// that are not deducible. If so, and the `impl` does not actually use all its
564+
// generic bindings, and will never be matched. This should be diagnossed to
565+
// the user.
566+
if (!VerifyAllGenericBindingsUsed(context, loc_id, implicit_params_loc_id,
567+
stored_impl)) {
568+
FillImplWitnessWithErrors(context, stored_impl);
569+
}
559570

560-
// Look to see if there are any generic bindings on the `impl` declaration
561-
// that are not deducible. If so, and the `impl` does not actually use all
562-
// its generic bindings, and will never be matched. This should be
563-
// diagnossed to the user.
564-
if (!VerifyAllGenericBindingsUsed(context, loc_id, implicit_params_loc_id,
565-
stored_impl)) {
571+
if (extend_node.has_value()) {
572+
if (!ApplyExtendImplAs(context, loc_id, stored_impl, extend_node,
573+
implicit_params_loc_id)) {
574+
// Signal the erroneous impl should not be used in lookup.
566575
FillImplWitnessWithErrors(context, stored_impl);
567576
}
568-
569-
if (extend_node.has_value()) {
570-
if (!ApplyExtendImplAs(context, loc_id, stored_impl, extend_node,
571-
implicit_params_loc_id)) {
572-
// Signal the erroneous impl should not be used in lookup.
573-
FillImplWitnessWithErrors(context, stored_impl);
574-
}
575-
}
576577
}
577578

578579
return impl_id;

0 commit comments

Comments
 (0)