-
Notifications
You must be signed in to change notification settings - Fork 381
fix: Run macros within comptime contexts #5576
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
84e75e4
Remove scan pass
jfecher 87ef361
Start integrating elaborator & interpreter
jfecher 8635ee9
Set and recover elaborator state
jfecher 168bc86
Merge branch 'master' into jf/elaborate-in-interpreter
jfecher cf7dcab
Merge branch 'master' into jf/elaborate-in-interpreter
jfecher 4b85e73
Format test
jfecher 0fae018
Restrict visibility
jfecher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| use std::mem::replace; | ||
|
|
||
| use crate::{ | ||
| hir_def::expr::HirIdent, | ||
| macros_api::Expression, | ||
| node_interner::{DependencyId, ExprId, FuncId}, | ||
| }; | ||
|
|
||
| use super::{Elaborator, FunctionContext, ResolverMeta}; | ||
|
|
||
| impl<'context> Elaborator<'context> { | ||
| /// Elaborate an expression from the middle of a comptime scope. | ||
| /// When this happens we require additional information to know | ||
| /// what variables should be in scope. | ||
| pub fn elaborate_expression_from_comptime( | ||
| &mut self, | ||
| expr: Expression, | ||
| function: Option<FuncId>, | ||
| ) -> ExprId { | ||
| self.function_context.push(FunctionContext::default()); | ||
| let old_scope = self.scopes.end_function(); | ||
| self.scopes.start_function(); | ||
| let function_id = function.map(DependencyId::Function); | ||
| let old_item = replace(&mut self.current_item, function_id); | ||
|
|
||
| // Note: recover_generics isn't good enough here because any existing generics | ||
| // should not be in scope of this new function | ||
| let old_generics = std::mem::take(&mut self.generics); | ||
|
|
||
| let old_crate_and_module = function.map(|function| { | ||
| let meta = self.interner.function_meta(&function); | ||
| let old_crate = replace(&mut self.crate_id, meta.source_crate); | ||
| let old_module = replace(&mut self.local_module, meta.source_module); | ||
| self.introduce_generics_into_scope(meta.all_generics.clone()); | ||
| (old_crate, old_module) | ||
| }); | ||
|
|
||
| self.populate_scope_from_comptime_scopes(); | ||
| let expr = self.elaborate_expression(expr).0; | ||
|
|
||
| if let Some((old_crate, old_module)) = old_crate_and_module { | ||
| self.crate_id = old_crate; | ||
| self.local_module = old_module; | ||
| } | ||
|
|
||
| self.generics = old_generics; | ||
| self.current_item = old_item; | ||
| self.scopes.end_function(); | ||
| self.scopes.0.push(old_scope); | ||
| self.check_and_pop_function_context(); | ||
| expr | ||
| } | ||
|
|
||
| fn populate_scope_from_comptime_scopes(&mut self) { | ||
| // Take the comptime scope to be our runtime scope. | ||
| // Iterate from global scope to the most local scope so that the | ||
| // later definitions will naturally shadow the former. | ||
| for scope in &self.comptime_scopes { | ||
| for definition_id in scope.keys() { | ||
| let definition = self.interner.definition(*definition_id); | ||
| let name = definition.name.clone(); | ||
| let location = definition.location; | ||
|
|
||
| let scope = self.scopes.get_mut_scope(); | ||
| let ident = HirIdent::non_trait_method(*definition_id, location); | ||
| let meta = ResolverMeta { ident, num_times_used: 0, warn_if_unused: false }; | ||
| scope.add_key_value(name.clone(), meta); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.