pub attrs: Attrs,
pub visibility: RawVisibility,
pub abi: Option<Interned<str>>,
+ pub legacy_const_generics_indices: Vec<u32>,
flags: FnFlags,
}
flags.bits |= FnFlags::IS_IN_EXTERN_BLOCK;
}
+ let legacy_const_generics_indices = item_tree
+ .attrs(db, krate, ModItem::from(loc.id.value).into())
+ .by_key("rustc_legacy_const_generics")
+ .tt_values()
+ .next()
+ .map(|arg| parse_rustc_legacy_const_generics(arg))
+ .unwrap_or_default();
+
Arc::new(FunctionData {
name: func.name.clone(),
params: enabled_params
attrs: item_tree.attrs(db, krate, ModItem::from(loc.id.value).into()),
visibility: item_tree[func.visibility].clone(),
abi: func.abi.clone(),
+ legacy_const_generics_indices,
flags,
})
}
}
}
+fn parse_rustc_legacy_const_generics(tt: &tt::Subtree) -> Vec<u32> {
+ let mut indices = Vec::new();
+ for args in tt.token_trees.chunks(2) {
+ match &args[0] {
+ tt::TokenTree::Leaf(tt::Leaf::Literal(lit)) => match lit.text.parse() {
+ Ok(index) => indices.push(index),
+ Err(_) => break,
+ },
+ _ => break,
+ }
+
+ if let Some(comma) = args.get(1) {
+ match comma {
+ tt::TokenTree::Leaf(tt::Leaf::Punct(punct)) if punct.char == ',' => {}
+ _ => break,
+ }
+ }
+ }
+
+ indices
+}
+
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TypeAliasData {
pub name: Name,
return;
}
- let params = sig.params();
+ if sig.legacy_const_generics_indices.is_empty() {
+ let mut param_count = sig.params().len();
- let mut param_count = params.len();
-
- if arg_count != param_count {
- if is_method_call {
- param_count -= 1;
- arg_count -= 1;
+ if arg_count != param_count {
+ if is_method_call {
+ param_count -= 1;
+ arg_count -= 1;
+ }
+ self.diagnostics.push(BodyValidationDiagnostic::MismatchedArgCount {
+ call_expr: call_id,
+ expected: param_count,
+ found: arg_count,
+ });
+ }
+ } else {
+ // With `#[rustc_legacy_const_generics]` there are basically two parameter counts that
+ // are allowed.
+ let count_non_legacy = sig.params().len();
+ let count_legacy = sig.params().len() + sig.legacy_const_generics_indices.len();
+ if arg_count != count_non_legacy && arg_count != count_legacy {
+ self.diagnostics.push(BodyValidationDiagnostic::MismatchedArgCount {
+ call_expr: call_id,
+ // Since most users will use the legacy way to call them, report against that.
+ expected: count_legacy,
+ found: arg_count,
+ });
}
- self.diagnostics.push(BodyValidationDiagnostic::MismatchedArgCount {
- call_expr: call_id,
- expected: param_count,
- found: arg_count,
- });
}
}
pub struct CallableSig {
params_and_return: Arc<[Ty]>,
is_varargs: bool,
+ legacy_const_generics_indices: Arc<[u32]>,
}
has_interner!(CallableSig);
impl CallableSig {
pub fn from_params_and_return(mut params: Vec<Ty>, ret: Ty, is_varargs: bool) -> CallableSig {
params.push(ret);
- CallableSig { params_and_return: params.into(), is_varargs }
+ CallableSig {
+ params_and_return: params.into(),
+ is_varargs,
+ legacy_const_generics_indices: Arc::new([]),
+ }
}
pub fn from_fn_ptr(fn_ptr: &FnPointer) -> CallableSig {
.map(|arg| arg.assert_ty_ref(&Interner).clone())
.collect(),
is_varargs: fn_ptr.sig.variadic,
+ legacy_const_generics_indices: Arc::new([]),
}
}
+ pub fn set_legacy_const_generics_indices(&mut self, indices: &[u32]) {
+ self.legacy_const_generics_indices = indices.into();
+ }
+
pub fn to_fn_ptr(&self) -> FnPointer {
FnPointer {
num_binders: 0,
{
let vec = self.params_and_return.to_vec();
let folded = vec.fold_with(folder, outer_binder)?;
- Ok(CallableSig { params_and_return: folded.into(), is_varargs: self.is_varargs })
+ Ok(CallableSig {
+ params_and_return: folded.into(),
+ is_varargs: self.is_varargs,
+ legacy_const_generics_indices: self.legacy_const_generics_indices,
+ })
}
}
.with_type_param_mode(TypeParamLoweringMode::Variable);
let ret = ctx_ret.lower_ty(&data.ret_type);
let generics = generics(db.upcast(), def.into());
- make_binders(&generics, CallableSig::from_params_and_return(params, ret, data.is_varargs()))
+ let mut sig = CallableSig::from_params_and_return(params, ret, data.is_varargs());
+ if !data.legacy_const_generics_indices.is_empty() {
+ sig.set_legacy_const_generics_indices(&data.legacy_const_generics_indices);
+ }
+ make_binders(&generics, sig)
}
/// Build the declared type of a function. This should not need to look at the
fixed(0);
varargs(1, 2, 3);
}
+}
+ "#,
+ )
+ }
+
+ #[test]
+ fn legacy_const_generics() {
+ check_diagnostics(
+ r#"
+#[rustc_legacy_const_generics(1, 3)]
+fn mixed<const N1: &'static str, const N2: bool>(
+ a: u8,
+ b: i8,
+) {}
+
+fn f() {
+ mixed(0, "", -1, true);
+ mixed::<"", true>(0, -1);
+}
+
+#[rustc_legacy_const_generics(1, 3)]
+fn b<const N1: u8, const N2: u8>(
+ a: u8,
+ b: u8,
+) {}
+
+fn g() {
+ b(0, 1, 2, 3);
+ b::<1, 3>(0, 2);
+
+ b(0, 1, 2);
+ //^ error: expected 4 arguments, found 3
}
"#,
)