From a09d48380204fa948a3af397dc2188b93bf5793f Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 14 Jul 2020 18:23:45 +0200 Subject: Thread varargs through r-a --- crates/ra_hir_ty/src/diagnostics/expr.rs | 30 ++++++++++++++++++++++++++++ crates/ra_hir_ty/src/display.rs | 11 ++++++++-- crates/ra_hir_ty/src/infer/expr.rs | 2 +- crates/ra_hir_ty/src/lib.rs | 19 ++++++++++-------- crates/ra_hir_ty/src/lower.rs | 13 +++++++----- crates/ra_hir_ty/src/traits/builtin.rs | 2 +- crates/ra_hir_ty/src/traits/chalk/mapping.rs | 7 +++++-- 7 files changed, 65 insertions(+), 19 deletions(-) (limited to 'crates/ra_hir_ty/src') diff --git a/crates/ra_hir_ty/src/diagnostics/expr.rs b/crates/ra_hir_ty/src/diagnostics/expr.rs index 21ff99a8c..557d01cdc 100644 --- a/crates/ra_hir_ty/src/diagnostics/expr.rs +++ b/crates/ra_hir_ty/src/diagnostics/expr.rs @@ -175,6 +175,10 @@ impl<'a, 'b> ExprValidator<'a, 'b> { }; let sig = db.callable_item_signature(callee); + if sig.value.is_varargs { + return None; + } + let params = sig.value.params(); let mut param_count = params.len(); @@ -512,4 +516,30 @@ impl Foo { "#, ); } + + #[test] + fn varargs() { + check_diagnostics( + r#" +extern "C" { + fn fixed(fixed: u8); + fn varargs(fixed: u8, ...); + fn varargs2(...); +} + +fn f() { + unsafe { + fixed(0); + fixed(0, 1); + //^^^^^^^^^^^ Expected 1 argument, found 2 + varargs(0); + varargs(0, 1); + varargs2(); + varargs2(0); + varargs2(0, 1); + } +} + "#, + ) + } } diff --git a/crates/ra_hir_ty/src/display.rs b/crates/ra_hir_ty/src/display.rs index ac68c5661..c860c254c 100644 --- a/crates/ra_hir_ty/src/display.rs +++ b/crates/ra_hir_ty/src/display.rs @@ -243,10 +243,17 @@ impl HirDisplay for ApplicationTy { write!(f, ")")?; } } - TypeCtor::FnPtr { .. } => { - let sig = FnSig::from_fn_ptr_substs(&self.parameters); + TypeCtor::FnPtr { is_varargs, .. } => { + let sig = FnSig::from_fn_ptr_substs(&self.parameters, is_varargs); write!(f, "fn(")?; f.write_joined(sig.params(), ", ")?; + if is_varargs { + if sig.params().is_empty() { + write!(f, "...")?; + } else { + write!(f, ", ...")?; + } + } write!(f, ")")?; let ret = sig.ret(); if *ret != Ty::unit() { diff --git a/crates/ra_hir_ty/src/infer/expr.rs b/crates/ra_hir_ty/src/infer/expr.rs index bd9a387f5..ab586b018 100644 --- a/crates/ra_hir_ty/src/infer/expr.rs +++ b/crates/ra_hir_ty/src/infer/expr.rs @@ -220,7 +220,7 @@ impl<'a> InferenceContext<'a> { }; sig_tys.push(ret_ty.clone()); let sig_ty = Ty::apply( - TypeCtor::FnPtr { num_args: sig_tys.len() as u16 - 1 }, + TypeCtor::FnPtr { num_args: sig_tys.len() as u16 - 1, is_varargs: false }, Substs(sig_tys.clone().into()), ); let closure_ty = diff --git a/crates/ra_hir_ty/src/lib.rs b/crates/ra_hir_ty/src/lib.rs index d54568e67..c4c24a83b 100644 --- a/crates/ra_hir_ty/src/lib.rs +++ b/crates/ra_hir_ty/src/lib.rs @@ -112,7 +112,7 @@ pub enum TypeCtor { /// fn foo() -> i32 { 1 } /// let bar: fn() -> i32 = foo; /// ``` - FnPtr { num_args: u16 }, + FnPtr { num_args: u16, is_varargs: bool }, /// The never type `!`. Never, @@ -187,7 +187,7 @@ impl TypeCtor { } } } - TypeCtor::FnPtr { num_args } => num_args as usize + 1, + TypeCtor::FnPtr { num_args, is_varargs: _ } => num_args as usize + 1, TypeCtor::Tuple { cardinality } => cardinality as usize, } } @@ -667,19 +667,20 @@ pub enum TyKind { #[derive(Clone, PartialEq, Eq, Debug)] pub struct FnSig { params_and_return: Arc<[Ty]>, + is_varargs: bool, } /// A polymorphic function signature. pub type PolyFnSig = Binders; impl FnSig { - pub fn from_params_and_return(mut params: Vec, ret: Ty) -> FnSig { + pub fn from_params_and_return(mut params: Vec, ret: Ty, is_varargs: bool) -> FnSig { params.push(ret); - FnSig { params_and_return: params.into() } + FnSig { params_and_return: params.into(), is_varargs } } - pub fn from_fn_ptr_substs(substs: &Substs) -> FnSig { - FnSig { params_and_return: Arc::clone(&substs.0) } + pub fn from_fn_ptr_substs(substs: &Substs, is_varargs: bool) -> FnSig { + FnSig { params_and_return: Arc::clone(&substs.0), is_varargs } } pub fn params(&self) -> &[Ty] { @@ -724,7 +725,7 @@ impl Ty { } pub fn fn_ptr(sig: FnSig) -> Self { Ty::apply( - TypeCtor::FnPtr { num_args: sig.params().len() as u16 }, + TypeCtor::FnPtr { num_args: sig.params().len() as u16, is_varargs: sig.is_varargs }, Substs(sig.params_and_return), ) } @@ -821,7 +822,9 @@ impl Ty { fn callable_sig(&self, db: &dyn HirDatabase) -> Option { match self { Ty::Apply(a_ty) => match a_ty.ctor { - TypeCtor::FnPtr { .. } => Some(FnSig::from_fn_ptr_substs(&a_ty.parameters)), + TypeCtor::FnPtr { is_varargs, .. } => { + Some(FnSig::from_fn_ptr_substs(&a_ty.parameters, is_varargs)) + } TypeCtor::FnDef(def) => { let sig = db.callable_item_signature(def); Some(sig.subst(&a_ty.parameters)) diff --git a/crates/ra_hir_ty/src/lower.rs b/crates/ra_hir_ty/src/lower.rs index 101b8aebe..6f4398e84 100644 --- a/crates/ra_hir_ty/src/lower.rs +++ b/crates/ra_hir_ty/src/lower.rs @@ -176,9 +176,12 @@ impl Ty { Ty::apply_one(TypeCtor::Ref(*mutability), inner_ty) } TypeRef::Placeholder => Ty::Unknown, - TypeRef::Fn(params) => { + TypeRef::Fn(params, is_varargs) => { let sig = Substs(params.iter().map(|tr| Ty::from_hir(ctx, tr)).collect()); - Ty::apply(TypeCtor::FnPtr { num_args: sig.len() as u16 - 1 }, sig) + Ty::apply( + TypeCtor::FnPtr { num_args: sig.len() as u16 - 1, is_varargs: *is_varargs }, + sig, + ) } TypeRef::DynTrait(bounds) => { let self_ty = Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, 0)); @@ -996,7 +999,7 @@ fn fn_sig_for_fn(db: &dyn HirDatabase, def: FunctionId) -> PolyFnSig { let ret = Ty::from_hir(&ctx_ret, &data.ret_type); let generics = generics(db.upcast(), def.into()); let num_binders = generics.len(); - Binders::new(num_binders, FnSig::from_params_and_return(params, ret)) + Binders::new(num_binders, FnSig::from_params_and_return(params, ret, data.is_varargs)) } /// Build the declared type of a function. This should not need to look at the @@ -1047,7 +1050,7 @@ fn fn_sig_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> PolyFnS let params = fields.iter().map(|(_, field)| Ty::from_hir(&ctx, &field.type_ref)).collect::>(); let ret = type_for_adt(db, def.into()); - Binders::new(ret.num_binders, FnSig::from_params_and_return(params, ret.value)) + Binders::new(ret.num_binders, FnSig::from_params_and_return(params, ret.value, false)) } /// Build the type of a tuple struct constructor. @@ -1071,7 +1074,7 @@ fn fn_sig_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) let params = fields.iter().map(|(_, field)| Ty::from_hir(&ctx, &field.type_ref)).collect::>(); let ret = type_for_adt(db, def.parent.into()); - Binders::new(ret.num_binders, FnSig::from_params_and_return(params, ret.value)) + Binders::new(ret.num_binders, FnSig::from_params_and_return(params, ret.value, false)) } /// Build the type of a tuple enum variant constructor. diff --git a/crates/ra_hir_ty/src/traits/builtin.rs b/crates/ra_hir_ty/src/traits/builtin.rs index 86e22e459..60cc9a9f5 100644 --- a/crates/ra_hir_ty/src/traits/builtin.rs +++ b/crates/ra_hir_ty/src/traits/builtin.rs @@ -121,7 +121,7 @@ fn closure_fn_trait_impl_datum( .build(), ); let sig_ty = Ty::apply( - TypeCtor::FnPtr { num_args }, + TypeCtor::FnPtr { num_args, is_varargs: false }, Substs::builder(num_args as usize + 1) .fill_with_bound_vars(DebruijnIndex::INNERMOST, 0) .build(), diff --git a/crates/ra_hir_ty/src/traits/chalk/mapping.rs b/crates/ra_hir_ty/src/traits/chalk/mapping.rs index 06453ef82..5ba2ff51b 100644 --- a/crates/ra_hir_ty/src/traits/chalk/mapping.rs +++ b/crates/ra_hir_ty/src/traits/chalk/mapping.rs @@ -30,7 +30,7 @@ impl ToChalk for Ty { Ty::Apply(apply_ty) => match apply_ty.ctor { TypeCtor::Ref(m) => ref_to_chalk(db, m, apply_ty.parameters), TypeCtor::Array => array_to_chalk(db, apply_ty.parameters), - TypeCtor::FnPtr { num_args: _ } => { + TypeCtor::FnPtr { num_args: _, is_varargs: _ } => { let substitution = apply_ty.parameters.to_chalk(db).shifted_in(&Interner); chalk_ir::TyData::Function(chalk_ir::Fn { num_binders: 0, substitution }) .intern(&Interner) @@ -124,7 +124,10 @@ impl ToChalk for Ty { substitution.shifted_out(&Interner).expect("fn ptr should have no binders"), ); Ty::Apply(ApplicationTy { - ctor: TypeCtor::FnPtr { num_args: (parameters.len() - 1) as u16 }, + ctor: TypeCtor::FnPtr { + num_args: (parameters.len() - 1) as u16, + is_varargs: false, + }, parameters, }) } -- cgit v1.2.3 From fdce4d9f5140085c6c362ecbcf837f1b6a7d50ca Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 14 Jul 2020 18:59:07 +0200 Subject: Add FIXME --- crates/ra_hir_ty/src/traits/chalk/mapping.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'crates/ra_hir_ty/src') diff --git a/crates/ra_hir_ty/src/traits/chalk/mapping.rs b/crates/ra_hir_ty/src/traits/chalk/mapping.rs index 5ba2ff51b..3ebb55f77 100644 --- a/crates/ra_hir_ty/src/traits/chalk/mapping.rs +++ b/crates/ra_hir_ty/src/traits/chalk/mapping.rs @@ -31,6 +31,7 @@ impl ToChalk for Ty { TypeCtor::Ref(m) => ref_to_chalk(db, m, apply_ty.parameters), TypeCtor::Array => array_to_chalk(db, apply_ty.parameters), TypeCtor::FnPtr { num_args: _, is_varargs: _ } => { + // FIXME: handle is_varargs let substitution = apply_ty.parameters.to_chalk(db).shifted_in(&Interner); chalk_ir::TyData::Function(chalk_ir::Fn { num_binders: 0, substitution }) .intern(&Interner) -- cgit v1.2.3