about summary refs log tree commit diff
diff options
context:
space:
mode:
authorTakayuki Maeda <takoyaki0316@gmail.com>2022-12-07 12:55:30 +0900
committerTakayuki Maeda <takoyaki0316@gmail.com>2022-12-07 12:55:30 +0900
commitcb596e3015811f6d08f45b2ebf41924a8f329c13 (patch)
tree4816e19b321d24ed10b42c5adbb05a0d19b6dce5
parented61c139c2bc778ebb91f5dd6a5393aa20467f78 (diff)
downloadrust-cb596e3015811f6d08f45b2ebf41924a8f329c13.tar.gz
rust-cb596e3015811f6d08f45b2ebf41924a8f329c13.zip
consider `parent_count` for const param defaults
-rw-r--r--compiler/rustc_hir_analysis/src/collect/generics_of.rs2
-rw-r--r--compiler/rustc_middle/src/ty/generics.rs9
-rw-r--r--src/test/ui/const-generics/generic_const_exprs/issue-105257.rs8
-rw-r--r--src/test/ui/const-generics/generic_const_exprs/issue-105257.stderr8
4 files changed, 26 insertions, 1 deletions
diff --git a/compiler/rustc_hir_analysis/src/collect/generics_of.rs b/compiler/rustc_hir_analysis/src/collect/generics_of.rs
index 639f81f20bf..225b2ce0c50 100644
--- a/compiler/rustc_hir_analysis/src/collect/generics_of.rs
+++ b/compiler/rustc_hir_analysis/src/collect/generics_of.rs
@@ -79,7 +79,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
                     let generics = tcx.generics_of(parent_def_id.to_def_id());
                     let param_def_idx = generics.param_def_id_to_index[&param_id.to_def_id()];
                     // In the above example this would be .params[..N#0]
-                    let params = generics.params[..param_def_idx as usize].to_owned();
+                    let params = generics.param_to(param_def_idx as usize, tcx).to_owned();
                     let param_def_id_to_index =
                         params.iter().map(|param| (param.def_id, param.index)).collect();
 
diff --git a/compiler/rustc_middle/src/ty/generics.rs b/compiler/rustc_middle/src/ty/generics.rs
index a8da93e4c69..e433896eccd 100644
--- a/compiler/rustc_middle/src/ty/generics.rs
+++ b/compiler/rustc_middle/src/ty/generics.rs
@@ -220,6 +220,15 @@ impl<'tcx> Generics {
         }
     }
 
+    pub fn param_to(&'tcx self, param_index: usize, tcx: TyCtxt<'tcx>) -> &'tcx [GenericParamDef] {
+        if let Some(index) = param_index.checked_sub(self.parent_count) {
+            &self.params[..index]
+        } else {
+            tcx.generics_of(self.parent.expect("parent_count > 0 but no parent?"))
+                .param_to(param_index, tcx)
+        }
+    }
+
     /// Returns the `GenericParamDef` associated with this `EarlyBoundRegion`.
     pub fn region_param(
         &'tcx self,
diff --git a/src/test/ui/const-generics/generic_const_exprs/issue-105257.rs b/src/test/ui/const-generics/generic_const_exprs/issue-105257.rs
new file mode 100644
index 00000000000..f84918ef705
--- /dev/null
+++ b/src/test/ui/const-generics/generic_const_exprs/issue-105257.rs
@@ -0,0 +1,8 @@
+#![feature(generic_const_exprs)]
+#![allow(incomplete_features)]
+
+trait Trait<T> {
+    fn fnc<const N: usize = "">(&self) {} //~ERROR defaults for const parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions
+}
+
+fn main() {}
diff --git a/src/test/ui/const-generics/generic_const_exprs/issue-105257.stderr b/src/test/ui/const-generics/generic_const_exprs/issue-105257.stderr
new file mode 100644
index 00000000000..6b221388b0d
--- /dev/null
+++ b/src/test/ui/const-generics/generic_const_exprs/issue-105257.stderr
@@ -0,0 +1,8 @@
+error: defaults for const parameters are only allowed in `struct`, `enum`, `type`, or `trait` definitions
+  --> $DIR/issue-105257.rs:5:12
+   |
+LL |     fn fnc<const N: usize = "">(&self) {}
+   |            ^^^^^^^^^^^^^^^^^^^
+
+error: aborting due to previous error
+