Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recognize infinite recursion caused by {self} #359

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 29 additions & 18 deletions impl/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,29 +102,40 @@ impl Display<'_> {
MemberUnraw::Unnamed(index) => format_ident!("_{}", index),
MemberUnraw::Named(ident) => ident.to_local(),
});
if let Some(&field) = member_index.get(&member) {
let end_spec = match read.find('}') {
Some(end_spec) => end_spec,
None => return Ok(()),
};
let bound = match read[..end_spec].chars().next_back() {
Some('?') => Trait::Debug,
Some('o') => Trait::Octal,
Some('x') => Trait::LowerHex,
Some('X') => Trait::UpperHex,
Some('p') => Trait::Pointer,
Some('b') => Trait::Binary,
Some('e') => Trait::LowerExp,
Some('E') => Trait::UpperExp,
Some(_) => Trait::Display,
None => {
let end_spec = match read.find('}') {
Some(end_spec) => end_spec,
None => return Ok(()),
};
let bound = match read[..end_spec].chars().next_back() {
Some('?') => Trait::Debug,
Some('o') => Trait::Octal,
Some('x') => Trait::LowerHex,
Some('X') => Trait::UpperHex,
Some('p') => Trait::Pointer,
Some('b') => Trait::Binary,
Some('e') => Trait::LowerExp,
Some('E') => Trait::UpperExp,
Some(_) => Trait::Display,
None => {
if member_index.contains_key(&member) {
has_bonus_display = true;
binding_value.extend(quote_spanned!(span=> .as_display()));
Trait::Display
}
};
Trait::Display
}
};
if let Some(&field) = member_index.get(&member) {
implied_bounds.insert((field, bound));
}
if member == *"self" && bound == Trait::Display {
binding_value = quote_spanned!(member.span()=>
{
#[warn(unconditional_recursion)]
fn _fmt() { _fmt() }
#member
}
);
}
bindings.push((local, binding_value));
}

Expand Down
9 changes: 9 additions & 0 deletions impl/src/unraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ impl PartialEq for MemberUnraw {
}
}

impl PartialEq<str> for MemberUnraw {
fn eq(&self, other: &str) -> bool {
match self {
MemberUnraw::Named(this) => this == other,
MemberUnraw::Unnamed(_) => false,
}
}
}

impl Hash for MemberUnraw {
fn hash<H: Hasher>(&self, hasher: &mut H) {
match self {
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/unconditional-recursion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use thiserror::Error;

#[derive(Error, Debug)]
#[error("{self}")]
pub struct Error;

fn main() {
@//fail
}
21 changes: 21 additions & 0 deletions tests/ui/unconditional-recursion.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
error: expected expression, found `@`
--> tests/ui/unconditional-recursion.rs:8:5
|
8 | @//fail
| ^ expected expression

warning: function cannot return without recursing
--> tests/ui/unconditional-recursion.rs:4:9
|
4 | #[error("{self}")]
| ^^^^^^^^
| |
| cannot return without recursing
| recursive call site
|
= help: a `loop` may express intention better if this is on purpose
note: the lint level is defined here
--> tests/ui/unconditional-recursion.rs:4:9
|
4 | #[error("{self}")]
| ^^^^^^^^
Loading