-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
fix: Lower range pattern bounds to expressions #18995
Open
alibektas
wants to merge
10
commits into
rust-lang:master
Choose a base branch
from
alibektas:12210
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c1fe505
Make Pat::Range's start and end Option<ExprId>
alibektas 7a78144
Make higher levels adapt Bodys exprs having ExprOrPatId values
alibektas 06a37cc
Add a test to monitor whats going on
alibektas d064cd2
Lower ast::Ident to hir::Path when lowering RangePats
alibektas 75365ab
Finish up
alibektas 0fa0e1d
make SourceAnalyzer::pat_id return ExprOrPatId
alibektas 7d3e36a
resolve_bind_pat_to_const does not early return if expr
alibektas 30298e8
Remove dbg lines
alibektas 06dd82d
Remove fixme and add a missing test attribute
alibektas b44c218
Fix clippy errors
alibektas 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 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 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 |
---|---|---|
|
@@ -41,8 +41,8 @@ use crate::{ | |
FormatPlaceholder, FormatSign, FormatTrait, | ||
}, | ||
Array, Binding, BindingAnnotation, BindingId, BindingProblems, CaptureBy, ClosureKind, | ||
Expr, ExprId, Item, Label, LabelId, Literal, LiteralOrConst, MatchArm, Movability, | ||
OffsetOf, Pat, PatId, RecordFieldPat, RecordLitField, Statement, | ||
Expr, ExprId, Item, Label, LabelId, Literal, MatchArm, Movability, OffsetOf, Pat, PatId, | ||
RecordFieldPat, RecordLitField, Statement, | ||
}, | ||
item_scope::BuiltinShadowMode, | ||
lang_item::LangItem, | ||
|
@@ -1716,23 +1716,39 @@ impl ExprCollector<'_> { | |
self.collect_macro_call(call, macro_ptr, true, |this, expanded_pat| { | ||
this.collect_pat_opt(expanded_pat, binding_list) | ||
}); | ||
self.source_map.pat_map.insert(src, pat); | ||
self.source_map.pat_map.insert(src, pat.into()); | ||
return pat; | ||
} | ||
None => Pat::Missing, | ||
}, | ||
// FIXME: implement in a way that also builds source map and calculates assoc resolutions in type inference. | ||
ast::Pat::RangePat(p) => { | ||
let mut range_part_lower = |p: Option<ast::Pat>| { | ||
p.and_then(|it| match &it { | ||
ast::Pat::LiteralPat(it) => { | ||
Some(Box::new(LiteralOrConst::Literal(pat_literal_to_hir(it)?.0))) | ||
} | ||
pat @ (ast::Pat::IdentPat(_) | ast::Pat::PathPat(_)) => { | ||
let subpat = self.collect_pat(pat.clone(), binding_list); | ||
Some(Box::new(LiteralOrConst::Const(subpat))) | ||
let mut range_part_lower = |p: Option<ast::Pat>| -> Option<ExprId> { | ||
p.and_then(|it| { | ||
let ptr = PatPtr::new(&it); | ||
match &it { | ||
ast::Pat::LiteralPat(it) => Some(self.alloc_expr_from_pat( | ||
Expr::Literal(pat_literal_to_hir(it)?.0), | ||
ptr, | ||
)), | ||
ast::Pat::IdentPat(ident) => { | ||
if ident.is_simple_ident() { | ||
return ident | ||
.name() | ||
.map(|name| name.as_name()) | ||
.map(Path::from) | ||
.map(|path| { | ||
self.alloc_expr_from_pat(Expr::Path(path), ptr) | ||
}); | ||
} | ||
|
||
None | ||
} | ||
ast::Pat::PathPat(p) => p | ||
.path() | ||
.and_then(|path| self.parse_path(path)) | ||
.map(|parsed| self.alloc_expr_from_pat(Expr::Path(parsed), ptr)), | ||
_ => None, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's maybe add a comment here (or on top of the match) regarding the unhandled variants.
|
||
} | ||
_ => None, | ||
}) | ||
}; | ||
let start = range_part_lower(p.start()); | ||
|
@@ -1795,7 +1811,7 @@ impl ExprCollector<'_> { | |
} | ||
}); | ||
if let Some(pat) = pat.left() { | ||
self.source_map.pat_map.insert(src, pat); | ||
self.source_map.pat_map.insert(src, pat.into()); | ||
} | ||
pat | ||
} | ||
|
@@ -2412,7 +2428,7 @@ impl ExprCollector<'_> { | |
fn alloc_expr(&mut self, expr: Expr, ptr: ExprPtr) -> ExprId { | ||
let src = self.expander.in_file(ptr); | ||
let id = self.body.exprs.alloc(expr); | ||
self.source_map.expr_map_back.insert(id, src); | ||
self.source_map.expr_map_back.insert(id, src.map(AstPtr::wrap_left)); | ||
self.source_map.expr_map.insert(src, id.into()); | ||
id | ||
} | ||
|
@@ -2424,7 +2440,7 @@ impl ExprCollector<'_> { | |
fn alloc_expr_desugared_with_ptr(&mut self, expr: Expr, ptr: ExprPtr) -> ExprId { | ||
let src = self.expander.in_file(ptr); | ||
let id = self.body.exprs.alloc(expr); | ||
self.source_map.expr_map_back.insert(id, src); | ||
self.source_map.expr_map_back.insert(id, src.map(AstPtr::wrap_left)); | ||
// We intentionally don't fill this as it could overwrite a non-desugared entry | ||
// self.source_map.expr_map.insert(src, id); | ||
id | ||
|
@@ -2448,11 +2464,20 @@ impl ExprCollector<'_> { | |
self.source_map.pat_map_back.insert(id, src.map(AstPtr::wrap_left)); | ||
id | ||
} | ||
|
||
fn alloc_expr_from_pat(&mut self, expr: Expr, ptr: PatPtr) -> ExprId { | ||
let src = self.expander.in_file(ptr); | ||
let id = self.body.exprs.alloc(expr); | ||
self.source_map.pat_map.insert(src, id.into()); | ||
self.source_map.expr_map_back.insert(id, src.map(AstPtr::wrap_right)); | ||
id | ||
} | ||
|
||
fn alloc_pat(&mut self, pat: Pat, ptr: PatPtr) -> PatId { | ||
let src = self.expander.in_file(ptr); | ||
let id = self.body.pats.alloc(pat); | ||
self.source_map.pat_map_back.insert(id, src.map(AstPtr::wrap_right)); | ||
self.source_map.pat_map.insert(src, id); | ||
self.source_map.pat_map.insert(src, id.into()); | ||
id | ||
} | ||
// FIXME: desugared pats don't have ptr, that's wrong and should be fixed somehow. | ||
|
This file contains 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 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 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 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 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 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.
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.
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.
Then we could drop the
return