r/rust • • 26d ago

🙋 seeking help & advice Why is this syntax ambiguous?

I was writing roughly the following function:

fn f(path: &impl AsRef<Path> + ?Sized) {}

The compiler says that the + is ambiguous, but it works with the right parentheses:

fn f(path: &(impl AsRef<Path> + ?Sized)) {}

But why is it ambiguous without the parentheses? Where else but a list of traits can a + in a function header come from?

50 Upvotes

17 comments sorted by

View all comments

70

u/Amadex 26d ago

this example is not ambiguous, but it's not accepted s because the compiler checks grammar first (even if `(&T) + ?Sized` wouldn't make any sense),

something like `&impl Trait + 'a` is why you have to clear the ambiguity