Skip to content

Commit 26d8bad

Browse files
bors[bot]japaric
andauthored
Merge #308
308: compile-fail test new Formatter move semantics r=japaric a=japaric now that the signature of `Format::format` has changed we expect that these uses of the `write!` macro and `format` method fail so let's test that the `fmt-*` examples were corrupting the defmt data stream before but now are rejected at compile time the error message for the `write!` examples could be better: it should not mention the `InternalFormatter` type, which is an implementation detail. I'll open an issue about improving the diagnostics of that macro Co-authored-by: Jorge Aparicio <jorge.aparicio@ferrous-systems.com>
2 parents dd4067f + 7cc64d3 commit 26d8bad

File tree

8 files changed

+81
-0
lines changed

8 files changed

+81
-0
lines changed

tests/ui/fmt-for-loop.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
struct S;
2+
3+
impl defmt::Format for S {
4+
fn format(&self, f: defmt::Formatter) {
5+
for _ in 0..3 {
6+
0u8.format(f);
7+
}
8+
}
9+
}
10+
11+
fn main() {}

tests/ui/fmt-for-loop.stderr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error[E0382]: use of moved value: `f`
2+
--> $DIR/fmt-for-loop.rs:6:24
3+
|
4+
4 | fn format(&self, f: defmt::Formatter) {
5+
| - move occurs because `f` has type `defmt::Formatter<'_>`, which does not implement the `Copy` trait
6+
5 | for _ in 0..3 {
7+
6 | 0u8.format(f);
8+
| ^ value moved here, in previous iteration of loop

tests/ui/fmt-twice.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
struct S;
2+
3+
impl defmt::Format for S {
4+
fn format(&self, f: defmt::Formatter) {
5+
0u8.format(f);
6+
0u16.format(f);
7+
}
8+
}
9+
10+
fn main() {}

tests/ui/fmt-twice.stderr

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0382]: use of moved value: `f`
2+
--> $DIR/fmt-twice.rs:6:21
3+
|
4+
4 | fn format(&self, f: defmt::Formatter) {
5+
| - move occurs because `f` has type `defmt::Formatter<'_>`, which does not implement the `Copy` trait
6+
5 | 0u8.format(f);
7+
| - value moved here
8+
6 | 0u16.format(f);
9+
| ^ value used here after move

tests/ui/write-for-loop.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
struct S;
2+
3+
impl defmt::Format for S {
4+
fn format(&self, f: defmt::Formatter) {
5+
for _ in 0..3 {
6+
defmt::write!(f, "hello");
7+
}
8+
}
9+
}
10+
11+
fn main() {}

tests/ui/write-for-loop.stderr

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0382]: use of moved value: `f.inner`
2+
--> $DIR/write-for-loop.rs:6:13
3+
|
4+
6 | defmt::write!(f, "hello");
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ value moved here, in previous iteration of loop
6+
|
7+
= note: move occurs because `f.inner` has type `&mut InternalFormatter`, which does not implement the `Copy` trait
8+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
9+
help: consider creating a fresh reborrow of `f.inner` here
10+
|
11+
6 | &mut *defmt::write!(f, "hello");
12+
| ^^^^^^

tests/ui/write-twice.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
struct S;
2+
3+
impl defmt::Format for S {
4+
fn format(&self, f: defmt::Formatter) {
5+
defmt::write!(f, "hello");
6+
defmt::write!(f, "world");
7+
}
8+
}
9+
10+
fn main() {}

tests/ui/write-twice.stderr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error[E0382]: use of moved value: `f.inner`
2+
--> $DIR/write-twice.rs:6:9
3+
|
4+
5 | defmt::write!(f, "hello");
5+
| -------------------------- value moved here
6+
6 | defmt::write!(f, "world");
7+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ value used here after move
8+
|
9+
= note: move occurs because `f.inner` has type `&mut InternalFormatter`, which does not implement the `Copy` trait
10+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

0 commit comments

Comments
 (0)