ADR 0041: Type tests on errors stay dynamic, and warn
Status: Accepted (2026-10-25). Confirms 0016 and 0018; adds a warning. Not breaking.
Context
Section titled “Context”The same question gets two answers, depending on how it is asked:
load(7) catch (err) { when err { is *NotFound -> "not found" // false: err is a wrapped *NotFound else -> "other" }}load(7) catch (e: *NotFound) { … } // matches: errors.AsType sees through wrappingADR 0016 made is a type assertion that sees the dynamic type only, and ADR
0018 made typed catch use errors.AsType on purpose. Each choice is right on
its own. But Lyth encourages wrapping (err.wrap(…)), so err is T and
when err { is T -> } on an error are almost always a bug in waiting: they
pass the tests that throw T directly and fail in production, where the
error arrives wrapped. The pre-announcement review listed two options: change
is, or warn. Working through the first showed it does not hold.
- The smart cast would rebind the name. After
if err is *NotFound,erris smart-cast to*NotFound. Witherrors.AsType, the value found is the wrapped one, noterr. So inside the brancherrwould silently mean a different value:err.Error()would printnot found 7instead ofload 7: not found 7, and loggingerrwould lose the context. - One spelling, two meanings.
x is Twould test the dynamic type for ananyand the chain for anerror. That is the hidden difference ADR 0016 rejected.
Decision
Section titled “Decision”-
isstays a type assertion.is,!is,as,as?, andwhen … { is T -> }keep ADR 0016’s meaning on every operand. -
New warning, W0007 (the next free warning code). It fires when the operand’s static type is
error(or an interface that embeds it) andTis a type that implementserror:warning[W0007]: a type test sees only the dynamic type of err, so a wrapped *NotFound does not match hint: to match through wrapping, handle the call with catch (e: *NotFound) { … }, or bind it: e, ok := errors.AsType*NotFound hint: to test the dynamic type on purpose, bind the error as any first: v: any = err
It is a hint, not a fix: the rewrite depends on the code around it.
-
Silencing it. To test the dynamic type deliberately, for example in an
UnwraporIsmethod, bind the error asanyfirst:v: any = err, thenv is *NotFound. That silences the warning and says so in the code. -
Later,
whenover errors. Awhenform with chain semantics and its own binding stays a later, non-breaking feature. Its spelling needs to show that the bound value is the wrapped error, not the subject.
Alternatives
Section titled “Alternatives”ison errors useserrors.AsType. It reads as intended in the common case, but it is unsound for the reason above: the smart-cast name would denote a different value than the one tested. The rewrite would also change the behaviour of code that compiles today.- Forbid
ison errors. This is too strong:Unwrap/Isimplementations and errors from code that never wraps have legitimate uses. - Status quo. The trap stays silent.
Consequences
Section titled “Consequences”The trap becomes a warning with the exact alternative, and nothing changes
meaning. This ADR is not breaking, so it does not block the announcement. It is
listed with the decisions because the alternative (changing is) would have
been breaking, and the warning should ship before people learn the pattern.
Implementation
Section titled “Implementation”- Checker. Add the warning in the type-test checking of
internal/check. - Tests.
check/errortest_warn:is,as?, andwhen … isonerror, onany(no warning), and the silencing form.- The guide’s error section and the tour gain a sentence.