Skip to content

Commit

Permalink
Add regression tests for unconditional_recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Dec 31, 2023
1 parent bc15698 commit 1212a7d
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 10 deletions.
59 changes: 53 additions & 6 deletions tests/ui/unconditional_recursion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,34 +158,81 @@ struct S5;
impl_partial_eq!(S5);
//~^ ERROR: function cannot return without recursing

struct S6;
struct S6 {
field: String,
}

impl PartialEq for S6 {
fn eq(&self, other: &Self) -> bool {
let mine = &self.field;
let theirs = &other.field;
mine == theirs // Should not warn!
}
}

struct S7<'a> {
field: &'a S7<'a>,
}

impl<'a> PartialEq for S7<'a> {
fn eq(&self, other: &Self) -> bool {
//~^ ERROR: function cannot return without recursing
let mine = &self.field;
let theirs = &other.field;
mine == theirs
}
}

impl std::string::ToString for S6 {
struct S8 {
num: i32,
field: Option<Box<S8>>,
}

impl PartialEq for S8 {
fn eq(&self, other: &Self) -> bool {
if self.num != other.num {
return false;
}

let (this, other) = match (self.field.as_deref(), other.field.as_deref()) {
(Some(x1), Some(x2)) => (x1, x2),
(None, None) => return true,
_ => return false,
};

this == other
}
}

struct S9;

impl std::string::ToString for S9 {
fn to_string(&self) -> String {
//~^ ERROR: function cannot return without recursing
self.to_string()
}
}

struct S7;
struct S10;

impl std::string::ToString for S7 {
impl std::string::ToString for S10 {
fn to_string(&self) -> String {
//~^ ERROR: function cannot return without recursing
let x = self;
x.to_string()
}
}

struct S8;
struct S11;

impl std::string::ToString for S8 {
impl std::string::ToString for S11 {
fn to_string(&self) -> String {
//~^ ERROR: function cannot return without recursing
(self as &Self).to_string()
}
}


fn main() {
// test code goes here
}
53 changes: 49 additions & 4 deletions tests/ui/unconditional_recursion.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ LL | self.eq(other)
= help: a `loop` may express intention better if this is on purpose

error: function cannot return without recursing
--> $DIR/unconditional_recursion.rs:164:5
--> $DIR/unconditional_recursion.rs:210:5
|
LL | fn to_string(&self) -> String {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot return without recursing
Expand All @@ -34,7 +34,7 @@ LL | self.to_string()
= help: a `loop` may express intention better if this is on purpose

error: function cannot return without recursing
--> $DIR/unconditional_recursion.rs:173:5
--> $DIR/unconditional_recursion.rs:219:5
|
LL | fn to_string(&self) -> String {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot return without recursing
Expand All @@ -45,7 +45,7 @@ LL | x.to_string()
= help: a `loop` may express intention better if this is on purpose

error: function cannot return without recursing
--> $DIR/unconditional_recursion.rs:183:5
--> $DIR/unconditional_recursion.rs:229:5
|
LL | fn to_string(&self) -> String {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot return without recursing
Expand Down Expand Up @@ -87,6 +87,34 @@ note: recursive call site
LL | self == other
| ^^^^^^^^^^^^^

error: function cannot return without recursing
--> $DIR/unconditional_recursion.rs:28:5
|
LL | / fn ne(&self, other: &Self) -> bool {
LL | | self != &Foo2::B // no error here
LL | | }
| |_____^
|
note: recursive call site
--> $DIR/unconditional_recursion.rs:29:9
|
LL | self != &Foo2::B // no error here
| ^^^^^^^^^^^^^^^^

error: function cannot return without recursing
--> $DIR/unconditional_recursion.rs:31:5
|
LL | / fn eq(&self, other: &Self) -> bool {
LL | | self == &Foo2::B // no error here
LL | | }
| |_____^
|
note: recursive call site
--> $DIR/unconditional_recursion.rs:32:9
|
LL | self == &Foo2::B // no error here
| ^^^^^^^^^^^^^^^^

error: function cannot return without recursing
--> $DIR/unconditional_recursion.rs:42:5
|
Expand Down Expand Up @@ -280,5 +308,22 @@ LL | impl_partial_eq!(S5);
| -------------------- in this macro invocation
= note: this error originates in the macro `impl_partial_eq` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 22 previous errors
error: function cannot return without recursing
--> $DIR/unconditional_recursion.rs:178:5
|
LL | / fn eq(&self, other: &Self) -> bool {
LL | |
LL | | let mine = &self.field;
LL | | let theirs = &other.field;
LL | | mine == theirs
LL | | }
| |_____^
|
note: recursive call site
--> $DIR/unconditional_recursion.rs:182:9
|
LL | mine == theirs
| ^^^^^^^^^^^^^^

error: aborting due to 25 previous errors

0 comments on commit 1212a7d

Please sign in to comment.