carbon-lang icon indicating copy to clipboard operation
carbon-lang copied to clipboard

explorer crashes on impl of 'unknown' invalid type expression

Open pk19604014 opened this issue 3 years ago • 3 comments

package P api;

class LazyNe {
  impl LazyNe.Make as true {
  }

  fn Make();
}

fn Main() -> i32 {
  return 0;
}

.../include/c++/v1/optional:911: _LIBCPP_ASSERT 'this->has_value()' failed. optional operator* called on a disengaged value

Moving fn Make() declaration above impl results in COMPILATION ERROR: /tmp/crash.carbon:6: Expected a type, but got fun<Make>

pk19604014 avatar Jul 15 '22 14:07 pk19604014

Problem is at: https://github.com/carbon-language/carbon-lang/blob/trunk/explorer/interpreter/type_checker.cpp#L4283

The SimpleMemberAccessExpression in impl is typechecked before the class is typed checked and when FindMixedMemberAndType() is called in it, it assumes the class is, and references the static_type of a member which crashes the program.

Moving fn Make() up above the impl, crash disappears and just gives a compilation error.

gysddn avatar Sep 11 '22 13:09 gysddn

@josh11b Loooking through https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/generics/details.md#external-impl, is impl LazyNe.Make as true actually invalid because LazyNe.Make shouldn't be allowed there without it being a external impl outside a class? Is this even valid syntax?

(is there concise documentation on what the allowed syntax is for generics?)

jonmeow avatar Sep 11 '22 16:09 jonmeow

@josh11b Loooking through https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/generics/details.md#external-impl, is impl LazyNe.Make as true actually invalid because LazyNe.Make shouldn't be allowed there without it being a external impl outside a class? Is this even valid syntax?

(is there concise documentation on what the allowed syntax is for generics?)

The design docs cover the syntax but not in a concise way, since that is not the primary focus. The currently defined rule is that impl LazyNe.Make as ... is syntactically legal outside a class definition, but is semantically invalid unless there is also a declaration that LazyNe.Make is implemented inside its class definition. I believe this is documented in the "forward declaration" section: https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/generics/details.md#forward-declarations-and-cyclic-references . Presumably the as true part is illegal since true is not an interface or constraint, though.

josh11b avatar Sep 12 '22 17:09 josh11b