mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 11:31:15 +00:00
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:
parent
7959c24876
commit
4a1423337f
@ -299,6 +299,7 @@ impl SourceAnalyzer {
|
|||||||
let parent = || parent.clone();
|
let parent = || parent.clone();
|
||||||
|
|
||||||
let mut prefer_value_ns = false;
|
let mut prefer_value_ns = false;
|
||||||
|
let resolved = (|| {
|
||||||
if let Some(path_expr) = parent().and_then(ast::PathExpr::cast) {
|
if let Some(path_expr) = parent().and_then(ast::PathExpr::cast) {
|
||||||
let expr_id = self.expr_id(db, &path_expr.into())?;
|
let expr_id = self.expr_id(db, &path_expr.into())?;
|
||||||
let infer = self.infer.as_ref()?;
|
let infer = self.infer.as_ref()?;
|
||||||
@ -328,17 +329,24 @@ impl SourceAnalyzer {
|
|||||||
{
|
{
|
||||||
return Some(PathResolution::Def(ModuleDef::Variant(variant.into())));
|
return Some(PathResolution::Def(ModuleDef::Variant(variant.into())));
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
|
|
||||||
let record_pat = parent().and_then(ast::RecordPat::cast).map(ast::Pat::from);
|
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) {
|
if let Some(pat) = record_pat.or_else(tuple_struct_pat) {
|
||||||
let pat_id = self.pat_id(&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 {
|
if let Some(VariantId::EnumVariantId(variant)) = variant_res_for_pat {
|
||||||
return Some(PathResolution::Def(ModuleDef::Variant(variant.into())));
|
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.
|
// This must be a normal source file rather than macro file.
|
||||||
let hygiene = Hygiene::new(db.upcast(), self.file_id);
|
let hygiene = Hygiene::new(db.upcast(), self.file_id);
|
||||||
|
@ -295,13 +295,13 @@ impl<'a> Ctx<'a> {
|
|||||||
let mut pat = param.pat();
|
let mut pat = param.pat();
|
||||||
// FIXME: This really shouldn't be here, in fact FunctionData/ItemTree's function shouldn't know about
|
// FIXME: This really shouldn't be here, in fact FunctionData/ItemTree's function shouldn't know about
|
||||||
// pattern names at all
|
// pattern names at all
|
||||||
let name = loop {
|
let name = 'name: loop {
|
||||||
match pat {
|
match pat {
|
||||||
Some(ast::Pat::RefPat(ref_pat)) => pat = ref_pat.pat(),
|
Some(ast::Pat::RefPat(ref_pat)) => pat = ref_pat.pat(),
|
||||||
Some(ast::Pat::IdentPat(ident)) => {
|
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))
|
self.data().params.alloc(Param::Normal(name, ty))
|
||||||
|
@ -118,6 +118,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
|
|||||||
<span class="brace">}</span>
|
<span class="brace">}</span>
|
||||||
|
|
||||||
<span class="keyword">fn</span> <span class="function declaration">const_param</span><span class="angle"><</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">></span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="operator">-></span> <span class="builtin_type">usize</span> <span class="brace">{</span>
|
<span class="keyword">fn</span> <span class="function declaration">const_param</span><span class="angle"><</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">></span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="operator">-></span> <span class="builtin_type">usize</span> <span class="brace">{</span>
|
||||||
|
<span class="function">const_param</span><span class="operator">::</span><span class="angle"><</span><span class="brace">{</span> <span class="const_param">FOO</span> <span class="brace">}</span><span class="angle">></span><span class="parenthesis">(</span><span class="parenthesis">)</span><span class="semicolon">;</span>
|
||||||
<span class="const_param">FOO</span>
|
<span class="const_param">FOO</span>
|
||||||
<span class="brace">}</span>
|
<span class="brace">}</span>
|
||||||
|
|
||||||
|
@ -172,6 +172,7 @@ fn never() -> ! {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn const_param<const FOO: usize>() -> usize {
|
fn const_param<const FOO: usize>() -> usize {
|
||||||
|
const_param::<{ FOO }>();
|
||||||
FOO
|
FOO
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user