Skip to content

Commit 83f9cc6

Browse files
committed
Auto merge of rust-lang#16895 - Veykril:spans, r=Veykril
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.
2 parents 8d74705 + 6d10719 commit 83f9cc6

File tree

28 files changed

+440
-375
lines changed

28 files changed

+440
-375
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/base-db/src/input.rs

Lines changed: 2 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
//! actual IO. See `vfs` and `project_model` in the `rust-analyzer` crate for how
77
//! actual IO is done and lowered to input.
88
9-
use std::{fmt, mem, ops, str::FromStr};
9+
use std::{fmt, mem, ops};
1010

1111
use cfg::CfgOptions;
1212
use la_arena::{Arena, Idx, RawIdx};
1313
use rustc_hash::{FxHashMap, FxHashSet};
14+
use span::Edition;
1415
use syntax::SmolStr;
1516
use triomphe::Arc;
1617
use vfs::{file_set::FileSet, AbsPathBuf, AnchoredPath, FileId, VfsPath};
@@ -293,42 +294,11 @@ pub struct CrateData {
293294
pub is_proc_macro: bool,
294295
}
295296

296-
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
297-
pub enum Edition {
298-
Edition2015,
299-
Edition2018,
300-
Edition2021,
301-
Edition2024,
302-
}
303-
304-
impl Edition {
305-
pub const CURRENT: Edition = Edition::Edition2021;
306-
pub const DEFAULT: Edition = Edition::Edition2015;
307-
}
308-
309297
#[derive(Default, Debug, Clone, PartialEq, Eq)]
310298
pub struct Env {
311299
entries: FxHashMap<String, String>,
312300
}
313301

314-
impl Env {
315-
pub fn new_for_test_fixture() -> Self {
316-
Env {
317-
entries: FxHashMap::from_iter([(
318-
String::from("__ra_is_test_fixture"),
319-
String::from("__ra_is_test_fixture"),
320-
)]),
321-
}
322-
}
323-
}
324-
325-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
326-
pub enum DependencyKind {
327-
Normal,
328-
Dev,
329-
Build,
330-
}
331-
332302
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
333303
pub struct Dependency {
334304
pub crate_id: CrateId,
@@ -670,32 +640,6 @@ impl CrateData {
670640
}
671641
}
672642

673-
impl FromStr for Edition {
674-
type Err = ParseEditionError;
675-
676-
fn from_str(s: &str) -> Result<Self, Self::Err> {
677-
let res = match s {
678-
"2015" => Edition::Edition2015,
679-
"2018" => Edition::Edition2018,
680-
"2021" => Edition::Edition2021,
681-
"2024" => Edition::Edition2024,
682-
_ => return Err(ParseEditionError { invalid_input: s.to_owned() }),
683-
};
684-
Ok(res)
685-
}
686-
}
687-
688-
impl fmt::Display for Edition {
689-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
690-
f.write_str(match self {
691-
Edition::Edition2015 => "2015",
692-
Edition::Edition2018 => "2018",
693-
Edition::Edition2021 => "2021",
694-
Edition::Edition2024 => "2024",
695-
})
696-
}
697-
}
698-
699643
impl Extend<(String, String)> for Env {
700644
fn extend<T: IntoIterator<Item = (String, String)>>(&mut self, iter: T) {
701645
self.entries.extend(iter);
@@ -722,19 +666,6 @@ impl Env {
722666
}
723667
}
724668

725-
#[derive(Debug)]
726-
pub struct ParseEditionError {
727-
invalid_input: String,
728-
}
729-
730-
impl fmt::Display for ParseEditionError {
731-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
732-
write!(f, "invalid edition: {:?}", self.invalid_input)
733-
}
734-
}
735-
736-
impl std::error::Error for ParseEditionError {}
737-
738669
#[derive(Debug)]
739670
pub struct CyclicDependenciesError {
740671
path: Vec<(CrateId, Option<CrateDisplayName>)>,

crates/base-db/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ use triomphe::Arc;
1414
pub use crate::{
1515
change::FileChange,
1616
input::{
17-
CrateData, CrateDisplayName, CrateGraph, CrateId, CrateName, CrateOrigin, Dependency,
18-
DependencyKind, Edition, Env, LangCrateOrigin, ProcMacroPaths, ReleaseChannel, SourceRoot,
19-
SourceRootId, TargetLayoutLoadResult,
17+
CrateData, CrateDisplayName, CrateGraph, CrateId, CrateName, CrateOrigin, Dependency, Env,
18+
LangCrateOrigin, ProcMacroPaths, ReleaseChannel, SourceRoot, SourceRootId,
19+
TargetLayoutLoadResult,
2020
},
2121
};
2222
pub use salsa::{self, Cancelled};

crates/hir-def/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ use std::{
7373
use base_db::{
7474
impl_intern_key,
7575
salsa::{self, impl_intern_value_trivial},
76-
CrateId, Edition,
76+
CrateId,
7777
};
7878
use hir_expand::{
7979
builtin_attr_macro::BuiltinAttrExpander,
@@ -90,7 +90,7 @@ use hir_expand::{
9090
use item_tree::ExternBlock;
9191
use la_arena::Idx;
9292
use nameres::DefMap;
93-
use span::{AstIdNode, FileAstId, FileId, SyntaxContextId};
93+
use span::{AstIdNode, Edition, FileAstId, FileId, SyntaxContextId};
9494
use stdx::impl_from;
9595
use syntax::{ast, AstNode};
9696

crates/hir-def/src/macro_expansion_tests/mbe.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,6 +1449,7 @@ ok!();
14491449
#[test]
14501450
fn test_new_std_matches() {
14511451
check(
1452+
//- edition:2021
14521453
r#"
14531454
macro_rules! matches {
14541455
($expression:expr, $pattern:pat $(if $guard:expr)? $(,)?) => {
@@ -1480,6 +1481,90 @@ fn main() {
14801481
);
14811482
}
14821483

1484+
#[test]
1485+
fn test_hygienic_pat() {
1486+
check(
1487+
r#"
1488+
//- /new.rs crate:new deps:old edition:2015
1489+
old::make!();
1490+
fn main() {
1491+
matches!(0, 0 | 1 if true);
1492+
}
1493+
//- /old.rs crate:old edition:2021
1494+
#[macro_export]
1495+
macro_rules! make {
1496+
() => {
1497+
macro_rules! matches {
1498+
($expression:expr, $pattern:pat if $guard:expr ) => {
1499+
match $expression {
1500+
$pattern if $guard => true,
1501+
_ => false
1502+
}
1503+
};
1504+
}
1505+
}
1506+
}
1507+
"#,
1508+
expect![[r#"
1509+
macro_rules !matches {
1510+
($expression: expr, $pattern: pat if $guard: expr) = > {
1511+
match $expression {
1512+
$pattern if $guard = > true , _ = > false
1513+
}
1514+
}
1515+
;
1516+
}
1517+
fn main() {
1518+
match 0 {
1519+
0|1 if true =>true , _=>false
1520+
};
1521+
}
1522+
"#]],
1523+
);
1524+
check(
1525+
r#"
1526+
//- /new.rs crate:new deps:old edition:2021
1527+
old::make!();
1528+
fn main() {
1529+
matches/*+errors*/!(0, 0 | 1 if true);
1530+
}
1531+
//- /old.rs crate:old edition:2015
1532+
#[macro_export]
1533+
macro_rules! make {
1534+
() => {
1535+
macro_rules! matches {
1536+
($expression:expr, $pattern:pat if $guard:expr ) => {
1537+
match $expression {
1538+
$pattern if $guard => true,
1539+
_ => false
1540+
}
1541+
};
1542+
}
1543+
}
1544+
}
1545+
"#,
1546+
expect![[r#"
1547+
macro_rules !matches {
1548+
($expression: expr, $pattern: pat if $guard: expr) = > {
1549+
match $expression {
1550+
$pattern if $guard = > true , _ = > false
1551+
}
1552+
}
1553+
;
1554+
}
1555+
fn main() {
1556+
/* error: unexpected token in input *//* parse error: expected expression */
1557+
/* parse error: expected FAT_ARROW */
1558+
/* parse error: expected `,` */
1559+
/* parse error: expected pattern */
1560+
match 0 {
1561+
0 if $guard=>true , _=>false
1562+
};
1563+
}
1564+
"#]],
1565+
);
1566+
}
1567+
14831568
#[test]
14841569
fn test_dollar_crate_lhs_is_not_meta() {
14851570
check(

crates/hir-def/src/nameres.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ mod tests;
5959

6060
use std::ops::Deref;
6161

62-
use base_db::{CrateId, Edition, FileId};
62+
use base_db::{CrateId, FileId};
6363
use hir_expand::{
6464
name::Name, proc_macro::ProcMacroKind, ErasedAstId, HirFileId, InFile, MacroCallId, MacroDefId,
6565
};
6666
use itertools::Itertools;
6767
use la_arena::Arena;
6868
use rustc_hash::{FxHashMap, FxHashSet};
69-
use span::{FileAstId, ROOT_ERASED_FILE_AST_ID};
69+
use span::{Edition, FileAstId, ROOT_ERASED_FILE_AST_ID};
7070
use stdx::format_to;
7171
use syntax::{ast, SmolStr};
7272
use triomphe::Arc;

crates/hir-def/src/nameres/collector.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
use std::{cmp::Ordering, iter, mem, ops::Not};
77

8-
use base_db::{CrateId, Dependency, Edition, FileId};
8+
use base_db::{CrateId, Dependency, FileId};
99
use cfg::{CfgExpr, CfgOptions};
1010
use either::Either;
1111
use hir_expand::{
@@ -22,7 +22,7 @@ use itertools::{izip, Itertools};
2222
use la_arena::Idx;
2323
use limit::Limit;
2424
use rustc_hash::{FxHashMap, FxHashSet};
25-
use span::{ErasedFileAstId, FileAstId, Span, SyntaxContextId};
25+
use span::{Edition, ErasedFileAstId, FileAstId, Span, SyntaxContextId};
2626
use stdx::always;
2727
use syntax::{ast, SmolStr};
2828
use triomphe::Arc;

crates/hir-def/src/nameres/path_resolution.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
//!
1111
//! `ReachedFixedPoint` signals about this.
1212
13-
use base_db::Edition;
1413
use hir_expand::{name::Name, Lookup};
14+
use span::Edition;
1515
use triomphe::Arc;
1616

1717
use crate::{

crates/hir-expand/src/builtin_fn_macro.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
//! Builtin macro
22
3-
use base_db::{AnchoredPath, Edition, FileId};
3+
use base_db::{AnchoredPath, FileId};
44
use cfg::CfgExpr;
55
use either::Either;
66
use itertools::Itertools;
77
use mbe::{parse_exprs_with_sep, parse_to_token_tree};
8-
use span::{Span, SpanAnchor, SyntaxContextId, ROOT_ERASED_FILE_AST_ID};
8+
use span::{Edition, Span, SpanAnchor, SyntaxContextId, ROOT_ERASED_FILE_AST_ID};
99
use syntax::ast::{self, AstToken};
1010

1111
use crate::{

0 commit comments

Comments
 (0)