exhaust: match exhaustiveness와 도달 불가 팔 검사
철학 1이 나열한 다섯 항목 중 비어 있던 자리를 채운다. Maranget의 usefulness 알고리즘으로 반례를 만들어 "빠진 경우"를 이름으로 말한다 — 중첩된 자리의 반례도 찾는다(Some(Rect(_, _))). 이 검사가 왜 지금 필요한가: interface hash가 enum 정의 본문을 입력으로 삼는 이유가 바로 이것이다. upstream에 variant가 하나 늘면 downstream의 match가 깨져야 하는데, 검사가 없으면 깨질 것이 없다. 다음 마일스톤(모듈 경계를 넘는 재검사)의 핵심 시나리오가 여기에 걸려 있다. 테스트로 그 시나리오를 직접 고정했다 — 같은 코드가 variant 둘일 때는 통과하고 셋이 되면 깨진다. 구현 중 한 번 틀렸다. 리터럴 패턴을 와일드카드로 줄였더니 Int 리터럴 두 개로 match가 완전해져 버렸다. 리터럴은 인자 없는 생성자이고, 타입의 생성자 집합이 무한하므로 리터럴만으로는 결코 완전해지지 않는다. 생성자 집합을 알 수 없는 타입(외부 타입, 미지수)은 검사하지 않는다. 모르는 것을 위반이라고 말하지 않는다. definite init은 문법이 이미 보장한다는 것을 문서에 적었다 — let이 항상 초기화식을 요구하므로 별도 검사가 필요 없다. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019ZVDeU6KLuUVL3gs18Hm3E
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
// 11. exhaustiveness 검사기가 거부해야 하는 코드
|
||||
//
|
||||
// 철학 1의 대표 항목이자, interface hash가 enum 정의 본문을 입력으로 삼는 이유다.
|
||||
// upstream에 variant가 하나 늘면 downstream의 match가 깨져야 하는데,
|
||||
// 이 검사가 없으면 깨질 것이 없다.
|
||||
|
||||
pub enum Shape {
|
||||
Circle(Int),
|
||||
Rect(Int, Int),
|
||||
Point,
|
||||
}
|
||||
|
||||
// --- 통과해야 하는 것 ---
|
||||
|
||||
pub fn area(s: Shape) -> Int {
|
||||
match s {
|
||||
Circle(r) => r * r,
|
||||
Rect(w, h) => w * h,
|
||||
Point => 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_wildcard(s: Shape) -> Int {
|
||||
match s {
|
||||
Circle(r) => r,
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn nested_full(o: Option[Shape]) -> Int {
|
||||
match o {
|
||||
Some(Circle(r)) => r,
|
||||
Some(Rect(w, h)) => w * h,
|
||||
Some(Point) => 0,
|
||||
None => 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn results(r: Result[Int, Int]) -> Int {
|
||||
match r {
|
||||
Ok(n) => n,
|
||||
Err(e) => e,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn flags(b: Bool) -> Int {
|
||||
match b {
|
||||
true => 1,
|
||||
false => 0,
|
||||
}
|
||||
}
|
||||
|
||||
// --- 여기서부터 전부 오류다 ---
|
||||
|
||||
// [E-match-missing] variant 하나가 빠졌다
|
||||
pub fn missing_variant(s: Shape) -> Int {
|
||||
match s {
|
||||
Circle(r) => r,
|
||||
Rect(w, h) => w * h,
|
||||
}
|
||||
}
|
||||
|
||||
// [E-match-missing] Bool도 생성자 집합이 유한하다
|
||||
pub fn missing_false(b: Bool) -> Int {
|
||||
match b {
|
||||
true => 1,
|
||||
}
|
||||
}
|
||||
|
||||
// [E-match-missing] Option
|
||||
pub fn missing_none(o: Option[Int]) -> Int {
|
||||
match o {
|
||||
Some(n) => n,
|
||||
}
|
||||
}
|
||||
|
||||
// [E-match-missing] 중첩된 자리에서 빠진 경우도 찾는다
|
||||
pub fn missing_nested(o: Option[Shape]) -> Int {
|
||||
match o {
|
||||
Some(Circle(r)) => r,
|
||||
None => 0,
|
||||
}
|
||||
}
|
||||
|
||||
// [E-match-missing] Int 리터럴은 생성자 집합이 무한하다
|
||||
pub fn missing_literal(n: Int) -> Int {
|
||||
match n {
|
||||
0 => 1,
|
||||
1 => 2,
|
||||
}
|
||||
}
|
||||
|
||||
// [E-match-unreachable] 앞의 와일드카드에 가린다
|
||||
pub fn shadowed(s: Shape) -> Int {
|
||||
match s {
|
||||
_ => 0,
|
||||
Point => 1,
|
||||
}
|
||||
}
|
||||
|
||||
// [E-match-unreachable] 같은 생성자가 두 번
|
||||
pub fn duplicated(s: Shape) -> Int {
|
||||
match s {
|
||||
Circle(r) => r,
|
||||
Circle(x) => x,
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user