Skip to content

Commit 67495b6

Browse files
author
Jonas Schievink
committed
Fix panics with #[cfg]'d-out self parameter
1 parent 8765baa commit 67495b6

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

crates/hir_def/src/data.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ 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+
cov_mark::hit!(cfgd_out_self_param);
62+
let param =
63+
func.params.clone().next().expect("fn HAS_SELF_PARAM but no parameters allocated");
64+
if !item_tree.attrs(db, krate, param.into()).is_cfg_enabled(cfg_options) {
65+
flags.bits &= !FnFlags::HAS_SELF_PARAM;
66+
}
67+
}
5968

6069
let legacy_const_generics_indices = item_tree
6170
.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)