Skip to content

Commit

Permalink
Auto merge of #16895 - Veykril:spans, r=Veykril
Browse files Browse the repository at this point in the history
Resolve whether `$pat` is `$pat_param` or not via 🌟hygiene🌟

Before we just picked the edition from the macro def which is wrong, since a macro call can produce the fragment kind from a different definition site.
  • Loading branch information
bors committed Mar 21, 2024
2 parents 8d74705 + 6d10719 commit 83f9cc6
Show file tree
Hide file tree
Showing 28 changed files with 440 additions and 375 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 2 additions & 71 deletions crates/base-db/src/input.rs
Expand Up @@ -6,11 +6,12 @@
//! actual IO. See `vfs` and `project_model` in the `rust-analyzer` crate for how
//! actual IO is done and lowered to input.

use std::{fmt, mem, ops, str::FromStr};
use std::{fmt, mem, ops};

use cfg::CfgOptions;
use la_arena::{Arena, Idx, RawIdx};
use rustc_hash::{FxHashMap, FxHashSet};
use span::Edition;
use syntax::SmolStr;
use triomphe::Arc;
use vfs::{file_set::FileSet, AbsPathBuf, AnchoredPath, FileId, VfsPath};
Expand Down Expand Up @@ -293,42 +294,11 @@ pub struct CrateData {
pub is_proc_macro: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Edition {
Edition2015,
Edition2018,
Edition2021,
Edition2024,
}

impl Edition {
pub const CURRENT: Edition = Edition::Edition2021;
pub const DEFAULT: Edition = Edition::Edition2015;
}

#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct Env {
entries: FxHashMap<String, String>,
}

impl Env {
pub fn new_for_test_fixture() -> Self {
Env {
entries: FxHashMap::from_iter([(
String::from("__ra_is_test_fixture"),
String::from("__ra_is_test_fixture"),
)]),
}
}
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum DependencyKind {
Normal,
Dev,
Build,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Dependency {
pub crate_id: CrateId,
Expand Down Expand Up @@ -670,32 +640,6 @@ impl CrateData {
}
}

impl FromStr for Edition {
type Err = ParseEditionError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let res = match s {
"2015" => Edition::Edition2015,
"2018" => Edition::Edition2018,
"2021" => Edition::Edition2021,
"2024" => Edition::Edition2024,
_ => return Err(ParseEditionError { invalid_input: s.to_owned() }),
};
Ok(res)
}
}

impl fmt::Display for Edition {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Edition::Edition2015 => "2015",
Edition::Edition2018 => "2018",
Edition::Edition2021 => "2021",
Edition::Edition2024 => "2024",
})
}
}

impl Extend<(String, String)> for Env {
fn extend<T: IntoIterator<Item = (String, String)>>(&mut self, iter: T) {
self.entries.extend(iter);
Expand All @@ -722,19 +666,6 @@ impl Env {
}
}

#[derive(Debug)]
pub struct ParseEditionError {
invalid_input: String,
}

impl fmt::Display for ParseEditionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "invalid edition: {:?}", self.invalid_input)
}
}

impl std::error::Error for ParseEditionError {}

#[derive(Debug)]
pub struct CyclicDependenciesError {
path: Vec<(CrateId, Option<CrateDisplayName>)>,
Expand Down
6 changes: 3 additions & 3 deletions crates/base-db/src/lib.rs
Expand Up @@ -14,9 +14,9 @@ use triomphe::Arc;
pub use crate::{
change::FileChange,
input::{
CrateData, CrateDisplayName, CrateGraph, CrateId, CrateName, CrateOrigin, Dependency,
DependencyKind, Edition, Env, LangCrateOrigin, ProcMacroPaths, ReleaseChannel, SourceRoot,
SourceRootId, TargetLayoutLoadResult,
CrateData, CrateDisplayName, CrateGraph, CrateId, CrateName, CrateOrigin, Dependency, Env,
LangCrateOrigin, ProcMacroPaths, ReleaseChannel, SourceRoot, SourceRootId,
TargetLayoutLoadResult,
},
};
pub use salsa::{self, Cancelled};
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-def/src/lib.rs
Expand Up @@ -73,7 +73,7 @@ use std::{
use base_db::{
impl_intern_key,
salsa::{self, impl_intern_value_trivial},
CrateId, Edition,
CrateId,
};
use hir_expand::{
builtin_attr_macro::BuiltinAttrExpander,
Expand All @@ -90,7 +90,7 @@ use hir_expand::{
use item_tree::ExternBlock;
use la_arena::Idx;
use nameres::DefMap;
use span::{AstIdNode, FileAstId, FileId, SyntaxContextId};
use span::{AstIdNode, Edition, FileAstId, FileId, SyntaxContextId};
use stdx::impl_from;
use syntax::{ast, AstNode};

Expand Down
85 changes: 85 additions & 0 deletions crates/hir-def/src/macro_expansion_tests/mbe.rs
Expand Up @@ -1449,6 +1449,7 @@ ok!();
#[test]
fn test_new_std_matches() {
check(
//- edition:2021
r#"
macro_rules! matches {
($expression:expr, $pattern:pat $(if $guard:expr)? $(,)?) => {
Expand Down Expand Up @@ -1480,6 +1481,90 @@ fn main() {
);
}

#[test]
fn test_hygienic_pat() {
check(
r#"
//- /new.rs crate:new deps:old edition:2015
old::make!();
fn main() {
matches!(0, 0 | 1 if true);
}
//- /old.rs crate:old edition:2021
#[macro_export]
macro_rules! make {
() => {
macro_rules! matches {
($expression:expr, $pattern:pat if $guard:expr ) => {
match $expression {
$pattern if $guard => true,
_ => false
}
};
}
}
}
"#,
expect![[r#"
macro_rules !matches {
($expression: expr, $pattern: pat if $guard: expr) = > {
match $expression {
$pattern if $guard = > true , _ = > false
}
}
;
}
fn main() {
match 0 {
0|1 if true =>true , _=>false
};
}
"#]],
);
check(
r#"
//- /new.rs crate:new deps:old edition:2021
old::make!();
fn main() {
matches/*+errors*/!(0, 0 | 1 if true);
}
//- /old.rs crate:old edition:2015
#[macro_export]
macro_rules! make {
() => {
macro_rules! matches {
($expression:expr, $pattern:pat if $guard:expr ) => {
match $expression {
$pattern if $guard => true,
_ => false
}
};
}
}
}
"#,
expect![[r#"
macro_rules !matches {
($expression: expr, $pattern: pat if $guard: expr) = > {
match $expression {
$pattern if $guard = > true , _ = > false
}
}
;
}
fn main() {
/* error: unexpected token in input *//* parse error: expected expression */
/* parse error: expected FAT_ARROW */
/* parse error: expected `,` */
/* parse error: expected pattern */
match 0 {
0 if $guard=>true , _=>false
};
}
"#]],
);
}

#[test]
fn test_dollar_crate_lhs_is_not_meta() {
check(
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-def/src/nameres.rs
Expand Up @@ -59,14 +59,14 @@ mod tests;

use std::ops::Deref;

use base_db::{CrateId, Edition, FileId};
use base_db::{CrateId, FileId};
use hir_expand::{
name::Name, proc_macro::ProcMacroKind, ErasedAstId, HirFileId, InFile, MacroCallId, MacroDefId,
};
use itertools::Itertools;
use la_arena::Arena;
use rustc_hash::{FxHashMap, FxHashSet};
use span::{FileAstId, ROOT_ERASED_FILE_AST_ID};
use span::{Edition, FileAstId, ROOT_ERASED_FILE_AST_ID};
use stdx::format_to;
use syntax::{ast, SmolStr};
use triomphe::Arc;
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-def/src/nameres/collector.rs
Expand Up @@ -5,7 +5,7 @@

use std::{cmp::Ordering, iter, mem, ops::Not};

use base_db::{CrateId, Dependency, Edition, FileId};
use base_db::{CrateId, Dependency, FileId};
use cfg::{CfgExpr, CfgOptions};
use either::Either;
use hir_expand::{
Expand All @@ -22,7 +22,7 @@ use itertools::{izip, Itertools};
use la_arena::Idx;
use limit::Limit;
use rustc_hash::{FxHashMap, FxHashSet};
use span::{ErasedFileAstId, FileAstId, Span, SyntaxContextId};
use span::{Edition, ErasedFileAstId, FileAstId, Span, SyntaxContextId};
use stdx::always;
use syntax::{ast, SmolStr};
use triomphe::Arc;
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-def/src/nameres/path_resolution.rs
Expand Up @@ -10,8 +10,8 @@
//!
//! `ReachedFixedPoint` signals about this.

use base_db::Edition;
use hir_expand::{name::Name, Lookup};
use span::Edition;
use triomphe::Arc;

use crate::{
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-expand/src/builtin_fn_macro.rs
@@ -1,11 +1,11 @@
//! Builtin macro

use base_db::{AnchoredPath, Edition, FileId};
use base_db::{AnchoredPath, FileId};
use cfg::CfgExpr;
use either::Either;
use itertools::Itertools;
use mbe::{parse_exprs_with_sep, parse_to_token_tree};
use span::{Span, SpanAnchor, SyntaxContextId, ROOT_ERASED_FILE_AST_ID};
use span::{Edition, Span, SpanAnchor, SyntaxContextId, ROOT_ERASED_FILE_AST_ID};
use syntax::ast::{self, AstToken};

use crate::{
Expand Down

0 comments on commit 83f9cc6

Please sign in to comment.