jakt icon indicating copy to clipboard operation
jakt copied to clipboard

codegen: Calls to emit variant access for non-generic enum matches could be omitted

Open ADKaster opened this issue 3 years ago • 0 comments

For example, looking at Error::MessageSeverity

enum MessageSeverity {
    Hint
    Error
    public function name(this) throws => match this {
        Hint => "Hint"
        Error => "Error"
    }
    public function ansi_color_code(this) throws => match this {
        Hint => "94"  // Bright Blue
        Error => "31" // Red
    }
}

The code generated for ansi_color_code looks like so:

ErrorOr<String> error::MessageSeverity::ansi_color_code() const {
	{
		return (JAKT_RESOLVE_EXPLICIT_VALUE_OR_CONTROL_FLOW_RETURN_ONLY(([&]() -> JaktInternal::ExplicitValueOrControlFlow<String, ErrorOr<String>> {
			auto&& __jakt_match_variant = *this;
			switch (__jakt_match_variant.index()) {
			case 0: {
-->			auto&& __jakt_match_value = __jakt_match_variant.template get<typename error::MessageSeverity::Hint>();
				return JaktInternal::ExplicitValue(String("94"));
			};/*case end*/
			case 1: {
-->    		auto&& __jakt_match_value = __jakt_match_variant.template get<typename error::MessageSeverity::Error>();
				return JaktInternal::ExplicitValue(String("31"));
			};/*case end*/
			default: VERIFY_NOT_REACHED();
			}/*switch end*/
			}()
				)));
	}
}

Each of the temporary vairables named __jakt_match_value could be omitted, because we know that the enum's values are not accessed. These cause the warning -Wunused-value when enough compiler warnings are turned on.

ADKaster avatar Aug 08 '22 01:08 ADKaster