Skip to content

Commit

Permalink
Merge pull request #394 from dtolnay/deprecated
Browse files Browse the repository at this point in the history
Prevent deprecation warning on generated impl for deprecated type
  • Loading branch information
dtolnay authored Dec 7, 2024
2 parents 42b1460 + 07e7d99 commit 6712f8c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
16 changes: 12 additions & 4 deletions impl/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::ast::{Enum, Field, Input, Struct};
use crate::attr::Trait;
use crate::generics::InferredBounds;
use crate::unraw::MemberUnraw;
use proc_macro2::{Ident, TokenStream};
use proc_macro2::{Ident, Span, TokenStream};
use quote::{format_ident, quote, quote_spanned, ToTokens};
use std::collections::BTreeSet as Set;
use syn::{DeriveInput, GenericArgument, PathArguments, Result, Token, Type};
Expand All @@ -27,7 +27,7 @@ fn try_expand(input: &DeriveInput) -> Result<TokenStream> {
}

fn fallback(input: &DeriveInput, error: syn::Error) -> TokenStream {
let ty = &input.ident;
let ty = call_site_ident(&input.ident);
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();

let error = error.to_compile_error();
Expand Down Expand Up @@ -55,7 +55,7 @@ fn fallback(input: &DeriveInput, error: syn::Error) -> TokenStream {
}

fn impl_struct(input: Struct) -> TokenStream {
let ty = &input.ident;
let ty = call_site_ident(&input.ident);
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let mut error_inferred_bounds = InferredBounds::new();

Expand Down Expand Up @@ -228,7 +228,7 @@ fn impl_struct(input: Struct) -> TokenStream {
}

fn impl_enum(input: Enum) -> TokenStream {
let ty = &input.ident;
let ty = call_site_ident(&input.ident);
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let mut error_inferred_bounds = InferredBounds::new();

Expand Down Expand Up @@ -492,6 +492,14 @@ fn impl_enum(input: Enum) -> TokenStream {
}
}

// Create an ident with which we can expand `impl Trait for #ident {}` on a
// deprecated type without triggering deprecation warning on the generated impl.
fn call_site_ident(ident: &Ident) -> Ident {
let mut ident = ident.clone();
ident.set_span(ident.span().resolved_at(Span::call_site()));
ident
}

fn fields_pat(fields: &[Field]) -> TokenStream {
let mut members = fields.iter().map(|field| &field.member).peekable();
match members.peek() {
Expand Down
31 changes: 28 additions & 3 deletions tests/test_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,37 @@ fn test_deprecated() {
#![deny(deprecated)]

#[derive(Error, Debug)]
pub enum MyError {
#[deprecated]
#[error("...")]
pub struct DeprecatedStruct;

#[derive(Error, Debug)]
#[error("{message} {}", .message)]
pub struct DeprecatedStructField {
#[deprecated]
message: String,
}

#[derive(Error, Debug)]
#[deprecated]
pub enum DeprecatedEnum {
#[error("...")]
Variant,
}

#[derive(Error, Debug)]
pub enum DeprecatedVariant {
#[deprecated]
#[error("...")]
Deprecated,
Variant,
}

#[allow(deprecated)]
let _ = MyError::Deprecated;
let _: DeprecatedStruct;
#[allow(deprecated)]
let _: DeprecatedStructField;
#[allow(deprecated)]
let _ = DeprecatedEnum::Variant;
#[allow(deprecated)]
let _ = DeprecatedVariant::Variant;
}
3 changes: 3 additions & 0 deletions tests/ui/missing-display.stderr
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
error[E0277]: `MyError` doesn't implement `std::fmt::Display`
--> tests/ui/missing-display.rs:4:10
|
3 | #[derive(Error, Debug)]
| ----- in this derive macro expansion
4 | pub enum MyError {
| ^^^^^^^ `MyError` cannot be formatted with the default formatter
|
Expand All @@ -11,3 +13,4 @@ note: required by a bound in `std::error::Error`
|
| pub trait Error: Debug + Display {
| ^^^^^^^ required by this bound in `Error`
= note: this error originates in the derive macro `Error` (in Nightly builds, run with -Z macro-backtrace for more info)

0 comments on commit 6712f8c

Please sign in to comment.