From 64d98f8ee037282c35007b64c2649055c56af1db Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:19:03 +0200 Subject: Merging upstream version 1.68.2+dfsg1. Signed-off-by: Daniel Baumann --- tests/ui/str/str-array-assignment.rs | 11 ++++ tests/ui/str/str-array-assignment.stderr | 45 ++++++++++++++++ tests/ui/str/str-as-char.fixed | 5 ++ tests/ui/str/str-as-char.rs | 5 ++ tests/ui/str/str-as-char.stderr | 13 +++++ tests/ui/str/str-concat-on-double-ref.rs | 7 +++ tests/ui/str/str-concat-on-double-ref.stderr | 18 +++++++ tests/ui/str/str-escape.rs | 11 ++++ tests/ui/str/str-escape.stderr | 21 ++++++++ tests/ui/str/str-idx.rs | 7 +++ tests/ui/str/str-idx.stderr | 54 +++++++++++++++++++ tests/ui/str/str-lit-type-mismatch.rs | 5 ++ tests/ui/str/str-lit-type-mismatch.stderr | 49 +++++++++++++++++ tests/ui/str/str-mut-idx.rs | 17 ++++++ tests/ui/str/str-mut-idx.stderr | 78 ++++++++++++++++++++++++++++ tests/ui/str/str-overrun.rs | 10 ++++ 16 files changed, 356 insertions(+) create mode 100644 tests/ui/str/str-array-assignment.rs create mode 100644 tests/ui/str/str-array-assignment.stderr create mode 100644 tests/ui/str/str-as-char.fixed create mode 100644 tests/ui/str/str-as-char.rs create mode 100644 tests/ui/str/str-as-char.stderr create mode 100644 tests/ui/str/str-concat-on-double-ref.rs create mode 100644 tests/ui/str/str-concat-on-double-ref.stderr create mode 100644 tests/ui/str/str-escape.rs create mode 100644 tests/ui/str/str-escape.stderr create mode 100644 tests/ui/str/str-idx.rs create mode 100644 tests/ui/str/str-idx.stderr create mode 100644 tests/ui/str/str-lit-type-mismatch.rs create mode 100644 tests/ui/str/str-lit-type-mismatch.stderr create mode 100644 tests/ui/str/str-mut-idx.rs create mode 100644 tests/ui/str/str-mut-idx.stderr create mode 100644 tests/ui/str/str-overrun.rs (limited to 'tests/ui/str') diff --git a/tests/ui/str/str-array-assignment.rs b/tests/ui/str/str-array-assignment.rs new file mode 100644 index 000000000..323eefb38 --- /dev/null +++ b/tests/ui/str/str-array-assignment.rs @@ -0,0 +1,11 @@ +fn main() { + let s = "abc"; + let t = if true { s[..2] } else { s }; + //~^ ERROR `if` and `else` have incompatible types + let u: &str = if true { s[..2] } else { s }; + //~^ ERROR mismatched types + let v = s[..2]; + //~^ ERROR the size for values of type + let w: &str = s[..2]; + //~^ ERROR mismatched types +} diff --git a/tests/ui/str/str-array-assignment.stderr b/tests/ui/str/str-array-assignment.stderr new file mode 100644 index 000000000..c23400a1d --- /dev/null +++ b/tests/ui/str/str-array-assignment.stderr @@ -0,0 +1,45 @@ +error[E0308]: `if` and `else` have incompatible types + --> $DIR/str-array-assignment.rs:3:37 + | +LL | let t = if true { s[..2] } else { s }; + | ------ ^ expected `str`, found `&str` + | | + | expected because of this + +error[E0308]: mismatched types + --> $DIR/str-array-assignment.rs:5:27 + | +LL | let u: &str = if true { s[..2] } else { s }; + | ^^^^^^ + | | + | expected `&str`, found `str` + | help: consider borrowing here: `&s[..2]` + +error[E0277]: the size for values of type `str` cannot be known at compilation time + --> $DIR/str-array-assignment.rs:7:7 + | +LL | let v = s[..2]; + | ^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `str` + = note: all local variables must have a statically known size + = help: unsized locals are gated as an unstable feature +help: consider borrowing here + | +LL | let v = &s[..2]; + | + + +error[E0308]: mismatched types + --> $DIR/str-array-assignment.rs:9:17 + | +LL | let w: &str = s[..2]; + | ---- ^^^^^^ + | | | + | | expected `&str`, found `str` + | | help: consider borrowing here: `&s[..2]` + | expected due to this + +error: aborting due to 4 previous errors + +Some errors have detailed explanations: E0277, E0308. +For more information about an error, try `rustc --explain E0277`. diff --git a/tests/ui/str/str-as-char.fixed b/tests/ui/str/str-as-char.fixed new file mode 100644 index 000000000..42bbef839 --- /dev/null +++ b/tests/ui/str/str-as-char.fixed @@ -0,0 +1,5 @@ +// run-rustfix + +fn main() { + println!("●●"); //~ ERROR character literal may only contain one codepoint +} diff --git a/tests/ui/str/str-as-char.rs b/tests/ui/str/str-as-char.rs new file mode 100644 index 000000000..09b9dfc59 --- /dev/null +++ b/tests/ui/str/str-as-char.rs @@ -0,0 +1,5 @@ +// run-rustfix + +fn main() { + println!('●●'); //~ ERROR character literal may only contain one codepoint +} diff --git a/tests/ui/str/str-as-char.stderr b/tests/ui/str/str-as-char.stderr new file mode 100644 index 000000000..c3cb488e3 --- /dev/null +++ b/tests/ui/str/str-as-char.stderr @@ -0,0 +1,13 @@ +error: character literal may only contain one codepoint + --> $DIR/str-as-char.rs:4:14 + | +LL | println!('●●'); + | ^^^^ + | +help: if you meant to write a `str` literal, use double quotes + | +LL | println!("●●"); + | ~~~~ + +error: aborting due to previous error + diff --git a/tests/ui/str/str-concat-on-double-ref.rs b/tests/ui/str/str-concat-on-double-ref.rs new file mode 100644 index 000000000..e68210d53 --- /dev/null +++ b/tests/ui/str/str-concat-on-double-ref.rs @@ -0,0 +1,7 @@ +fn main() { + let a: &String = &"1".to_owned(); + let b: &str = &"2"; + let c = a + b; + //~^ ERROR cannot add `&str` to `&String` + println!("{:?}", c); +} diff --git a/tests/ui/str/str-concat-on-double-ref.stderr b/tests/ui/str/str-concat-on-double-ref.stderr new file mode 100644 index 000000000..bd354679f --- /dev/null +++ b/tests/ui/str/str-concat-on-double-ref.stderr @@ -0,0 +1,18 @@ +error[E0369]: cannot add `&str` to `&String` + --> $DIR/str-concat-on-double-ref.rs:4:15 + | +LL | let c = a + b; + | - ^ - &str + | | | + | | `+` cannot be used to concatenate two `&str` strings + | &String + | + = note: string concatenation requires an owned `String` on the left +help: create an owned `String` from a string reference + | +LL | let c = a.to_owned() + b; + | +++++++++++ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0369`. diff --git a/tests/ui/str/str-escape.rs b/tests/ui/str/str-escape.rs new file mode 100644 index 000000000..0264632fd --- /dev/null +++ b/tests/ui/str/str-escape.rs @@ -0,0 +1,11 @@ +// check-pass +fn main() { + let s = "\ + + "; + //~^^^ WARNING multiple lines skipped by escaped newline + let s = "foo\ +   bar + "; + //~^^^ WARNING non-ASCII whitespace symbol '\u{a0}' is not skipped +} diff --git a/tests/ui/str/str-escape.stderr b/tests/ui/str/str-escape.stderr new file mode 100644 index 000000000..b2501f1a2 --- /dev/null +++ b/tests/ui/str/str-escape.stderr @@ -0,0 +1,21 @@ +warning: multiple lines skipped by escaped newline + --> $DIR/str-escape.rs:3:14 + | +LL | let s = "\ + | ______________^ +LL | | +LL | | "; + | |_____________^ skipping everything up to and including this point + +warning: non-ASCII whitespace symbol '\u{a0}' is not skipped + --> $DIR/str-escape.rs:7:17 + | +LL | let s = "foo\ + | _________________^ +LL | |   bar + | | ^ non-ASCII whitespace symbol '\u{a0}' is not skipped + | |___| + | + +warning: 2 warnings emitted + diff --git a/tests/ui/str/str-idx.rs b/tests/ui/str/str-idx.rs new file mode 100644 index 000000000..1b32ed553 --- /dev/null +++ b/tests/ui/str/str-idx.rs @@ -0,0 +1,7 @@ +pub fn main() { + let s: &str = "hello"; + let _: u8 = s[4]; //~ ERROR the type `str` cannot be indexed by `{integer}` + let _ = s.get(4); //~ ERROR the type `str` cannot be indexed by `{integer}` + let _ = s.get_unchecked(4); //~ ERROR the type `str` cannot be indexed by `{integer}` + let _: u8 = s['c']; //~ ERROR the type `str` cannot be indexed by `char` +} diff --git a/tests/ui/str/str-idx.stderr b/tests/ui/str/str-idx.stderr new file mode 100644 index 000000000..cb1a6fcac --- /dev/null +++ b/tests/ui/str/str-idx.stderr @@ -0,0 +1,54 @@ +error[E0277]: the type `str` cannot be indexed by `{integer}` + --> $DIR/str-idx.rs:3:19 + | +LL | let _: u8 = s[4]; + | ^ string indices are ranges of `usize` + | + = help: the trait `SliceIndex` is not implemented for `{integer}` + = note: you can use `.chars().nth()` or `.bytes().nth()` + for more information, see chapter 8 in The Book: + = help: the trait `SliceIndex<[T]>` is implemented for `usize` + = note: required for `str` to implement `Index<{integer}>` + +error[E0277]: the type `str` cannot be indexed by `{integer}` + --> $DIR/str-idx.rs:4:19 + | +LL | let _ = s.get(4); + | --- ^ string indices are ranges of `usize` + | | + | required by a bound introduced by this call + | + = help: the trait `SliceIndex` is not implemented for `{integer}` + = note: you can use `.chars().nth()` or `.bytes().nth()` + for more information, see chapter 8 in The Book: + = help: the trait `SliceIndex<[T]>` is implemented for `usize` +note: required by a bound in `core::str::::get` + --> $SRC_DIR/core/src/str/mod.rs:LL:COL + +error[E0277]: the type `str` cannot be indexed by `{integer}` + --> $DIR/str-idx.rs:5:29 + | +LL | let _ = s.get_unchecked(4); + | ------------- ^ string indices are ranges of `usize` + | | + | required by a bound introduced by this call + | + = help: the trait `SliceIndex` is not implemented for `{integer}` + = note: you can use `.chars().nth()` or `.bytes().nth()` + for more information, see chapter 8 in The Book: + = help: the trait `SliceIndex<[T]>` is implemented for `usize` +note: required by a bound in `core::str::::get_unchecked` + --> $SRC_DIR/core/src/str/mod.rs:LL:COL + +error[E0277]: the type `str` cannot be indexed by `char` + --> $DIR/str-idx.rs:6:19 + | +LL | let _: u8 = s['c']; + | ^^^ string indices are ranges of `usize` + | + = help: the trait `SliceIndex` is not implemented for `char` + = note: required for `str` to implement `Index` + +error: aborting due to 4 previous errors + +For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/str/str-lit-type-mismatch.rs b/tests/ui/str/str-lit-type-mismatch.rs new file mode 100644 index 000000000..12637c7b9 --- /dev/null +++ b/tests/ui/str/str-lit-type-mismatch.rs @@ -0,0 +1,5 @@ +fn main() { + let x: &[u8] = "foo"; //~ ERROR mismatched types + let y: &[u8; 4] = "baaa"; //~ ERROR mismatched types + let z: &str = b"foo"; //~ ERROR mismatched types +} diff --git a/tests/ui/str/str-lit-type-mismatch.stderr b/tests/ui/str/str-lit-type-mismatch.stderr new file mode 100644 index 000000000..6b56cd6f3 --- /dev/null +++ b/tests/ui/str/str-lit-type-mismatch.stderr @@ -0,0 +1,49 @@ +error[E0308]: mismatched types + --> $DIR/str-lit-type-mismatch.rs:2:20 + | +LL | let x: &[u8] = "foo"; + | ----- ^^^^^ expected slice `[u8]`, found `str` + | | + | expected due to this + | + = note: expected reference `&[u8]` + found reference `&'static str` +help: consider adding a leading `b` + | +LL | let x: &[u8] = b"foo"; + | + + +error[E0308]: mismatched types + --> $DIR/str-lit-type-mismatch.rs:3:23 + | +LL | let y: &[u8; 4] = "baaa"; + | -------- ^^^^^^ expected array `[u8; 4]`, found `str` + | | + | expected due to this + | + = note: expected reference `&[u8; 4]` + found reference `&'static str` +help: consider adding a leading `b` + | +LL | let y: &[u8; 4] = b"baaa"; + | + + +error[E0308]: mismatched types + --> $DIR/str-lit-type-mismatch.rs:4:19 + | +LL | let z: &str = b"foo"; + | ---- ^^^^^^ expected `str`, found array `[u8; 3]` + | | + | expected due to this + | + = note: expected reference `&str` + found reference `&'static [u8; 3]` +help: consider removing the leading `b` + | +LL - let z: &str = b"foo"; +LL + let z: &str = "foo"; + | + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/str/str-mut-idx.rs b/tests/ui/str/str-mut-idx.rs new file mode 100644 index 000000000..575a9eae8 --- /dev/null +++ b/tests/ui/str/str-mut-idx.rs @@ -0,0 +1,17 @@ +fn bot() -> T { loop {} } + +fn mutate(s: &mut str) { + s[1..2] = bot(); + //~^ ERROR the size for values of type + //~| ERROR the size for values of type + s[1usize] = bot(); + //~^ ERROR the type `str` cannot be indexed by `usize` + s.get_mut(1); + //~^ ERROR the type `str` cannot be indexed by `{integer}` + s.get_unchecked_mut(1); + //~^ ERROR the type `str` cannot be indexed by `{integer}` + s['c']; + //~^ ERROR the type `str` cannot be indexed by `char` +} + +pub fn main() {} diff --git a/tests/ui/str/str-mut-idx.stderr b/tests/ui/str/str-mut-idx.stderr new file mode 100644 index 000000000..ca4b86ba3 --- /dev/null +++ b/tests/ui/str/str-mut-idx.stderr @@ -0,0 +1,78 @@ +error[E0277]: the size for values of type `str` cannot be known at compilation time + --> $DIR/str-mut-idx.rs:4:15 + | +LL | s[1..2] = bot(); + | ^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `str` +note: required by a bound in `bot` + --> $DIR/str-mut-idx.rs:1:8 + | +LL | fn bot() -> T { loop {} } + | ^ required by this bound in `bot` +help: consider relaxing the implicit `Sized` restriction + | +LL | fn bot() -> T { loop {} } + | ++++++++ + +error[E0277]: the size for values of type `str` cannot be known at compilation time + --> $DIR/str-mut-idx.rs:4:5 + | +LL | s[1..2] = bot(); + | ^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `str` + = note: the left-hand-side of an assignment must have a statically known size + +error[E0277]: the type `str` cannot be indexed by `usize` + --> $DIR/str-mut-idx.rs:7:7 + | +LL | s[1usize] = bot(); + | ^^^^^^ string indices are ranges of `usize` + | + = help: the trait `SliceIndex` is not implemented for `usize` + = help: the trait `SliceIndex<[T]>` is implemented for `usize` + = note: required for `str` to implement `Index` + +error[E0277]: the type `str` cannot be indexed by `{integer}` + --> $DIR/str-mut-idx.rs:9:15 + | +LL | s.get_mut(1); + | ------- ^ string indices are ranges of `usize` + | | + | required by a bound introduced by this call + | + = help: the trait `SliceIndex` is not implemented for `{integer}` + = note: you can use `.chars().nth()` or `.bytes().nth()` + for more information, see chapter 8 in The Book: + = help: the trait `SliceIndex<[T]>` is implemented for `usize` +note: required by a bound in `core::str::::get_mut` + --> $SRC_DIR/core/src/str/mod.rs:LL:COL + +error[E0277]: the type `str` cannot be indexed by `{integer}` + --> $DIR/str-mut-idx.rs:11:25 + | +LL | s.get_unchecked_mut(1); + | ----------------- ^ string indices are ranges of `usize` + | | + | required by a bound introduced by this call + | + = help: the trait `SliceIndex` is not implemented for `{integer}` + = note: you can use `.chars().nth()` or `.bytes().nth()` + for more information, see chapter 8 in The Book: + = help: the trait `SliceIndex<[T]>` is implemented for `usize` +note: required by a bound in `core::str::::get_unchecked_mut` + --> $SRC_DIR/core/src/str/mod.rs:LL:COL + +error[E0277]: the type `str` cannot be indexed by `char` + --> $DIR/str-mut-idx.rs:13:7 + | +LL | s['c']; + | ^^^ string indices are ranges of `usize` + | + = help: the trait `SliceIndex` is not implemented for `char` + = note: required for `str` to implement `Index` + +error: aborting due to 6 previous errors + +For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/str/str-overrun.rs b/tests/ui/str/str-overrun.rs new file mode 100644 index 000000000..a3ec89413 --- /dev/null +++ b/tests/ui/str/str-overrun.rs @@ -0,0 +1,10 @@ +// run-fail +// error-pattern:index out of bounds: the len is 5 but the index is 5 +// ignore-emscripten no processes + +fn main() { + let s: String = "hello".to_string(); + + // Bounds-check panic. + assert_eq!(s.as_bytes()[5], 0x0 as u8); +} -- cgit v1.2.3