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

Move ModuleSource, create ModuleOrigin

Merged
merged 10 commits into from Dec 5, 2019
Merged

Move ModuleSource, create ModuleOrigin #2474

merged 10 commits into from Dec 5, 2019

Conversation

ice1000
Copy link
Contributor

@ice1000 ice1000 commented Dec 3, 2019

As title. This comes right after #2473

@matklad
Copy link
Member

matklad commented Dec 4, 2019

I am not sure that creating full-blown ModuleData for blocks is the right approach. I am not even sure that the fake module setup which rustc uses is a good idea. It would be convenient if all the code can assume that modules always come from real modules.

I think the right thing to share here is ModuleScope (which rightfully doesn't care about the origin of the module). I suggest to start with adding a ModuleScope to Body.

@ice1000
Copy link
Contributor Author

ice1000 commented Dec 4, 2019

@matklad but does it make sense to have ModuleOrigin and a moved ModuleSource anyway? I mean, without ::Blocks?

@matklad
Copy link
Member

matklad commented Dec 4, 2019

Yeah, replacing definition/declaration pair with an enum Origin makes sense. The current setup allows invalid None / None state, which is ungreat.

As for moving ModuleSource, i am not sure... I don't like the fact that it exists at all, and would prefer to find a design which doesn't use it at all. There's no guarantee that such design exists though :)

@ice1000 ice1000 changed the title Move ModuleSource and add ModuleSource::Block Move ModuleSource, create ModuleOrigin Dec 4, 2019
@ice1000
Copy link
Contributor Author

ice1000 commented Dec 4, 2019

@matklad Rebased master, removed ::Blocks. Please take a look. I can help on removing ModuleSource completely, but not in this PR.

@ice1000
Copy link
Contributor Author

ice1000 commented Dec 4, 2019

The current setup allows invalid None / None state, which is ungreat.

That's because the crate root file is late-initialized

@ice1000
Copy link
Contributor Author

ice1000 commented Dec 4, 2019

Rebased

crates/ra_hir_def/src/nameres.rs Outdated Show resolved Hide resolved
crates/ra_hir_def/src/nameres.rs Outdated Show resolved Hide resolved
crates/ra_hir_def/src/nameres.rs Outdated Show resolved Hide resolved
crates/ra_hir_def/src/nameres.rs Outdated Show resolved Hide resolved
@ice1000
Copy link
Contributor Author

ice1000 commented Dec 5, 2019

All comments addressed except the one under argument

@ice1000
Copy link
Contributor Author

ice1000 commented Dec 5, 2019

Not sure if I want to do this:

diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs
index a46bba4f2..18d87f6d7 100644
--- a/crates/ra_hir/src/from_source.rs
+++ b/crates/ra_hir/src/from_source.rs
@@ -235,11 +235,10 @@ impl Module {
                 let src_parent = InFile { file_id: src.file_id, value: parent_declaration };
                 Module::from_declaration(db, src_parent)
             }
-            _ => {
-                let src_parent = InFile {
-                    file_id: src.file_id,
-                    value: ModuleSource::from_file_id(db, src.file_id.original_file(db)),
-                };
+            None => {
+                let source_file = db.parse(src.file_id.original_file(db)).tree();
+                let src_parent =
+                    InFile { file_id: src.file_id, value: ModuleSource::SourceFile(source_file) };
                 Module::from_definition(db, src_parent)
             }
         }?;
diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs
index 01d67777d..0a2b32bbd 100644
--- a/crates/ra_hir_def/src/nameres.rs
+++ b/crates/ra_hir_def/src/nameres.rs
@@ -390,11 +390,6 @@ impl ModuleSource {
             ModuleSource::SourceFile(source_file)
         }
     }
-
-    pub fn from_file_id(db: &impl DefDatabase, file_id: FileId) -> ModuleSource {
-        let source_file = db.parse(file_id).tree();
-        ModuleSource::SourceFile(source_file)
-    }
 }
 
 mod diagnostics {
diff --git a/crates/ra_ide/src/parent_module.rs b/crates/ra_ide/src/parent_module.rs
index 616d69fce..aef3fa3df 100644
--- a/crates/ra_ide/src/parent_module.rs
+++ b/crates/ra_ide/src/parent_module.rs
@@ -1,6 +1,6 @@
 //! FIXME: write short doc here
 
-use ra_db::{CrateId, FileId, FilePosition};
+use ra_db::{CrateId, FileId, FilePosition, SourceDatabase};
 
 use crate::{db::RootDatabase, NavigationTarget};
 
@@ -21,7 +21,8 @@ pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec<Na
 
 /// Returns `Vec` for the same reason as `parent_module`
 pub(crate) fn crate_for(db: &RootDatabase, file_id: FileId) -> Vec<CrateId> {
-    let src = hir::ModuleSource::from_file_id(db, file_id);
+    let source_file = db.parse(file_id).tree();
+    let src = hir::ModuleSource::SourceFile(source_file);
     let module =
         match hir::Module::from_definition(db, hir::InFile { file_id: file_id.into(), value: src })
         {

Any idea? @matklad

@matklad
Copy link
Member

matklad commented Dec 5, 2019

Not sure if I want to do this:

One pub function less is good!

@ice1000
Copy link
Contributor Author

ice1000 commented Dec 5, 2019

All comments addressed

@matklad
Copy link
Member

matklad commented Dec 5, 2019

bors r+

Thanks @ice1000

bors bot added a commit that referenced this issue Dec 5, 2019
2474: Move `ModuleSource`, create `ModuleOrigin` r=matklad a=ice1000

As title. This comes right after #2473 

Co-authored-by: ice1000 <ice1000kotlin@foxmail.com>
@ice1000
Copy link
Contributor Author

ice1000 commented Dec 5, 2019

You're welcome!

I wish I can finish the function-local items someday 😢

@ice1000
Copy link
Contributor Author

ice1000 commented Dec 5, 2019

Oh no

##[error]  --> crates/ra_hir_ty/src/test_db.rs:77:32
   |
77 |                 if data.origin.file_id() == Some(file_id) {
   |                                ^^^^^^^

@bors
Copy link
Contributor

bors bot commented Dec 5, 2019

Build failed

  • Rust

@ice1000
Copy link
Contributor Author

ice1000 commented Dec 5, 2019

@matklad try again? 😉😉😉😉😉😉😉😉😉

@matklad
Copy link
Member

matklad commented Dec 5, 2019

bors merge

bors bot added a commit that referenced this issue Dec 5, 2019
2474: Move `ModuleSource`, create `ModuleOrigin` r=matklad a=ice1000

As title. This comes right after #2473 

Co-authored-by: ice1000 <ice1000kotlin@foxmail.com>
@bors
Copy link
Contributor

bors bot commented Dec 5, 2019

Build succeeded

  • Rust
  • TypeScript

@bors bors bot merged commit d15f300 into rust-lang:master Dec 5, 2019
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants