GOAL P1-4: 작은 규칙 일곱을 정하고, 어긋난 둘을 고친다
일곱 중 다섯은 구현이 이미 옳게 답하고 있었다 -- match 는 enum 전용이고,
enum payload 에 소유 타입을 담을 수 있고, undefined 배열은 원소 단위로
추적하지 않고, 다른 유닛에서 private 필드가 있는 리터럴은 거부되고,
by-value self 의 부분 이동도 mem.replace 가 필요하다. SPEC §7.9 에 적었다.
나머지 둘은 그냥 통과하고 있었다.
defer { return 1; } 이 받아들여졌다. 지연 블록은 함수가 무엇을 반환할지 이미
정한 뒤 스코프 정리 중에 도는데 거기서 return 이 뜻할 것이 없다.
for x in xs { xs[0] = 9; } 도 받아들여졌다. x 가 xs 안을 가리키는 참조이니
순회 몸통에서 xs 에 쓰는 것은 그 참조가 가리키는 것을 옮기는 일이다 (R6).
순회는 이제 도는 동안 대여한다. 읽기는 공유 순회에서 그대로 된다.
240/240, 35/35.
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
// ERROR:9:borrow
|
||||
unit badforwr;
|
||||
|
||||
// Walking a container borrows it: the item is a reference into it, so writing
|
||||
// the container underneath moves what that reference points at.
|
||||
|
||||
fn bad(xs: []mut i32) -> void {
|
||||
for x in xs {
|
||||
xs[0] = 9;
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// ERROR:8:cannot return from inside defer
|
||||
unit bdefret;
|
||||
|
||||
// A deferred block runs during scope cleanup, on the way out of a function
|
||||
// that has already decided what it returns.
|
||||
|
||||
fn bad() -> i32 {
|
||||
defer { return 1; }
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
unit okforrd;
|
||||
|
||||
// A shared walk still allows reading -- of the item and of the container.
|
||||
|
||||
fn ok(xs: []i32) -> i32 {
|
||||
var sum: i32 = 0;
|
||||
for x in xs {
|
||||
sum = sum + x.^ + (xs.n as i32);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
Reference in New Issue
Block a user