Skip to content

Commit 82fa6ad

Browse files
bors[bot]Jonas Schievink
andauthored
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>
2 parents 8765baa + 16d0f72 commit 82fa6ad

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

crates/hir_def/src/data.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,22 @@ impl FunctionData {
5656
if is_varargs {
5757
flags.bits |= FnFlags::IS_VARARGS;
5858
}
59+
if flags.bits & FnFlags::HAS_SELF_PARAM != 0 {
60+
// If there's a self param in the syntax, but it is cfg'd out, remove the flag.
61+
let is_cfgd_out = match func.params.clone().next() {
62+
Some(param) => {
63+
!item_tree.attrs(db, krate, param.into()).is_cfg_enabled(cfg_options)
64+
}
65+
None => {
66+
stdx::never!("fn HAS_SELF_PARAM but no parameters allocated");
67+
true
68+
}
69+
};
70+
if is_cfgd_out {
71+
cov_mark::hit!(cfgd_out_self_param);
72+
flags.bits &= !FnFlags::HAS_SELF_PARAM;
73+
}
74+
}
5975

6076
let legacy_const_generics_indices = item_tree
6177
.attrs(db, krate, ModItem::from(loc.id.value).into())

crates/hir_ty/src/tests/regression.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,3 +1488,20 @@ fn test<T: Crash>() {
14881488
"#,
14891489
);
14901490
}
1491+
1492+
#[test]
1493+
fn cfgd_out_self_param() {
1494+
cov_mark::check!(cfgd_out_self_param);
1495+
check_no_mismatches(
1496+
r#"
1497+
struct S;
1498+
impl S {
1499+
fn f(#[cfg(never)] &self) {}
1500+
}
1501+
1502+
fn f(s: S) {
1503+
s.f();
1504+
}
1505+
"#,
1506+
);
1507+
}

0 commit comments

Comments
 (0)