rust-analyzer icon indicating copy to clipboard operation
rust-analyzer copied to clipboard

Custom derive attribute stripping is insufficient

Open jonas-schievink opened this issue 4 years ago • 9 comments

In

#[derive(Derive)]
#[cfg_attr(never, derive(Clone))]
struct S {
    #[cfg(never)]
    invisible_field: u8,
    #[cfg(not(never))]
    visible_field: u8,
}

The Derive macro should be passed the token stream corresponding to

struct S {
    #[cfg(not(never))]
    visible_field: u8,
}

Currently, we pass

#[cfg_attr(never, derive(Clone))]
struct S {
    #[cfg(never)]
    invisible_field: u8,
    #[cfg(not(never))]
    visible_field: u8,
}

Since we have to evalutate cfgs to do this correctly, I propose moving the remove_derive_attrs code to hir_expand.

jonas-schievink avatar Apr 08 '21 23:04 jonas-schievink

Also, we should only be removing #[derive] attributes that come before the currently expanded derive macro, at least when https://github.com/rust-lang/rust/pull/84023 lands (see its tests)

jonas-schievink avatar Apr 09 '21 07:04 jonas-schievink

Another relevant upstream PR: https://github.com/rust-lang/rust/pull/84110

jonas-schievink avatar Apr 11 '21 22:04 jonas-schievink

Hello! Our team is struggling because of this bug. Do you plan to fix this? It's open for 2 years already.

Arjentix avatar Apr 07 '23 13:04 Arjentix

Same problem. Is there any progress on this issue?

silver-ymz avatar May 12 '23 13:05 silver-ymz

This got worse for me about 3 or 4 weeks ago, specifically with #[cfg(target_os = ...)] fields. Maybe a regression? It's unfortunately making it impossible to see real errors in VSCode.

jeff-hiner avatar May 26 '23 16:05 jeff-hiner

Any updates here? This would be a big usability boost IMO.

smalis-msft avatar Sep 25 '23 17:09 smalis-msft

Same (or very similar) problem here with serde derive, it causes no-such-field error , it's caused by field, which is behind inactive feature, line 164,165. If I enable the feature in rust-analyzer settings that it's OK. I'm almost sure that it did work some time ago - few month, so wondering why this old issue demostrated just now.

image

izderadicka avatar Dec 16 '23 09:12 izderadicka

OMG, a three-year-age bug.

ssrlive avatar Jan 16 '24 08:01 ssrlive

I have started implementing a fix for this. image

@rustbot claim

wyatt-herkamp avatar Mar 08 '24 16:03 wyatt-herkamp