Skip to content
Merged
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
20 changes: 3 additions & 17 deletions compiler/rustc_hir_analysis/src/delegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use rustc_middle::ty::{
use rustc_span::{ErrorGuaranteed, Span, kw};

use crate::collect::ItemCtxt;
use crate::hir_ty_lowering::{GenericArgPosition, HirTyLowerer};
use crate::hir_ty_lowering::HirTyLowerer;

type RemapTable = FxHashMap<u32, u32>;

Expand Down Expand Up @@ -581,14 +581,7 @@ fn get_delegation_user_specified_args<'tcx>(
let self_ty = get_delegation_self_ty(tcx, delegation_id);

lowerer
.lower_generic_args_of_path(
segment.ident.span,
def_id,
&[],
segment,
self_ty,
GenericArgPosition::Type,
)
.lower_generic_args_of_path(segment.ident.span, def_id, &[], segment, self_ty)
.0
.as_slice()
});
Expand All @@ -610,14 +603,7 @@ fn get_delegation_user_specified_args<'tcx>(
};

let args = lowerer
.lower_generic_args_of_path(
segment.ident.span,
def_id,
parent_args,
segment,
None,
GenericArgPosition::Value,
)
.lower_generic_args_of_path(segment.ident.span, def_id, parent_args, segment, None)
.0;

&args[parent_args.len()..]
Expand Down
9 changes: 3 additions & 6 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,17 +385,14 @@ pub fn lower_generic_args<'tcx: 'a, 'a>(

/// Checks that the correct number of generic arguments have been provided.
/// Used specifically for function calls.
pub fn check_generic_arg_count_for_call(
pub fn check_generic_arg_count_for_value_path(
cx: &dyn HirTyLowerer<'_>,
def_id: DefId,
generics: &ty::Generics,
seg: &hir::PathSegment<'_>,
is_method_call: IsMethodCall,
) -> GenericArgCountResult {
let gen_pos = match is_method_call {
IsMethodCall::Yes => GenericArgPosition::MethodCall,
IsMethodCall::No => GenericArgPosition::Value,
};
let gen_pos = GenericArgPosition::Value(is_method_call);
check_generic_arg_count(cx, def_id, seg, generics, gen_pos, generics.has_own_self())
}

Expand Down Expand Up @@ -649,7 +646,7 @@ pub(crate) fn prohibit_explicit_late_bound_lifetimes(
let note = "the late bound lifetime parameter is introduced here";
let span = args.args[0].span();

if position == GenericArgPosition::Value
if position == GenericArgPosition::Value(IsMethodCall::No)
&& args.num_lifetime_params() != param_counts.lifetimes
{
struct_span_code_err!(cx.dcx(), span, E0794, "{}", msg)
Expand Down
46 changes: 15 additions & 31 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ pub enum ExplicitLateBound {
No,
}

#[derive(Copy, Clone, PartialEq)]
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum IsMethodCall {
Yes,
No,
Expand All @@ -329,8 +329,7 @@ pub enum IsMethodCall {
#[derive(Debug, Copy, Clone, PartialEq)]
pub(crate) enum GenericArgPosition {
Type,
Value, // e.g., functions
MethodCall,
Value(IsMethodCall),
}

/// Whether to allow duplicate associated iten constraints in a trait ref, e.g.
Expand Down Expand Up @@ -561,14 +560,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
def_id: DefId,
item_segment: &hir::PathSegment<'tcx>,
) -> GenericArgsRef<'tcx> {
let (args, _) = self.lower_generic_args_of_path(
span,
def_id,
&[],
item_segment,
None,
GenericArgPosition::Type,
);
let (args, _) = self.lower_generic_args_of_path(span, def_id, &[], item_segment, None);
if let Some(c) = item_segment.args().constraints.first() {
prohibit_assoc_item_constraint(self, c, Some((def_id, item_segment, span)));
}
Expand Down Expand Up @@ -617,7 +609,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
parent_args: &[ty::GenericArg<'tcx>],
segment: &hir::PathSegment<'tcx>,
self_ty: Option<Ty<'tcx>>,
pos: GenericArgPosition,
) -> (GenericArgsRef<'tcx>, GenericArgCountResult) {
// If the type is parameterized by this region, then replace this
// region with the current anon region binding (in other words,
Expand All @@ -640,8 +631,14 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
assert!(self_ty.is_none());
}

let arg_count =
check_generic_arg_count(self, def_id, segment, generics, pos, self_ty.is_some());
let arg_count = check_generic_arg_count(
self,
def_id,
segment,
generics,
GenericArgPosition::Type,
self_ty.is_some(),
);

// Skip processing if type has no generic parameters.
// Traits always have `Self` as a generic parameter, which means they will not return early
Expand Down Expand Up @@ -826,14 +823,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
item_segment: &hir::PathSegment<'tcx>,
parent_args: GenericArgsRef<'tcx>,
) -> GenericArgsRef<'tcx> {
let (args, _) = self.lower_generic_args_of_path(
span,
item_def_id,
parent_args,
item_segment,
None,
GenericArgPosition::Type,
);
let (args, _) =
self.lower_generic_args_of_path(span, item_def_id, parent_args, item_segment, None);
if let Some(c) = item_segment.args().constraints.first() {
prohibit_assoc_item_constraint(self, c, Some((item_def_id, item_segment, span)));
}
Expand Down Expand Up @@ -945,7 +936,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
&[],
segment,
Some(self_ty),
GenericArgPosition::Type,
);

let constraints = segment.args().constraints;
Expand Down Expand Up @@ -1121,14 +1111,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
) -> ty::TraitRef<'tcx> {
self.report_internal_fn_trait(span, trait_def_id, trait_segment, is_impl);

let (generic_args, _) = self.lower_generic_args_of_path(
span,
trait_def_id,
&[],
trait_segment,
Some(self_ty),
GenericArgPosition::Type,
);
let (generic_args, _) =
self.lower_generic_args_of_path(span, trait_def_id, &[], trait_segment, Some(self_ty));
if let Some(c) = trait_segment.args().constraints.first() {
prohibit_assoc_item_constraint(self, c, Some((trait_def_id, trait_segment, span)));
}
Expand Down
11 changes: 8 additions & 3 deletions compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rustc_hir::lang_items::LangItem;
use rustc_hir::{self as hir, AmbigArg, ExprKind, GenericArg, HirId, Node, QPath, intravisit};
use rustc_hir_analysis::hir_ty_lowering::errors::GenericsArgsErrExtend;
use rustc_hir_analysis::hir_ty_lowering::generics::{
check_generic_arg_count_for_call, lower_generic_args,
check_generic_arg_count_for_value_path, lower_generic_args,
};
use rustc_hir_analysis::hir_ty_lowering::{
ExplicitLateBound, GenericArgCountMismatch, GenericArgCountResult, GenericArgsLowerer,
Expand Down Expand Up @@ -1098,8 +1098,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// parameter internally, but we don't allow users to specify the
// parameter's value explicitly, so we have to do some error-
// checking here.
let arg_count =
check_generic_arg_count_for_call(self, def_id, generics, seg, IsMethodCall::No);
let arg_count = check_generic_arg_count_for_value_path(
self,
def_id,
generics,
seg,
IsMethodCall::No,
);

if let ExplicitLateBound::Yes = arg_count.explicit_late_bound {
explicit_late_bound = ExplicitLateBound::Yes;
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir_typeck/src/method/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rustc_hir as hir;
use rustc_hir::GenericArg;
use rustc_hir::def_id::DefId;
use rustc_hir_analysis::hir_ty_lowering::generics::{
check_generic_arg_count_for_call, lower_generic_args,
check_generic_arg_count_for_value_path, lower_generic_args,
};
use rustc_hir_analysis::hir_ty_lowering::{
GenericArgsLowerer, HirTyLowerer, IsMethodCall, RegionInferReason,
Expand Down Expand Up @@ -403,7 +403,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
// variables.
let generics = self.tcx.generics_of(pick.item.def_id);

let arg_count_correct = check_generic_arg_count_for_call(
let arg_count_correct = check_generic_arg_count_for_value_path(
self.fcx,
pick.item.def_id,
generics,
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/delegation/generics/generics-gen-args-errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ mod test_1 {
//~| ERROR can't use generic parameters from outer item
//~| ERROR can't use generic parameters from outer item
//~| ERROR: unresolved item provided when a constant was expected
//~| ERROR: function takes 2 lifetime arguments but 0 lifetime arguments were supplied
}
}

Expand All @@ -47,6 +48,7 @@ mod test_2 {

reuse foo::<String, String> as bar2;
//~^ ERROR: function takes 3 generic arguments but 2 generic arguments were supplied
//~| ERROR: function takes 2 lifetime arguments but 0 lifetime arguments were supplied

reuse foo::<'static, _, 'asdasd, 'static, 'static, 'static, _> as bar3;
//~^ ERROR: use of undeclared lifetime name `'asdasd`
Expand All @@ -58,10 +60,12 @@ mod test_2 {

reuse foo::<1, 2, _, 4, 5, _> as bar5;
//~^ ERROR: function takes 3 generic arguments but 6 generic arguments were supplied
//~| ERROR: function takes 2 lifetime arguments but 0 lifetime arguments were supplied

reuse foo::<1, 2,asd,String, { let x = 0; }> as bar6;
//~^ ERROR: cannot find type `asd` in this scope
//~| ERROR: function takes 3 generic arguments but 5 generic arguments were supplied
//~| ERROR: function takes 2 lifetime arguments but 0 lifetime arguments were supplied

reuse foo::<"asdasd", asd, "askdn", 'static, 'a> as bar7;
//~^ ERROR: use of undeclared lifetime name `'a`
Expand All @@ -70,6 +74,7 @@ mod test_2 {

reuse foo::<{}, {}, {}> as bar8;
//~^ ERROR: constant provided when a type was expected
//~| ERROR: function takes 2 lifetime arguments but 0 lifetime arguments were supplied
}

mod test_3 {
Expand Down Expand Up @@ -107,12 +112,14 @@ mod test_3 {
//~| ERROR: trait takes 3 lifetime arguments but 1 lifetime argument was supplied
//~| ERROR: trait takes 2 generic arguments but 3 generic arguments were supplied
//~| ERROR: method takes 2 generic arguments but 6 generic arguments were supplied
//~| ERROR: method takes 1 lifetime argument but 0 lifetime arguments were supplied

reuse Trait::<Trait, Clone, _, 'static, dyn Send, _>::foo::<1, 2, 3, _, 6> as bar7;
//~^ ERROR: missing lifetime specifiers [E0106]
//~| ERROR: trait takes 3 lifetime arguments but 1 lifetime argument was supplied
//~| ERROR: trait takes 2 generic arguments but 5 generic arguments were supplied
//~| ERROR: method takes 2 generic arguments but 5 generic arguments were supplied
//~| ERROR: method takes 1 lifetime argument but 0 lifetime arguments were supplied
}

fn main() {}
Loading
Loading