Skip to content

Add try_type_param and param_at to avoid array out of bound #138219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions compiler/rustc_middle/src/ty/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,19 @@ impl<'tcx> Generics {

/// Returns the `GenericParamDef` with the given index.
pub fn param_at(&'tcx self, param_index: usize, tcx: TyCtxt<'tcx>) -> &'tcx GenericParamDef {
self.try_param_at(param_index, tcx).expect("bug: invalid parameter index")
}

pub fn try_param_at(
&'tcx self,
param_index: usize,
tcx: TyCtxt<'tcx>,
) -> Option<&'tcx GenericParamDef> {
if let Some(index) = param_index.checked_sub(self.parent_count) {
&self.own_params[index]
self.own_params.get(index)
} else {
tcx.generics_of(self.parent.expect("parent_count > 0 but no parent?"))
.param_at(param_index, tcx)
.try_param_at(param_index, tcx)
}
}

Expand Down Expand Up @@ -260,6 +268,16 @@ impl<'tcx> Generics {
}
}

pub fn try_type_param(
&'tcx self,
param: ParamTy,
tcx: TyCtxt<'tcx>,
) -> Option<&'tcx GenericParamDef> {
self.try_param_at(param.index as usize, tcx).and_then(|param| {
if matches!(param.kind, GenericParamDefKind::Type { .. }) { Some(param) } else { None }
})
}

/// Returns the `GenericParamDef` associated with this `ParamConst`.
pub fn const_param(&'tcx self, param: ParamConst, tcx: TyCtxt<'tcx>) -> &'tcx GenericParamDef {
let param = self.param_at(param.index as usize, tcx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,14 +241,17 @@ impl<T> Trait<T> for X {
}
(ty::Param(p), _) | (_, ty::Param(p)) => {
let generics = tcx.generics_of(body_owner_def_id);
let p_span = tcx.def_span(generics.type_param(p, tcx).def_id);
let expected = match (values.expected.kind(), values.found.kind()) {
(ty::Param(_), _) => "expected ",
(_, ty::Param(_)) => "found ",
_ => "",
};
if !sp.contains(p_span) {
diag.span_label(p_span, format!("{expected}this type parameter"));
let type_param = generics.try_type_param(p, tcx);
if let Some(type_param) = type_param {
let p_span = tcx.def_span(type_param.def_id);
let expected = match (values.expected.kind(), values.found.kind()) {
(ty::Param(_), _) => "expected ",
(_, ty::Param(_)) => "found ",
_ => "",
};
if !sp.contains(p_span) {
diag.span_label(p_span, format!("{expected}this type parameter"));
}
}
}
(ty::Alias(ty::Projection | ty::Inherent, proj_ty), _)
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/generics/generic-param-issue-137865.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//@ edition: 2021


trait Foo {
type Assoc<const N: Self>; //~ ERROR: `Self` is forbidden as the type of a const generic parameter
fn foo() -> Self::Assoc<3>; //~ ERROR: mismatched types
}

fn main() {}
20 changes: 20 additions & 0 deletions tests/ui/generics/generic-param-issue-137865.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error: `Self` is forbidden as the type of a const generic parameter
--> $DIR/generic-param-issue-137865.rs:5:25
|
LL | type Assoc<const N: Self>;
| ^^^^
|
= note: the only supported types are integers, `bool`, and `char`

error[E0308]: mismatched types
--> $DIR/generic-param-issue-137865.rs:6:29
|
LL | fn foo() -> Self::Assoc<3>;
| ^ expected type parameter `Self`, found integer
|
= note: expected type parameter `Self`
found type `{integer}`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.
Loading