diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs index 7dc4b8d7674ca..6059ede2ecdc6 100644 --- a/compiler/rustc_hir_typeck/src/expr.rs +++ b/compiler/rustc_hir_typeck/src/expr.rs @@ -1515,9 +1515,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { .type_of(param.def_id) .instantiate(self.tcx, method.args) .skip_norm_wip(); + // Insert function argument const param types back into the inputs. // We use pos - 1 since method_inputs skips self, but arg_pos counts it. - method_inputs.to_mut().insert(pos as usize - 1, ty); + method_inputs.to_mut().insert( + pos.checked_sub(1).expect("pos should always be greater than 0") as usize, + ty, + ); } let method_tuple_args_flag = diff --git a/compiler/rustc_ty_utils/src/assoc.rs b/compiler/rustc_ty_utils/src/assoc.rs index bff2f1e6804af..6b30019127f27 100644 --- a/compiler/rustc_ty_utils/src/assoc.rs +++ b/compiler/rustc_ty_utils/src/assoc.rs @@ -2,7 +2,7 @@ use rustc_hir::def::DefKind; use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId}; use rustc_hir::definitions::{DefPathData, PerParentDisambiguatorState}; use rustc_hir::intravisit::{self, Visitor}; -use rustc_hir::{self as hir, ImplItemImplKind, ItemKind}; +use rustc_hir::{self as hir, Generics, ImplItemImplKind, ItemKind}; use rustc_middle::query::Providers; use rustc_middle::ty::{self, ImplTraitInTraitData, TyCtxt}; use rustc_span::symbol::kw; @@ -76,8 +76,14 @@ fn associated_item(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::AssocItem { assoc_item } -fn fn_has_self_parameter(tcx: TyCtxt<'_>, owner_id: hir::OwnerId) -> bool { +fn fn_has_self_parameter(tcx: TyCtxt<'_>, owner_id: hir::OwnerId, generics: &Generics<'_>) -> bool { + // With `function_arg_const_generics`, const parameters are moved into the + // generics. We check the original `arg_pos` to avoid treating a later `self` + // as a valid receiver after the const parameters are removed. matches!(tcx.fn_arg_idents(owner_id.def_id), [Some(Ident { name: kw::SelfLower, .. }), ..]) + && !generics.params.iter().any(|param| { + matches!(param.kind, hir::GenericParamKind::Const { arg_pos: Some(0), .. }) + }) } fn associated_item_from_trait_item( @@ -88,9 +94,10 @@ fn associated_item_from_trait_item( let name = trait_item.ident.name; let kind = match trait_item.kind { hir::TraitItemKind::Const(_, _) => ty::AssocKind::Const { name }, - hir::TraitItemKind::Fn { .. } => { - ty::AssocKind::Fn { name, has_self: fn_has_self_parameter(tcx, owner_id) } - } + hir::TraitItemKind::Fn { .. } => ty::AssocKind::Fn { + name, + has_self: fn_has_self_parameter(tcx, owner_id, trait_item.generics), + }, hir::TraitItemKind::Type { .. } => { ty::AssocKind::Type { data: ty::AssocTypeData::Normal(name) } } @@ -104,9 +111,10 @@ fn associated_item_from_impl_item(tcx: TyCtxt<'_>, impl_item: &hir::ImplItem<'_> let name = impl_item.ident.name; let kind = match impl_item.kind { hir::ImplItemKind::Const(..) => ty::AssocKind::Const { name }, - hir::ImplItemKind::Fn(..) => { - ty::AssocKind::Fn { name, has_self: fn_has_self_parameter(tcx, owner_id) } - } + hir::ImplItemKind::Fn(..) => ty::AssocKind::Fn { + name, + has_self: fn_has_self_parameter(tcx, owner_id, impl_item.generics), + }, hir::ImplItemKind::Type(..) => { ty::AssocKind::Type { data: ty::AssocTypeData::Normal(name) } } diff --git a/tests/ui/const-generics/function_arg_const_generics/self-param-after-const-param.rs b/tests/ui/const-generics/function_arg_const_generics/self-param-after-const-param.rs new file mode 100644 index 0000000000000..f9de0a02112bd --- /dev/null +++ b/tests/ui/const-generics/function_arg_const_generics/self-param-after-const-param.rs @@ -0,0 +1,16 @@ +#![feature(function_arg_const_generics, gca_min_const_items)] + +trait Foo { + fn a(const N: usize, self: [isize; N]) -> isize; //~ ERROR + fn b(&self) -> isize { + self.a() + 2 //~ ERROR E0599 + } +} + +impl Foo for isize { + fn a(&self) -> isize { //~ ERROR E0185 + 3 + } +} + +fn main() {} diff --git a/tests/ui/const-generics/function_arg_const_generics/self-param-after-const-param.stderr b/tests/ui/const-generics/function_arg_const_generics/self-param-after-const-param.stderr new file mode 100644 index 0000000000000..0253d69602aff --- /dev/null +++ b/tests/ui/const-generics/function_arg_const_generics/self-param-after-const-param.stderr @@ -0,0 +1,42 @@ +error: unexpected `self` parameter in function + --> $DIR/self-param-after-const-param.rs:4:26 + | +LL | fn a(const N: usize, self: [isize; N]) -> isize; + | ^^^^ must be the first parameter of an associated function + +error[E0185]: method `a` has a `&self` declaration in the impl, but not in the trait + --> $DIR/self-param-after-const-param.rs:11:5 + | +LL | fn a(const N: usize, self: [isize; N]) -> isize; + | ------------------------------------------------ trait method declared without `&self` +... +LL | fn a(&self) -> isize { + | ^^^^^^^^^^^^^^^^^^^^ `&self` used in impl + +error[E0599]: no method named `a` found for reference `&Self` in the current scope + --> $DIR/self-param-after-const-param.rs:6:14 + | +LL | self.a() + 2 + | ^ this is an associated function, not a method + | + = note: found the following associated functions; to be used as methods, functions must have a `self` parameter +note: the candidate is defined in the trait `Foo` + --> $DIR/self-param-after-const-param.rs:4:5 + | +LL | fn a(const N: usize, self: [isize; N]) -> isize; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: use associated function syntax instead + | +LL - self.a() + 2 +LL + Self::a() + 2 + | +help: there is a method `b` with a similar name + | +LL - self.a() + 2 +LL + self.b() + 2 + | + +error: aborting due to 3 previous errors + +Some errors have detailed explanations: E0185, E0599. +For more information about an error, try `rustc --explain E0185`.