Skip to content

Commit acd06de

Browse files
committed
Auto merge of rust-lang#13690 - Crauzer:vararg-type, r=Veykril
feat: Implement vararg parameter type inference This PR implements the "collection" of the `va_list` type into the function parameter types list.
2 parents 3769cc3 + b3bd5a4 commit acd06de

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

crates/hir-expand/src/name.rs

+1
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ pub mod known {
419419
shr,
420420
sub_assign,
421421
sub,
422+
va_list
422423
);
423424

424425
// self/Self cannot be used as an identifier

crates/hir-ty/src/infer.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -537,8 +537,20 @@ impl<'a> InferenceContext<'a> {
537537
let data = self.db.function_data(func);
538538
let ctx = crate::lower::TyLoweringContext::new(self.db, &self.resolver)
539539
.with_impl_trait_mode(ImplTraitLoweringMode::Param);
540-
let param_tys =
540+
let mut param_tys =
541541
data.params.iter().map(|(_, type_ref)| ctx.lower_ty(type_ref)).collect::<Vec<_>>();
542+
// Check if function contains a va_list, if it does then we append it to the parameter types
543+
// that are collected from the function data
544+
if data.is_varargs() {
545+
let va_list_ty = match self.resolve_va_list() {
546+
Some(va_list) => TyBuilder::adt(self.db, va_list)
547+
.fill_with_defaults(self.db, || self.table.new_type_var())
548+
.build(),
549+
None => self.err_ty(),
550+
};
551+
552+
param_tys.push(va_list_ty)
553+
}
542554
for (ty, pat) in param_tys.into_iter().zip(self.body.params.iter()) {
543555
let ty = self.insert_type_vars(ty);
544556
let ty = self.normalize_associated_types_in(ty);
@@ -983,6 +995,11 @@ impl<'a> InferenceContext<'a> {
983995
let trait_ = self.resolve_ops_index()?;
984996
self.db.trait_data(trait_).associated_type_by_name(&name![Output])
985997
}
998+
999+
fn resolve_va_list(&self) -> Option<AdtId> {
1000+
let struct_ = self.resolve_lang_item(name![va_list])?.as_struct()?;
1001+
Some(struct_.into())
1002+
}
9861003
}
9871004

9881005
/// When inferring an expression, we propagate downward whatever type hint we

crates/hir-ty/src/tests/patterns.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1080,3 +1080,15 @@ fn my_fn(#[cfg(feature = "feature")] u8: u8, u32: u32) {}
10801080
"#,
10811081
);
10821082
}
1083+
1084+
#[test]
1085+
fn var_args() {
1086+
check_types(
1087+
r#"
1088+
#[lang = "va_list"]
1089+
pub struct VaListImpl<'f>;
1090+
fn my_fn(foo: ...) {}
1091+
//^^^ VaListImpl
1092+
"#,
1093+
);
1094+
}

0 commit comments

Comments
 (0)