fix: Attempt to resolve paths in const arguments heuristically

While we don't support const args in type inference yet, we can at least
make use of the fallback path resolution to resolve paths in const args
in the IDE layer to enable some features for them.
This commit is contained in:
Lukas Wirth 2022-04-06 20:22:12 +02:00
parent 7959c24876
commit 4a1423337f
4 changed files with 51 additions and 41 deletions

View File

@ -299,6 +299,7 @@ pub(crate) fn resolve_path(
let parent = || parent.clone();
let mut prefer_value_ns = false;
let resolved = (|| {
if let Some(path_expr) = parent().and_then(ast::PathExpr::cast) {
let expr_id = self.expr_id(db, &path_expr.into())?;
let infer = self.infer.as_ref()?;
@ -328,17 +329,24 @@ pub(crate) fn resolve_path(
{
return Some(PathResolution::Def(ModuleDef::Variant(variant.into())));
}
}
} else {
let record_pat = parent().and_then(ast::RecordPat::cast).map(ast::Pat::from);
let tuple_struct_pat = || parent().and_then(ast::TupleStructPat::cast).map(ast::Pat::from);
let tuple_struct_pat =
|| parent().and_then(ast::TupleStructPat::cast).map(ast::Pat::from);
if let Some(pat) = record_pat.or_else(tuple_struct_pat) {
let pat_id = self.pat_id(&pat)?;
let variant_res_for_pat = self.infer.as_ref()?.variant_resolution_for_pat(pat_id);
let variant_res_for_pat =
self.infer.as_ref()?.variant_resolution_for_pat(pat_id);
if let Some(VariantId::EnumVariantId(variant)) = variant_res_for_pat {
return Some(PathResolution::Def(ModuleDef::Variant(variant.into())));
}
}
}
None
})();
if let resolved @ Some(_) = resolved {
return resolved;
}
// This must be a normal source file rather than macro file.
let hygiene = Hygiene::new(db.upcast(), self.file_id);

View File

@ -295,13 +295,13 @@ fn lower_function(&mut self, func: &ast::Fn) -> Option<FileItemTreeId<Function>>
let mut pat = param.pat();
// FIXME: This really shouldn't be here, in fact FunctionData/ItemTree's function shouldn't know about
// pattern names at all
let name = loop {
let name = 'name: loop {
match pat {
Some(ast::Pat::RefPat(ref_pat)) => pat = ref_pat.pat(),
Some(ast::Pat::IdentPat(ident)) => {
break ident.name().map(|it| it.as_name())
break 'name ident.name().map(|it| it.as_name())
}
_ => break None,
_ => break 'name None,
}
};
self.data().params.alloc(Param::Normal(name, ty))

View File

@ -118,6 +118,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
<span class="brace">}</span>
<span class="keyword">fn</span> <span class="function declaration">const_param</span><span class="angle">&lt;</span><span class="keyword">const</span> <span class="const_param declaration">FOO</span><span class="colon">:</span> <span class="builtin_type">usize</span><span class="angle">&gt;</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="operator">-&gt;</span> <span class="builtin_type">usize</span> <span class="brace">{</span>
<span class="function">const_param</span><span class="operator">::</span><span class="angle">&lt;</span><span class="brace">{</span> <span class="const_param">FOO</span> <span class="brace">}</span><span class="angle">&gt;</span><span class="parenthesis">(</span><span class="parenthesis">)</span><span class="semicolon">;</span>
<span class="const_param">FOO</span>
<span class="brace">}</span>

View File

@ -172,6 +172,7 @@ fn never() -> ! {
}
fn const_param<const FOO: usize>() -> usize {
const_param::<{ FOO }>();
FOO
}