Skip to content

Commit

Permalink
Merge #11926
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
bors[bot] and Jonas Schievink committed Apr 7, 2022
2 parents 8765baa + 67495b6 commit df645c4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
9 changes: 9 additions & 0 deletions crates/hir_def/src/data.rs
Expand Up @@ -56,6 +56,15 @@ 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.
cov_mark::hit!(cfgd_out_self_param);
let param =
func.params.clone().next().expect("fn HAS_SELF_PARAM but no parameters allocated");
if !item_tree.attrs(db, krate, param.into()).is_cfg_enabled(cfg_options) {
flags.bits &= !FnFlags::HAS_SELF_PARAM;
}
}

let legacy_const_generics_indices = item_tree
.attrs(db, krate, ModItem::from(loc.id.value).into())
Expand Down
17 changes: 17 additions & 0 deletions crates/hir_ty/src/tests/regression.rs
Expand Up @@ -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();
}
"#,
);
}

0 comments on commit df645c4

Please sign in to comment.