-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MIR episode 3 #14368
MIR episode 3 #14368
Conversation
e5d1bd2
to
b91ac56
Compare
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only reviewed the first commit (try desugaring) for now, will take another look at the rest later
crates/hir-def/src/body/lower.rs
Outdated
@@ -199,6 +204,10 @@ impl ExprCollector<'_> { | |||
self.source_map.pat_map.insert(src, id); | |||
id | |||
} | |||
// desugared pats don't have ptr, that's wrong and should be fixed somehow. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a FIXME, and ideally we should fix this before doing more desugaring as this will degrade IDE features otherwise
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the FIXME.
I copied this comment from the existing alloc_expr_desugared
. I didn't use alloc_expr_desugared
and used the syntax_ptr of the root expression for generated expressions. Is it correct? And is there any feature that might not work due usage of alloc_pat_desugared
in try desugaring?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I took another look, this will only be problematic for diagnostics it seems (that is the desugared stuff causes some diagnostics to trigger, we won't show them then).
pub enum Path { | ||
/// A normal path | ||
Normal { | ||
/// Type based path like `<T>::foo`. | ||
/// Note that paths like `<Type as Trait>::foo` are desugared to `Trait::<Self=Type>::foo`. | ||
type_anchor: Option<Interned<TypeRef>>, | ||
mod_path: Interned<ModPath>, | ||
/// Invariant: the same len as `self.mod_path.segments` or `None` if all segments are `None`. | ||
generic_args: Option<Box<[Option<Interned<GenericArgs>>]>>, | ||
}, | ||
/// A link to a lang item. It is used in desugaring of things like `x?`. We can show these | ||
/// links via a normal path since they might be private and not accessible in the usage place. | ||
LangItem(LangItemTarget), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not too fond of this change, I see why the need for it is there but I wonder if we can solve this in a better way that doesn't require enum-ifying it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can add it to PathKind
which is already an enum, but it doesn't look great since segments, type anchor and generic args doesn't really mean for lang item paths.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That won't work as PathKind
comes from hir-expand which does not depend on hir-def (so no LangItemTarget
there)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, this is fine, let's keep it this way.
@@ -245,8 +265,24 @@ impl Resolver { | |||
pub fn resolve_path_in_value_ns( | |||
&self, | |||
db: &dyn DefDatabase, | |||
path: &ModPath, | |||
path: &Path, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This likewise isn't too nice, the resolver should not care about generics and what not, so it now taking a Path
feels a bit weird. (Though maybe its time for us to take another look at how we want to encode hir paths anyways)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OTOH it saves a bunch of .mod_path()
in callers. Maybe it make sense to have another method that gets a ModPath
and this function call it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though maybe its time for us to take another look at how we want to encode hir paths anyways
I would suggest encoding paths as resolved ids and (partially) resolving them in hir lowering. Does it have any problem that current state avoid it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After a second thought this should be fine as well I think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it simplifies many things. I will try to do it in another PR.
Given MIR will grow from the looks of it, would it make sense (and is it feasible) to split the MIR things out into its own crate? |
dd46d07
to
7c9e99d
Compare
I missed this one:
MIR is dependent on type definitions, and type checking / lowering depends on MIR (due const evaluation), so it is possible to split the current |
f3bd169
to
7bc1761
Compare
Right, these things are very much intertwined, I don't think we gain anything from splitting these into crates then so we should just try to keep the modules cleanly apart where we can |
2064ae1
to
2514bcf
Compare
source_map: BodySourceMap, | ||
current_try_block: Option<LabelId>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes me wonder if we should make label resolution be part of the body lowering (not in this PR as it is already way too big, but just a thougght)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fully agree, we should resolve everything we can in body lowering.
/// Generates a new name which is only equal to itself, by incrementing a counter. Due | ||
/// its implementation, it should not be used in things that salsa considers, like | ||
/// type names or field names, and it should be only used in names of local variables | ||
/// and labels and similar things. | ||
pub fn generate_new_name() -> Name { | ||
use std::sync::atomic::{AtomicUsize, Ordering}; | ||
static CNT: AtomicUsize = AtomicUsize::new(0); | ||
let c = CNT.fetch_add(1, Ordering::Relaxed); | ||
// FIXME: Currently a `__RA_generated_name` in user code will break our analysis | ||
Name::new_text(format!("__RA_geneated_name_{c}").into()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you put a FIXME on this that we should really move to interned symbols (with support for unique name generation)? A similar comment already exists on missing
but thats also missing a FIXME. The fact that we use SmolStr
for names is something I really really dislike (which becomes apparent here)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function should be removed entirely when we resolve paths in hir lowering.
@@ -1126,5 +1126,5 @@ fn benchmark_syntax_highlighting_parser() { | |||
.filter(|it| it.highlight.tag == HlTag::Symbol(SymbolKind::Function)) | |||
.count() | |||
}; | |||
assert_eq!(hash, 1608); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems odd, we effectively resolve less things as functions in this test now? Can you make sure that analysis-stats didn't regress?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh actually, this might be because this test runs without the sysroot which this PR might cause to regress some resolutions depending on things in there (which is fine, but verifying analysis-stats would be good if you haven't yet)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right. analysis-stats
has 31 ??ty
now.
@@ -181,7 +181,7 @@ pub mod convert { | |||
} | |||
// endregion:as_ref | |||
// region:infallible | |||
pub enum Infallibe {} | |||
pub enum Infallible {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thing I just realized regarding minicore, we should have a test that verifies minicore contains no unresolved things somehow to guard against accidental typos. (Again something for a follow up)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea. Long term I think we also need something to make sure it is in sync with real core, or reusing the real core somehow.
I will go ahead and merge this to have it in the next nightly. I will address current (and future, if any) reviews in follow up PRs. @bors r+ |
☀️ Test successful - checks-actions |
This PR adds lowering for try operator and overloaded dereference, and adds evaluating support for function pointers and trait objects. It also adds a flag to
analysis-stats
to show percentage of functions that it fails to emit mir for them, which is currently20%
(which is somehow lying, as most of the supported80%
are tests). The most offenders are closure (1975 items) and overloaded index (415 items). I will try to add overloaded index before monday to have it in this PR, and tackle the closure in the next episode.