Skip to content

Commit

Permalink
Some more minor cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Dec 2, 2023
1 parent 5edf7bd commit e952e18
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 19 deletions.
1 change: 0 additions & 1 deletion crates/base-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

mod input;
mod change;
// FIXME: Is this purely a test util mod? Consider #[cfg(test)] gating it.
pub mod fixture;
pub mod span;

Expand Down
48 changes: 46 additions & 2 deletions crates/hir-def/src/body/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ mod block;

use base_db::{fixture::WithFixture, SourceDatabase};
use expect_test::{expect, Expect};
use hir_expand::db::ExpandDatabase;

use crate::{test_db::TestDB, ModuleDefId};

Expand Down Expand Up @@ -255,7 +254,6 @@ impl SsrError {
}
"##,
);
println!("{}", db.dump_syntax_contexts());

assert_eq!(db.body_with_source_map(def.into()).1.diagnostics(), &[]);
expect![[r#"
Expand Down Expand Up @@ -288,3 +286,49 @@ impl SsrError {
}"#]]
.assert_eq(&body.pretty_print(&db, def))
}

#[test]
fn regression_10300() {
let (db, body, def) = lower(
r#"
//- minicore: concat, panic
mod private {
pub use core::concat;
}
macro_rules! m {
() => {
panic!(concat!($crate::private::concat!("cc")));
};
}
fn f() {
m!();
}
"#,
);

let (_, source_map) = db.body_with_source_map(def.into());
assert_eq!(source_map.diagnostics(), &[]);

for (_, def_map) in body.blocks(&db) {
assert_eq!(def_map.diagnostics(), &[]);
}

expect![[r#"
fn f() {
$crate::panicking::panic_fmt(
builtin#lang(Arguments::new_v1_formatted)(
&[
"\"cc\"",
],
&[],
&[],
unsafe {
builtin#lang(UnsafeArg::new)()
},
),
);
}"#]]
.assert_eq(&body.pretty_print(&db, def))
}
5 changes: 2 additions & 3 deletions crates/hir-expand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,14 +628,13 @@ impl ExpansionInfo {
span: SpanData,
// FIXME: use this for range mapping, so that we can resolve inline format args
_relative_token_offset: Option<TextSize>,
// FIXME: ret ty should be wrapped in InMacroFile
) -> Option<impl Iterator<Item = InFile<SyntaxToken>> + 'a> {
) -> Option<impl Iterator<Item = InMacroFile<SyntaxToken>> + 'a> {
let tokens = self
.exp_map
.ranges_with_span(span)
.flat_map(move |range| self.expanded.value.covering_element(range).into_token());

Some(tokens.map(move |token| InFile::new(self.expanded.file_id.into(), token)))
Some(tokens.map(move |token| InMacroFile::new(self.expanded.file_id, token)))
}

/// Maps up the text range out of the expansion hierarchy back into the original file its from.
Expand Down
12 changes: 6 additions & 6 deletions crates/hir-ty/src/tests/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ fn infer_macros_expanded() {
}
"#,
expect![[r#"
!0..21 '{Foo(v...2),])}': Foo
!0..17 '{Foo(v...,2,])}': Foo
!1..4 'Foo': Foo({unknown}) -> Foo
!1..20 'Foo(ve...(2),])': Foo
!5..19 'vec![(1),(2),]': {unknown}
!1..16 'Foo(vec![1,2,])': Foo
!5..15 'vec![1,2,]': {unknown}
155..181 '{ ...,2); }': ()
165..166 'x': Foo
"#]],
Expand Down Expand Up @@ -96,10 +96,10 @@ fn infer_legacy_textual_scoped_macros_expanded() {
}
"#,
expect![[r#"
!0..21 '{Foo(v...2),])}': Foo
!0..17 '{Foo(v...,2,])}': Foo
!1..4 'Foo': Foo({unknown}) -> Foo
!1..20 'Foo(ve...(2),])': Foo
!5..19 'vec![(1),(2),]': {unknown}
!1..16 'Foo(vec![1,2,])': Foo
!5..15 'vec![1,2,]': {unknown}
194..250 '{ ...,2); }': ()
204..205 'x': Foo
227..228 'y': {unknown}
Expand Down
7 changes: 1 addition & 6 deletions crates/hir/src/semantics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,11 +557,6 @@ impl<'db> SemanticsImpl<'db> {
.span_at(token.text_range().start()),
};

// fetch span information of token in real file, then use that look through expansions of
// calls the token is in and afterwards recursively with the same span.
// what about things where spans change? Due to being joined etc, that is we don't find the
// exact span anymore?

let def_map = sa.resolver.def_map();
let mut stack: SmallVec<[_; 4]> = smallvec![InFile::new(sa.file_id, token)];

Expand All @@ -580,7 +575,7 @@ impl<'db> SemanticsImpl<'db> {
let len = stack.len();

// requeue the tokens we got from mapping our current token down
stack.extend(mapped_tokens);
stack.extend(mapped_tokens.map(Into::into));
// if the length changed we have found a mapping for the token
(stack.len() != len).then_some(())
};
Expand Down
9 changes: 8 additions & 1 deletion crates/test-utils/src/minicore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//! cell: copy, drop
//! clone: sized
//! coerce_unsized: unsize
//! concat:
//! copy: clone
//! default: sized
//! deref_mut: deref
Expand Down Expand Up @@ -1353,7 +1354,7 @@ mod panicking {
mod macros {
// region:panic
#[macro_export]
#[rustc_builtin_macro(std_panic)]
#[rustc_builtin_macro(core_panic)]
macro_rules! panic {
($($arg:tt)*) => {
/* compiler built-in */
Expand Down Expand Up @@ -1406,6 +1407,12 @@ mod macros {
($file:expr $(,)?) => {{ /* compiler built-in */ }};
}
// endregion:include

// region:concat
#[rustc_builtin_macro]
#[macro_export]
macro_rules! concat {}
// endregion:concat
}

// region:non_zero
Expand Down

0 comments on commit e952e18

Please sign in to comment.