diff options
| author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2022-04-07 13:56:14 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-07 13:56:14 +0000 |
| commit | 82fa6ad245300c817f5ff67094dd2108704ca531 (patch) | |
| tree | d3232fbe08ceaef65ea7d9008642ebceff08b4a9 | |
| parent | 8765baaf9df91eade2ecf13dd61314067dbcada1 (diff) | |
| parent | 16d0f724b6544a87148e5fc34e251af41ac63da5 (diff) | |
| download | rust-82fa6ad245300c817f5ff67094dd2108704ca531.tar.gz rust-82fa6ad245300c817f5ff67094dd2108704ca531.zip | |
Merge #11926
11926: fix: Fix panics with `#[cfg]`'d-out `self` parameter r=jonas-schievink a=jonas-schievink bors r+ Co-authored-by: Jonas Schievink <jonas.schievink@ferrous-systems.com>
| -rw-r--r-- | crates/hir_def/src/data.rs | 16 | ||||
| -rw-r--r-- | crates/hir_ty/src/tests/regression.rs | 17 |
2 files changed, 33 insertions, 0 deletions
diff --git a/crates/hir_def/src/data.rs b/crates/hir_def/src/data.rs index 8e0bb0c34d3..5140a9d4ca5 100644 --- a/crates/hir_def/src/data.rs +++ b/crates/hir_def/src/data.rs @@ -56,6 +56,22 @@ impl FunctionData { if is_varargs { flags.bits |= FnFlags::IS_VARARGS; } + if flags.bits & FnFlags::HAS_SELF_PARAM != 0 { + // If there's a self param in the syntax, but it is cfg'd out, remove the flag. + let is_cfgd_out = match func.params.clone().next() { + Some(param) => { + !item_tree.attrs(db, krate, param.into()).is_cfg_enabled(cfg_options) + } + None => { + stdx::never!("fn HAS_SELF_PARAM but no parameters allocated"); + true + } + }; + if is_cfgd_out { + cov_mark::hit!(cfgd_out_self_param); + flags.bits &= !FnFlags::HAS_SELF_PARAM; + } + } let legacy_const_generics_indices = item_tree .attrs(db, krate, ModItem::from(loc.id.value).into()) diff --git a/crates/hir_ty/src/tests/regression.rs b/crates/hir_ty/src/tests/regression.rs index be2b733a4b1..37f321a0f00 100644 --- a/crates/hir_ty/src/tests/regression.rs +++ b/crates/hir_ty/src/tests/regression.rs @@ -1488,3 +1488,20 @@ fn test<T: Crash>() { "#, ); } + +#[test] +fn cfgd_out_self_param() { + cov_mark::check!(cfgd_out_self_param); + check_no_mismatches( + r#" +struct S; +impl S { + fn f(#[cfg(never)] &self) {} +} + +fn f(s: S) { + s.f(); +} +"#, + ); +} |
