Skip to content
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

Merged
merged 13 commits into from
Mar 21, 2023
Merged

MIR episode 3 #14368

merged 13 commits into from
Mar 21, 2023

Conversation

HKalbasi
Copy link
Member

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 currently 20% (which is somehow lying, as most of the supported 80% 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.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 16, 2023
@HKalbasi HKalbasi force-pushed the mir branch 3 times, most recently from e5d1bd2 to b91ac56 Compare March 16, 2023 22:31
@HKalbasi
Copy link
Member Author

HKalbasi commented Mar 17, 2023

benchmark_syntax_highlighting_parser doesn't fail on my machine, and it seems unstable (IIRC I hit it another time). Would it make sense to ignore it? nevermind, it was another syntax highlighting test.

Copy link
Member

@Veykril Veykril left a 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

@@ -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.
Copy link
Member

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

Copy link
Member Author

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?

Copy link
Member

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).

crates/hir-def/src/body/lower.rs Outdated Show resolved Hide resolved
Comment on lines +40 to +52
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),
Copy link
Member

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

Copy link
Member Author

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.

Copy link
Member

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)

Copy link
Member

@Veykril Veykril Mar 20, 2023

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.

crates/hir-expand/src/name.rs Outdated Show resolved Hide resolved
@@ -245,8 +265,24 @@ impl Resolver {
pub fn resolve_path_in_value_ns(
&self,
db: &dyn DefDatabase,
path: &ModPath,
path: &Path,
Copy link
Member

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)

Copy link
Member Author

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?

Copy link
Member Author

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?

Copy link
Member

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

Copy link
Member Author

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.

@Veykril
Copy link
Member

Veykril commented Mar 17, 2023

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?

@HKalbasi HKalbasi force-pushed the mir branch 2 times, most recently from dd46d07 to 7c9e99d Compare March 17, 2023 10:18
crates/hir-ty/src/traits.rs Outdated Show resolved Hide resolved
crates/hir-def/src/body/lower.rs Outdated Show resolved Hide resolved
crates/hir-def/src/body/lower.rs Show resolved Hide resolved
crates/hir-def/src/body/lower.rs Outdated Show resolved Hide resolved
crates/hir-ty/src/infer/unify.rs Outdated Show resolved Hide resolved
@HKalbasi
Copy link
Member Author

I missed this one:

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?

MIR is dependent on type definitions, and type checking / lowering depends on MIR (due const evaluation), so it is possible to split the current hir-ty crate into 3 crates: hir-ty-def, mir and hir-ty-impl. Whether we should do it or not is another topic and I'm very neutral on that.

@HKalbasi HKalbasi force-pushed the mir branch 3 times, most recently from f3bd169 to 7bc1761 Compare March 19, 2023 09:02
@HKalbasi HKalbasi requested a review from lowr March 19, 2023 09:03
@Veykril
Copy link
Member

Veykril commented Mar 20, 2023

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

@HKalbasi HKalbasi force-pushed the mir branch 2 times, most recently from 2064ae1 to 2514bcf Compare March 20, 2023 22:34
source_map: BodySourceMap,
current_try_block: Option<LabelId>,
Copy link
Member

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)

Copy link
Member Author

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.

Comment on lines +115 to +125
/// 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())
}
Copy link
Member

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)

Copy link
Member Author

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);
Copy link
Member

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?

Copy link
Member

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)

Copy link
Member Author

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 {}
Copy link
Member

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)

Copy link
Member Author

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.

@HKalbasi
Copy link
Member Author

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+

@bors
Copy link
Contributor

bors commented Mar 21, 2023

📌 Commit 8a3ad7c has been approved by HKalbasi

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Mar 21, 2023

⌛ Testing commit 8a3ad7c with merge 3321799...

@bors
Copy link
Contributor

bors commented Mar 21, 2023

☀️ Test successful - checks-actions
Approved by: HKalbasi
Pushing 3321799 to master...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants