summaryrefslogtreecommitdiffstats
path: root/src/test/ui/str
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /src/test/ui/str
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/test/ui/str')
-rw-r--r--src/test/ui/str/str-array-assignment.rs11
-rw-r--r--src/test/ui/str/str-array-assignment.stderr45
-rw-r--r--src/test/ui/str/str-as-char.fixed5
-rw-r--r--src/test/ui/str/str-as-char.rs5
-rw-r--r--src/test/ui/str/str-as-char.stderr13
-rw-r--r--src/test/ui/str/str-concat-on-double-ref.rs7
-rw-r--r--src/test/ui/str/str-concat-on-double-ref.stderr18
-rw-r--r--src/test/ui/str/str-escape.rs11
-rw-r--r--src/test/ui/str/str-escape.stderr21
-rw-r--r--src/test/ui/str/str-idx.rs7
-rw-r--r--src/test/ui/str/str-idx.stderr60
-rw-r--r--src/test/ui/str/str-lit-type-mismatch.rs5
-rw-r--r--src/test/ui/str/str-lit-type-mismatch.stderr49
-rw-r--r--src/test/ui/str/str-mut-idx.rs17
-rw-r--r--src/test/ui/str/str-mut-idx.stderr84
-rw-r--r--src/test/ui/str/str-overrun.rs10
16 files changed, 368 insertions, 0 deletions
diff --git a/src/test/ui/str/str-array-assignment.rs b/src/test/ui/str/str-array-assignment.rs
new file mode 100644
index 000000000..323eefb38
--- /dev/null
+++ b/src/test/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/src/test/ui/str/str-array-assignment.stderr b/src/test/ui/str/str-array-assignment.stderr
new file mode 100644
index 000000000..c23400a1d
--- /dev/null
+++ b/src/test/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/src/test/ui/str/str-as-char.fixed b/src/test/ui/str/str-as-char.fixed
new file mode 100644
index 000000000..42bbef839
--- /dev/null
+++ b/src/test/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/src/test/ui/str/str-as-char.rs b/src/test/ui/str/str-as-char.rs
new file mode 100644
index 000000000..09b9dfc59
--- /dev/null
+++ b/src/test/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/src/test/ui/str/str-as-char.stderr b/src/test/ui/str/str-as-char.stderr
new file mode 100644
index 000000000..c3cb488e3
--- /dev/null
+++ b/src/test/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/src/test/ui/str/str-concat-on-double-ref.rs b/src/test/ui/str/str-concat-on-double-ref.rs
new file mode 100644
index 000000000..e68210d53
--- /dev/null
+++ b/src/test/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/src/test/ui/str/str-concat-on-double-ref.stderr b/src/test/ui/str/str-concat-on-double-ref.stderr
new file mode 100644
index 000000000..bd354679f
--- /dev/null
+++ b/src/test/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/src/test/ui/str/str-escape.rs b/src/test/ui/str/str-escape.rs
new file mode 100644
index 000000000..0264632fd
--- /dev/null
+++ b/src/test/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/src/test/ui/str/str-escape.stderr b/src/test/ui/str/str-escape.stderr
new file mode 100644
index 000000000..b2501f1a2
--- /dev/null
+++ b/src/test/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/src/test/ui/str/str-idx.rs b/src/test/ui/str/str-idx.rs
new file mode 100644
index 000000000..1b32ed553
--- /dev/null
+++ b/src/test/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/src/test/ui/str/str-idx.stderr b/src/test/ui/str/str-idx.stderr
new file mode 100644
index 000000000..9079a18d6
--- /dev/null
+++ b/src/test/ui/str/str-idx.stderr
@@ -0,0 +1,60 @@
+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<str>` is not implemented for `{integer}`
+ = note: you can use `.chars().nth()` or `.bytes().nth()`
+ for more information, see chapter 8 in The Book: <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>
+ = help: the trait `SliceIndex<[T]>` is implemented for `usize`
+ = note: required because of the requirements on the impl of `Index<{integer}>` for `str`
+
+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<str>` is not implemented for `{integer}`
+ = note: you can use `.chars().nth()` or `.bytes().nth()`
+ for more information, see chapter 8 in The Book: <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>
+ = help: the trait `SliceIndex<[T]>` is implemented for `usize`
+note: required by a bound in `core::str::<impl str>::get`
+ --> $SRC_DIR/core/src/str/mod.rs:LL:COL
+ |
+LL | pub const fn get<I: ~const SliceIndex<str>>(&self, i: I) -> Option<&I::Output> {
+ | ^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `core::str::<impl str>::get`
+
+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<str>` is not implemented for `{integer}`
+ = note: you can use `.chars().nth()` or `.bytes().nth()`
+ for more information, see chapter 8 in The Book: <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>
+ = help: the trait `SliceIndex<[T]>` is implemented for `usize`
+note: required by a bound in `core::str::<impl str>::get_unchecked`
+ --> $SRC_DIR/core/src/str/mod.rs:LL:COL
+ |
+LL | pub const unsafe fn get_unchecked<I: ~const SliceIndex<str>>(&self, i: I) -> &I::Output {
+ | ^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `core::str::<impl str>::get_unchecked`
+
+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<str>` is not implemented for `char`
+ = note: required because of the requirements on the impl of `Index<char>` for `str`
+
+error: aborting due to 4 previous errors
+
+For more information about this error, try `rustc --explain E0277`.
diff --git a/src/test/ui/str/str-lit-type-mismatch.rs b/src/test/ui/str/str-lit-type-mismatch.rs
new file mode 100644
index 000000000..12637c7b9
--- /dev/null
+++ b/src/test/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/src/test/ui/str/str-lit-type-mismatch.stderr b/src/test/ui/str/str-lit-type-mismatch.stderr
new file mode 100644
index 000000000..6b56cd6f3
--- /dev/null
+++ b/src/test/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/src/test/ui/str/str-mut-idx.rs b/src/test/ui/str/str-mut-idx.rs
new file mode 100644
index 000000000..575a9eae8
--- /dev/null
+++ b/src/test/ui/str/str-mut-idx.rs
@@ -0,0 +1,17 @@
+fn bot<T>() -> 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/src/test/ui/str/str-mut-idx.stderr b/src/test/ui/str/str-mut-idx.stderr
new file mode 100644
index 000000000..2d062e56a
--- /dev/null
+++ b/src/test/ui/str/str-mut-idx.stderr
@@ -0,0 +1,84 @@
+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>() -> T { loop {} }
+ | ^ required by this bound in `bot`
+help: consider relaxing the implicit `Sized` restriction
+ |
+LL | fn bot<T: ?Sized>() -> 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<str>` is not implemented for `usize`
+ = help: the trait `SliceIndex<[T]>` is implemented for `usize`
+ = note: required because of the requirements on the impl of `Index<usize>` for `str`
+
+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<str>` is not implemented for `{integer}`
+ = note: you can use `.chars().nth()` or `.bytes().nth()`
+ for more information, see chapter 8 in The Book: <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>
+ = help: the trait `SliceIndex<[T]>` is implemented for `usize`
+note: required by a bound in `core::str::<impl str>::get_mut`
+ --> $SRC_DIR/core/src/str/mod.rs:LL:COL
+ |
+LL | pub const fn get_mut<I: ~const SliceIndex<str>>(&mut self, i: I) -> Option<&mut I::Output> {
+ | ^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `core::str::<impl str>::get_mut`
+
+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<str>` is not implemented for `{integer}`
+ = note: you can use `.chars().nth()` or `.bytes().nth()`
+ for more information, see chapter 8 in The Book: <https://doc.rust-lang.org/book/ch08-02-strings.html#indexing-into-strings>
+ = help: the trait `SliceIndex<[T]>` is implemented for `usize`
+note: required by a bound in `core::str::<impl str>::get_unchecked_mut`
+ --> $SRC_DIR/core/src/str/mod.rs:LL:COL
+ |
+LL | pub const unsafe fn get_unchecked_mut<I: ~const SliceIndex<str>>(
+ | ^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `core::str::<impl str>::get_unchecked_mut`
+
+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<str>` is not implemented for `char`
+ = note: required because of the requirements on the impl of `Index<char>` for `str`
+
+error: aborting due to 6 previous errors
+
+For more information about this error, try `rustc --explain E0277`.
diff --git a/src/test/ui/str/str-overrun.rs b/src/test/ui/str/str-overrun.rs
new file mode 100644
index 000000000..a3ec89413
--- /dev/null
+++ b/src/test/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);
+}