Skip to content
Open
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
19 changes: 19 additions & 0 deletions impl/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ impl Display<'_> {
None => return Ok(()),
};
let member = match next {
'}' => {
// `{}` displays extra argument 0. `#[error("{}", self)]`
// recurses through Display the same way `{self}` does.
if first_unnamed.as_ref().is_some_and(extra_arg_is_self) {
infinite_recursive = true;
}
continue;
}
'0'..='9' => {
let int = take_int(&mut read);
if !extra_positional_arguments_allowed {
Expand Down Expand Up @@ -108,6 +116,9 @@ impl Display<'_> {
}
};
infinite_recursive |= member == *"self" && bound == Trait::Display;
infinite_recursive |= bound == Trait::Display
&& matches!(&member, MemberUnraw::Unnamed(index) if index.index == 0)
&& first_unnamed.as_ref().is_some_and(extra_arg_is_self);
let field = match member_index.get(&member) {
Some(&field) => field,
None => {
Expand Down Expand Up @@ -270,6 +281,14 @@ fn is_syn_full() -> bool {
}
}

fn extra_arg_is_self(tokens: &TokenStream) -> bool {
let mut iter = tokens.clone().into_iter();
match (iter.next(), iter.next()) {
(Some(TokenTree::Ident(ident)), None) => ident == "self",
_ => false,
}
}

fn take_int<'a>(read: &mut &'a str) -> &'a str {
let mut int_len = 0;
for ch in read.chars() {
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/unconditional-recursion-2.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-2.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
error[E0425]: cannot find value `__FAIL__` in this scope
--> tests/ui/unconditional-recursion-2.rs:8:5
|
8 | __FAIL__;
| ^^^^^^^^ not found in this scope

warning: function cannot return without recursing
--> tests/ui/unconditional-recursion-2.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-2.rs:4:9
|
4 | #[error("{}", self)]
| ^^^^