Skip to content

Commit daa3352

Browse files
committed
Auto merge of #144269 - jieyouxu:rollup-137ysl2, r=jieyouxu
Rollup of 14 pull requests Successful merges: - rust-lang/rust#142097 (gpu offload host code generation) - rust-lang/rust#143430 (Lower extra lifetimes before normal generic params.) - rust-lang/rust#143768 (Constify Try, From, TryFrom and relevant traits) - rust-lang/rust#143816 (Implement `check` for compiletest and RA using tool macro) - rust-lang/rust#143985 (rustc_public: de-StableMIR-ize) - rust-lang/rust#144027 (clippy: make tests work in stage 1) - rust-lang/rust#144080 (Mitigate `#[align]` name resolution ambiguity regression with a rename) - rust-lang/rust#144176 (Add approval blocking labels for new bors) - rust-lang/rust#144187 (fix handling of base address for TypeId allocations) - rust-lang/rust#144212 (Remove the ptr_unique lang item) - rust-lang/rust#144243 (Subtree update of `rust-analyzer`) - rust-lang/rust#144246 (Don't use another main test file as auxiliary) - rust-lang/rust#144251 (rustc-dev-guide subtree update) - rust-lang/rust#144254 (opt-dist: make `artifact-dir` an absolute path for `opt-dist local`) r? `@ghost` `@rustbot` modify labels: rollup
2 parents f13fe5a + 05737ad commit daa3352

File tree

14 files changed

+278
-28
lines changed

14 files changed

+278
-28
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ vfs-notify = { path = "./crates/vfs-notify", version = "0.0.0" }
8989
vfs = { path = "./crates/vfs", version = "0.0.0" }
9090
edition = { path = "./crates/edition", version = "0.0.0" }
9191

92-
ra-ap-rustc_lexer = { version = "0.121", default-features = false }
92+
ra-ap-rustc_lexer = { version = "0.122", default-features = false }
9393
ra-ap-rustc_parse_format = { version = "0.121", default-features = false }
94-
ra-ap-rustc_index = { version = "0.121", default-features = false }
95-
ra-ap-rustc_abi = { version = "0.121", default-features = false }
96-
ra-ap-rustc_pattern_analysis = { version = "0.121", default-features = false }
94+
ra-ap-rustc_index = { version = "0.122", default-features = false }
95+
ra-ap-rustc_abi = { version = "0.122", default-features = false }
96+
ra-ap-rustc_pattern_analysis = { version = "0.122", default-features = false }
9797

9898
# local crates that aren't published to crates.io. These should not have versions.
9999

crates/cfg/src/cfg_expr.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ impl CfgExpr {
6868
next_cfg_expr(&mut tt.iter()).unwrap_or(CfgExpr::Invalid)
6969
}
7070

71+
#[cfg(feature = "tt")]
72+
pub fn parse_from_iter<S: Copy>(tt: &mut tt::iter::TtIter<'_, S>) -> CfgExpr {
73+
next_cfg_expr(tt).unwrap_or(CfgExpr::Invalid)
74+
}
75+
7176
/// Fold the cfg by querying all basic `Atom` and `KeyValue` predicates.
7277
pub fn fold(&self, query: &dyn Fn(&CfgAtom) -> bool) -> Option<bool> {
7378
match self {
@@ -96,7 +101,14 @@ fn next_cfg_expr<S: Copy>(it: &mut tt::iter::TtIter<'_, S>) -> Option<CfgExpr> {
96101
};
97102

98103
let ret = match it.peek() {
99-
Some(TtElement::Leaf(tt::Leaf::Punct(punct))) if punct.char == '=' => {
104+
Some(TtElement::Leaf(tt::Leaf::Punct(punct)))
105+
// Don't consume on e.g. `=>`.
106+
if punct.char == '='
107+
&& (punct.spacing == tt::Spacing::Alone
108+
|| it.remaining().flat_tokens().get(1).is_none_or(|peek2| {
109+
!matches!(peek2, tt::TokenTree::Leaf(tt::Leaf::Punct(_)))
110+
})) =>
111+
{
100112
match it.remaining().flat_tokens().get(1) {
101113
Some(tt::TokenTree::Leaf(tt::Leaf::Literal(literal))) => {
102114
it.next();

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,3 +550,51 @@ fn main() { "\"hello\""; }
550550
"##]],
551551
);
552552
}
553+
554+
#[test]
555+
fn cfg_select() {
556+
check(
557+
r#"
558+
#[rustc_builtin_macro]
559+
pub macro cfg_select($($tt:tt)*) {}
560+
561+
cfg_select! {
562+
false => { fn false_1() {} }
563+
any(false, true) => { fn true_1() {} }
564+
}
565+
566+
cfg_select! {
567+
false => { fn false_2() {} }
568+
_ => { fn true_2() {} }
569+
}
570+
571+
cfg_select! {
572+
false => { fn false_3() {} }
573+
}
574+
575+
cfg_select! {
576+
false
577+
}
578+
579+
cfg_select! {
580+
false =>
581+
}
582+
583+
"#,
584+
expect![[r#"
585+
#[rustc_builtin_macro]
586+
pub macro cfg_select($($tt:tt)*) {}
587+
588+
fn true_1() {}
589+
590+
fn true_2() {}
591+
592+
/* error: none of the predicates in this `cfg_select` evaluated to true */
593+
594+
/* error: expected `=>` after cfg expression */
595+
596+
/* error: expected a token tree after `=>` */
597+
598+
"#]],
599+
);
600+
}

crates/hir-expand/src/builtin/fn_macro.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ register_builtin! {
127127
(asm, Asm) => asm_expand,
128128
(global_asm, GlobalAsm) => global_asm_expand,
129129
(naked_asm, NakedAsm) => naked_asm_expand,
130+
(cfg_select, CfgSelect) => cfg_select_expand,
130131
(cfg, Cfg) => cfg_expand,
131132
(core_panic, CorePanic) => panic_expand,
132133
(std_panic, StdPanic) => panic_expand,
@@ -355,6 +356,71 @@ fn naked_asm_expand(
355356
ExpandResult::ok(expanded)
356357
}
357358

359+
fn cfg_select_expand(
360+
db: &dyn ExpandDatabase,
361+
id: MacroCallId,
362+
tt: &tt::TopSubtree,
363+
span: Span,
364+
) -> ExpandResult<tt::TopSubtree> {
365+
let loc = db.lookup_intern_macro_call(id);
366+
let cfg_options = loc.krate.cfg_options(db);
367+
368+
let mut iter = tt.iter();
369+
let mut expand_to = None;
370+
while let Some(next) = iter.peek() {
371+
let active = if let tt::TtElement::Leaf(tt::Leaf::Ident(ident)) = next
372+
&& ident.sym == sym::underscore
373+
{
374+
iter.next();
375+
true
376+
} else {
377+
cfg_options.check(&CfgExpr::parse_from_iter(&mut iter)) != Some(false)
378+
};
379+
match iter.expect_glued_punct() {
380+
Ok(it) if it.len() == 2 && it[0].char == '=' && it[1].char == '>' => {}
381+
_ => {
382+
let err_span = iter.peek().map(|it| it.first_span()).unwrap_or(span);
383+
return ExpandResult::new(
384+
tt::TopSubtree::empty(tt::DelimSpan::from_single(span)),
385+
ExpandError::other(err_span, "expected `=>` after cfg expression"),
386+
);
387+
}
388+
}
389+
let expand_to_if_active = match iter.next() {
390+
Some(tt::TtElement::Subtree(_, tt)) => tt.remaining(),
391+
_ => {
392+
let err_span = iter.peek().map(|it| it.first_span()).unwrap_or(span);
393+
return ExpandResult::new(
394+
tt::TopSubtree::empty(tt::DelimSpan::from_single(span)),
395+
ExpandError::other(err_span, "expected a token tree after `=>`"),
396+
);
397+
}
398+
};
399+
400+
if expand_to.is_none() && active {
401+
expand_to = Some(expand_to_if_active);
402+
}
403+
}
404+
match expand_to {
405+
Some(expand_to) => {
406+
let mut builder = tt::TopSubtreeBuilder::new(tt::Delimiter {
407+
kind: tt::DelimiterKind::Invisible,
408+
open: span,
409+
close: span,
410+
});
411+
builder.extend_with_tt(expand_to);
412+
ExpandResult::ok(builder.build())
413+
}
414+
None => ExpandResult::new(
415+
tt::TopSubtree::empty(tt::DelimSpan::from_single(span)),
416+
ExpandError::other(
417+
span,
418+
"none of the predicates in this `cfg_select` evaluated to true",
419+
),
420+
),
421+
}
422+
}
423+
358424
fn cfg_expand(
359425
db: &dyn ExpandDatabase,
360426
id: MacroCallId,

crates/ide-assists/src/handlers/generate_deref.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use syntax::{
1010
use crate::{
1111
AssistId,
1212
assist_context::{AssistContext, Assists, SourceChangeBuilder},
13-
utils::generate_trait_impl_text,
13+
utils::generate_trait_impl_text_intransitive,
1414
};
1515

1616
// Assist: generate_deref
@@ -150,7 +150,7 @@ fn generate_edit(
150150
),
151151
};
152152
let strukt_adt = ast::Adt::Struct(strukt);
153-
let deref_impl = generate_trait_impl_text(
153+
let deref_impl = generate_trait_impl_text_intransitive(
154154
&strukt_adt,
155155
&trait_path.display(db, edition).to_string(),
156156
&impl_code,
@@ -227,6 +227,28 @@ impl core::ops::Deref for B {
227227
);
228228
}
229229

230+
#[test]
231+
fn test_generate_record_deref_with_generic() {
232+
check_assist(
233+
generate_deref,
234+
r#"
235+
//- minicore: deref
236+
struct A<T>($0T);
237+
"#,
238+
r#"
239+
struct A<T>(T);
240+
241+
impl<T> core::ops::Deref for A<T> {
242+
type Target = T;
243+
244+
fn deref(&self) -> &Self::Target {
245+
&self.0
246+
}
247+
}
248+
"#,
249+
);
250+
}
251+
230252
#[test]
231253
fn test_generate_record_deref_short_path() {
232254
check_assist(

crates/ide-assists/src/handlers/generate_mut_trait_impl.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ fn get_trait_mut(apply_trait: &hir::Trait, famous: FamousDefs<'_, '_>) -> Option
134134
if trait_ == famous.core_borrow_Borrow().as_ref() {
135135
return Some("BorrowMut");
136136
}
137+
if trait_ == famous.core_ops_Deref().as_ref() {
138+
return Some("DerefMut");
139+
}
137140
None
138141
}
139142

@@ -142,6 +145,7 @@ fn process_method_name(name: ast::Name) -> Option<(ast::Name, &'static str)> {
142145
"index" => "index_mut",
143146
"as_ref" => "as_mut",
144147
"borrow" => "borrow_mut",
148+
"deref" => "deref_mut",
145149
_ => return None,
146150
};
147151
Some((name, new_name))
@@ -258,6 +262,39 @@ impl core::convert::AsRef<i32> for Foo {
258262
&self.0
259263
}
260264
}
265+
"#,
266+
);
267+
268+
check_assist(
269+
generate_mut_trait_impl,
270+
r#"
271+
//- minicore: deref
272+
struct Foo(i32);
273+
274+
impl core::ops::Deref$0 for Foo {
275+
type Target = i32;
276+
277+
fn deref(&self) -> &Self::Target {
278+
&self.0
279+
}
280+
}
281+
"#,
282+
r#"
283+
struct Foo(i32);
284+
285+
$0impl core::ops::DerefMut for Foo {
286+
fn deref_mut(&mut self) -> &mut Self::Target {
287+
&mut self.0
288+
}
289+
}
290+
291+
impl core::ops::Deref for Foo {
292+
type Target = i32;
293+
294+
fn deref(&self) -> &Self::Target {
295+
&self.0
296+
}
297+
}
261298
"#,
262299
);
263300
}

0 commit comments

Comments
 (0)