Skip to content
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
12 changes: 6 additions & 6 deletions src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2593,7 +2593,6 @@ pub(crate) mod parsing {
input.parse::<Token![const]>()?;
}

let begin = input.fork();
let polarity = if input.peek(Token![!]) && !input.peek2(token::Brace) {
Some(input.parse::<Token![!]>()?)
} else {
Expand Down Expand Up @@ -2631,13 +2630,14 @@ pub(crate) mod parsing {
trait_ = None;
}
self_ty = input.parse()?;
} else if let Some(polarity) = polarity {
return Err(Error::new(
polarity.span,
"inherent impls cannot be negative",
));
} else {
trait_ = None;
self_ty = if polarity.is_none() {
first_ty
} else {
Type::Verbatim(verbatim::between(&begin, input))
};
self_ty = first_ty;
}

generics.where_clause = input.parse()?;
Expand Down
4 changes: 3 additions & 1 deletion tests/common/eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ use rustc_ast::ast::StructRest;
use rustc_ast::ast::Term;
use rustc_ast::ast::Trait;
use rustc_ast::ast::TraitBoundModifiers;
use rustc_ast::ast::TraitImplHeader;
use rustc_ast::ast::TraitObjectSyntax;
use rustc_ast::ast::TraitRef;
use rustc_ast::ast::Ty;
Expand Down Expand Up @@ -508,7 +509,7 @@ spanless_eq_struct!(FormatOptions; width precision alignment fill sign alternate
spanless_eq_struct!(FormatPlaceholder; argument span format_trait format_options);
spanless_eq_struct!(GenericParam; id ident attrs bounds is_placeholder kind !colon_span);
spanless_eq_struct!(Generics; params where_clause span);
spanless_eq_struct!(Impl; defaultness safety generics constness polarity of_trait self_ty items);
spanless_eq_struct!(Impl; generics of_trait self_ty items);
spanless_eq_struct!(InlineAsm; asm_macro template template_strs operands clobber_abis options line_spans);
spanless_eq_struct!(InlineAsmSym; id qself path);
spanless_eq_struct!(Item<K>; attrs id span vis kind !tokens);
Expand Down Expand Up @@ -539,6 +540,7 @@ spanless_eq_struct!(StructExpr; qself path fields rest);
spanless_eq_struct!(Token; kind span);
spanless_eq_struct!(Trait; constness safety is_auto ident generics bounds items);
spanless_eq_struct!(TraitBoundModifiers; constness asyncness polarity);
spanless_eq_struct!(TraitImplHeader; defaultness safety constness polarity trait_ref);
spanless_eq_struct!(TraitRef; path ref_id);
spanless_eq_struct!(Ty; id kind span tokens);
spanless_eq_struct!(TyAlias; defaultness ident generics where_clauses bounds ty);
Expand Down
6 changes: 5 additions & 1 deletion tests/repo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,11 @@ static EXCLUDE_FILES: &[&str] = &[
// Rustc bug: https://github.com/rust-lang/rust/issues/132080
"src/tools/rust-analyzer/crates/parser/test_data/parser/ok/0073_safe_declarations_in_extern_blocks.rs",

// Compile-fail expr parameter in const generic position: f::<1 + 2>()
// Negative inherent impl: `impl !Box<JoinHandle> {}`
"src/tools/rustfmt/tests/source/negative-impl.rs",
"src/tools/rustfmt/tests/target/negative-impl.rs",

// Compile-fail expr parameter in const generic position: `f::<1 + 2>()`
"tests/ui/const-generics/early/closing-args-token.rs",
"tests/ui/const-generics/early/const-expression-parameter.rs",

Expand Down
26 changes: 2 additions & 24 deletions tests/test_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ fn test_macro_variable_attr() {

#[test]
fn test_negative_impl() {
// Rustc parses all of the following.

#[cfg(any())]
impl ! {}
let tokens = quote! {
Expand All @@ -67,18 +65,11 @@ fn test_negative_impl() {
}
"#);

#[cfg(any())]
#[rustfmt::skip]
impl !Trait {}
let tokens = quote! {
impl !Trait {}
};
snapshot!(tokens as Item, @r#"
Item::Impl {
generics: Generics,
self_ty: Type::Verbatim(`! Trait`),
}
"#);
let err = syn::parse2::<Item>(tokens).unwrap_err();
assert_eq!(err.to_string(), "inherent impls cannot be negative");

#[cfg(any())]
impl !Trait for T {}
Expand Down Expand Up @@ -109,19 +100,6 @@ fn test_negative_impl() {
},
}
"#);

#[cfg(any())]
#[rustfmt::skip]
impl !! {}
let tokens = quote! {
impl !! {}
};
snapshot!(tokens as Item, @r#"
Item::Impl {
generics: Generics,
self_ty: Type::Verbatim(`! !`),
}
"#);
}

#[test]
Expand Down
Loading