feat: Implement `feature(exhaustive_patterns)` from unstable Rust by iDawer · Pu...
source link: https://github.com/rust-lang/rust-analyzer/pull/13167
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Conversation
Contributor
iDawer commented 7 days ago
Closes #12753
Recognize Rust's unstable #![feature(exhaustive_patterns)]
(RFC 1872). Allow omitting visibly uninhabited variants from match
expressions when the feature is on.
This adjusts match checking to the current implementation of the postponed RFC 1872 in rustc.
I cannot find how r-a handles unstable features. Is it just silently accepts a wider superset of stable Rust? Quick performance test not showing visible degradation. Should not be fooled, it iterates root attributes on each Open question:
|
Member
lnicola commented 7 days ago
Yes. I don't think we've felt the need for more until now. |
pub(super) fn feature_exhaustive_patterns(&self) -> bool { |
||
// FIXME see MatchCheckCtx::is_uninhabited |
||
false |
||
*self.feature_exhaustive_patterns.get_or_init(|| { |
||
let def_map = self.db.crate_def_map(self.module.krate()); |
||
let root_mod = def_map.module_id(def_map.root()); |
||
let rood_attrs = self.db.attrs(root_mod.into()); |
||
let mut nightly_features = rood_attrs |
||
.by_key("feature") |
||
.attrs() |
||
.map(|attr| attr.parse_path_comma_token_tree()) |
||
.flatten() |
||
.flatten(); |
||
nightly_features.any( |
||
|feat| matches!(feat.segments(), [name] if name.to_smol_str() == "exhaustive_patterns"), |
||
) |
||
}) |
I think we should record the all enabled features in the DefMap
instead of parsing them here. We will need the enabled features for completions as well once we implement honoring them there (and probably other places can benefit from knowing features as well).
Then exposing the feature list from the def map seems fine as an API until more things make use of it at which point we can reconsider I'd say.
@@ -114,6 +114,8 @@ pub struct DefMap { | ||
registered_attrs: Vec<SmolStr>, |
||
/// Custom tool modules registered with `#![register_tool]`. |
||
registered_tools: Vec<SmolStr>, |
||
/// Unstable features of Rust enabled with `#![feature(A, B)]`. |
||
unstable_features: FxHashSet<SmolStr>, |
I think we can just go with a Vec
here, there aren't gonna be too many features so searching a Vec seems fine, though I suppose it doesn't really matter
Member
Veykril commented 6 days ago
Thanks |
Collaborator
bors commented 6 days ago
Collaborator
bors commented 6 days ago
Member
Veykril commented 6 days ago
This should also fix #4896 I assume? |
Collaborator
bors commented 6 days ago
Test successful - checks-actions |
Contributor
Author
iDawer commented 6 days ago
Yes, it should.
Thank you! |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
No one assigned
None yet
No milestone
Successfully merging this pull request may close these issues.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK