about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJonas Schievink <jonas.schievink@ferrous-systems.com>2022-04-07 15:47:03 +0200
committerJonas Schievink <jonas.schievink@ferrous-systems.com>2022-04-07 15:47:03 +0200
commit67495b618b1538e1e90f49ff6b8f980ca83c3aa9 (patch)
tree4d2cf3ee338aa5c90ffb0d9358c1a3cd8b6ba1e4
parent8765baaf9df91eade2ecf13dd61314067dbcada1 (diff)
downloadrust-67495b618b1538e1e90f49ff6b8f980ca83c3aa9.tar.gz
rust-67495b618b1538e1e90f49ff6b8f980ca83c3aa9.zip
Fix panics with `#[cfg]`'d-out `self` parameter
-rw-r--r--crates/hir_def/src/data.rs9
-rw-r--r--crates/hir_ty/src/tests/regression.rs17
2 files changed, 26 insertions, 0 deletions
diff --git a/crates/hir_def/src/data.rs b/crates/hir_def/src/data.rs
index 8e0bb0c34d3..6571b2a9276 100644
--- a/crates/hir_def/src/data.rs
+++ b/crates/hir_def/src/data.rs
@@ -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())
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();
+}
+"#,
+    );
+}