Skip to content

Commit 14dbf18

Browse files
authored
Merge branch 'rust-lang:master' into mo-usefull-order
2 parents 7ef37f3 + 19387d3 commit 14dbf18

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+2039
-828
lines changed

Cargo.lock

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

crates/hir-def/src/body/lower.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,7 +1611,11 @@ impl ExprCollector<'_> {
16111611
}
16121612
},
16131613
),
1614-
None => FormatArgs { template: Default::default(), arguments: args.finish() },
1614+
None => FormatArgs {
1615+
template: Default::default(),
1616+
arguments: args.finish(),
1617+
orphans: Default::default(),
1618+
},
16151619
};
16161620

16171621
// Create a list of all _unique_ (argument, format trait) combinations.
@@ -1750,7 +1754,13 @@ impl ExprCollector<'_> {
17501754
});
17511755
let unsafe_arg_new = self.alloc_expr_desugared(Expr::Unsafe {
17521756
id: None,
1753-
statements: Box::default(),
1757+
// We collect the unused expressions here so that we still infer them instead of
1758+
// dropping them out of the expression tree
1759+
statements: fmt
1760+
.orphans
1761+
.into_iter()
1762+
.map(|expr| Statement::Expr { expr, has_semi: true })
1763+
.collect(),
17541764
tail: Some(unsafe_arg_new),
17551765
});
17561766

crates/hir-def/src/hir/format_args.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::mem;
33

44
use hir_expand::name::Name;
55
use rustc_dependencies::parse_format as parse;
6+
use stdx::TupleExt;
67
use syntax::{
78
ast::{self, IsString},
89
SmolStr, TextRange, TextSize,
@@ -14,6 +15,7 @@ use crate::hir::ExprId;
1415
pub struct FormatArgs {
1516
pub template: Box<[FormatArgsPiece]>,
1617
pub arguments: FormatArguments,
18+
pub orphans: Vec<ExprId>,
1719
}
1820

1921
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -196,7 +198,11 @@ pub(crate) fn parse(
196198
let is_source_literal = parser.is_source_literal;
197199
if !parser.errors.is_empty() {
198200
// FIXME: Diagnose
199-
return FormatArgs { template: Default::default(), arguments: args.finish() };
201+
return FormatArgs {
202+
template: Default::default(),
203+
arguments: args.finish(),
204+
orphans: vec![],
205+
};
200206
}
201207

202208
let to_span = |inner_span: parse::InnerSpan| {
@@ -419,7 +425,11 @@ pub(crate) fn parse(
419425
// FIXME: Diagnose
420426
}
421427

422-
FormatArgs { template: template.into_boxed_slice(), arguments: args.finish() }
428+
FormatArgs {
429+
template: template.into_boxed_slice(),
430+
arguments: args.finish(),
431+
orphans: unused.into_iter().map(TupleExt::head).collect(),
432+
}
423433
}
424434

425435
#[derive(Debug, Clone, PartialEq, Eq)]

crates/hir-def/src/item_tree/tests.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,3 +370,15 @@ struct S<#[cfg(never)] T>;
370370
"#]],
371371
)
372372
}
373+
374+
#[test]
375+
fn pub_self() {
376+
check(
377+
r#"
378+
pub(self) struct S;
379+
"#,
380+
expect![[r#"
381+
pub(self) struct S;
382+
"#]],
383+
)
384+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,12 +468,12 @@ macro_rules! concat_bytes {}
468468
469469
fn main() { concat_bytes!(b'A', b"BC", [68, b'E', 70]); }
470470
"##,
471-
expect![[r##"
471+
expect![[r#"
472472
#[rustc_builtin_macro]
473473
macro_rules! concat_bytes {}
474474
475475
fn main() { [b'A', 66, 67, 68, b'E', 70]; }
476-
"##]],
476+
"#]],
477477
);
478478
}
479479

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,3 +1004,29 @@ fn main() {
10041004
"##]],
10051005
);
10061006
}
1007+
1008+
#[test]
1009+
fn eager_concat_bytes_panic() {
1010+
check(
1011+
r#"
1012+
#[rustc_builtin_macro]
1013+
#[macro_export]
1014+
macro_rules! concat_bytes {}
1015+
1016+
fn main() {
1017+
let x = concat_bytes!(2);
1018+
}
1019+
1020+
"#,
1021+
expect![[r#"
1022+
#[rustc_builtin_macro]
1023+
#[macro_export]
1024+
macro_rules! concat_bytes {}
1025+
1026+
fn main() {
1027+
let x = /* error: unexpected token in input */[];
1028+
}
1029+
1030+
"#]],
1031+
);
1032+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ impl DefMap {
9696
let types = result.take_types()?;
9797
match types {
9898
ModuleDefId::ModuleId(m) => Visibility::Module(m),
99+
// error: visibility needs to refer to module
99100
_ => {
100-
// error: visibility needs to refer to module
101101
return None;
102102
}
103103
}

crates/hir-def/src/resolver.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,14 @@ impl Resolver {
588588
_ => None,
589589
})
590590
}
591+
592+
pub fn impl_def(&self) -> Option<ImplId> {
593+
self.scopes().find_map(|scope| match scope {
594+
Scope::ImplDefScope(def) => Some(*def),
595+
_ => None,
596+
})
597+
}
598+
591599
/// `expr_id` is required to be an expression id that comes after the top level expression scope in the given resolver
592600
#[must_use]
593601
pub fn update_to_inner_scope(

crates/hir-def/src/visibility.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl RawVisibility {
7373
RawVisibility::Module(path)
7474
}
7575
ast::VisibilityKind::PubSelf => {
76-
let path = ModPath::from_kind(PathKind::Plain);
76+
let path = ModPath::from_kind(PathKind::Super(0));
7777
RawVisibility::Module(path)
7878
}
7979
ast::VisibilityKind::Pub => RawVisibility::Public,

crates/hir-expand/src/builtin_fn_macro.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use base_db::{
66
};
77
use cfg::CfgExpr;
88
use either::Either;
9+
use itertools::Itertools;
910
use mbe::{parse_exprs_with_sep, parse_to_token_tree};
1011
use syntax::{
1112
ast::{self, AstToken},
@@ -491,8 +492,25 @@ fn concat_bytes_expand(
491492
}
492493
}
493494
}
494-
let ident = tt::Ident { text: bytes.join(", ").into(), span };
495-
ExpandResult { value: quote!(span =>[#ident]), err }
495+
let value = tt::Subtree {
496+
delimiter: tt::Delimiter { open: span, close: span, kind: tt::DelimiterKind::Bracket },
497+
token_trees: {
498+
Itertools::intersperse_with(
499+
bytes.into_iter().map(|it| {
500+
tt::TokenTree::Leaf(tt::Leaf::Literal(tt::Literal { text: it.into(), span }))
501+
}),
502+
|| {
503+
tt::TokenTree::Leaf(tt::Leaf::Punct(tt::Punct {
504+
char: ',',
505+
spacing: tt::Spacing::Alone,
506+
span,
507+
}))
508+
},
509+
)
510+
.collect()
511+
},
512+
};
513+
ExpandResult { value, err }
496514
}
497515

498516
fn concat_bytes_expand_subtree(

crates/hir-expand/src/files.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,21 @@ impl InFile<TextRange> {
314314
}
315315
}
316316

317+
pub fn original_node_file_range_rooted(self, db: &dyn db::ExpandDatabase) -> FileRange {
318+
match self.file_id.repr() {
319+
HirFileIdRepr::FileId(file_id) => FileRange { file_id, range: self.value },
320+
HirFileIdRepr::MacroFile(mac_file) => {
321+
match ExpansionInfo::new(db, mac_file).map_node_range_up(db, self.value) {
322+
Some((it, SyntaxContextId::ROOT)) => it,
323+
_ => {
324+
let loc = db.lookup_intern_macro_call(mac_file.macro_call_id);
325+
loc.kind.original_call_range(db)
326+
}
327+
}
328+
}
329+
}
330+
}
331+
317332
pub fn original_node_file_range_opt(
318333
self,
319334
db: &dyn db::ExpandDatabase,

crates/hir-expand/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -605,8 +605,8 @@ pub struct ExpansionInfo {
605605
}
606606

607607
impl ExpansionInfo {
608-
pub fn expanded(&self) -> InFile<SyntaxNode> {
609-
self.expanded.clone().into()
608+
pub fn expanded(&self) -> InMacroFile<SyntaxNode> {
609+
self.expanded.clone()
610610
}
611611

612612
pub fn call_node(&self) -> Option<InFile<SyntaxNode>> {
@@ -617,13 +617,13 @@ impl ExpansionInfo {
617617
pub fn map_range_down<'a>(
618618
&'a self,
619619
span: SpanData,
620-
) -> Option<impl Iterator<Item = InMacroFile<SyntaxToken>> + 'a> {
620+
) -> Option<InMacroFile<impl Iterator<Item = SyntaxToken> + 'a>> {
621621
let tokens = self
622622
.exp_map
623623
.ranges_with_span(span)
624624
.flat_map(move |range| self.expanded.value.covering_element(range).into_token());
625625

626-
Some(tokens.map(move |token| InMacroFile::new(self.expanded.file_id, token)))
626+
Some(InMacroFile::new(self.expanded.file_id, tokens))
627627
}
628628

629629
/// Looks up the span at the given offset.

crates/hir-ty/src/consteval.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
use base_db::{salsa::Cycle, CrateId};
44
use chalk_ir::{cast::Cast, BoundVar, DebruijnIndex};
55
use hir_def::{
6-
hir::Expr,
6+
body::Body,
7+
hir::{Expr, ExprId},
78
path::Path,
89
resolver::{Resolver, ValueNs},
910
type_ref::LiteralConstRef,
@@ -136,7 +137,7 @@ pub fn intern_const_ref(
136137
ty: Ty,
137138
krate: CrateId,
138139
) -> Const {
139-
let layout = db.layout_of_ty(ty.clone(), Arc::new(TraitEnvironment::empty(krate)));
140+
let layout = db.layout_of_ty(ty.clone(), TraitEnvironment::empty(krate));
140141
let bytes = match value {
141142
LiteralConstRef::Int(i) => {
142143
// FIXME: We should handle failure of layout better.
@@ -280,21 +281,32 @@ pub(crate) fn const_eval_discriminant_variant(
280281
// get an `InferenceResult` instead of an `InferenceContext`. And we should remove `ctx.clone().resolve_all()` here
281282
// and make this function private. See the fixme comment on `InferenceContext::resolve_all`.
282283
pub(crate) fn eval_to_const(
283-
expr: Idx<Expr>,
284+
expr: ExprId,
284285
mode: ParamLoweringMode,
285286
ctx: &mut InferenceContext<'_>,
286287
args: impl FnOnce() -> Generics,
287288
debruijn: DebruijnIndex,
288289
) -> Const {
289290
let db = ctx.db;
290291
let infer = ctx.clone().resolve_all();
292+
fn has_closure(body: &Body, expr: ExprId) -> bool {
293+
if matches!(body[expr], Expr::Closure { .. }) {
294+
return true;
295+
}
296+
let mut r = false;
297+
body[expr].walk_child_exprs(|idx| r |= has_closure(body, idx));
298+
r
299+
}
300+
if has_closure(&ctx.body, expr) {
301+
// Type checking clousres need an isolated body (See the above FIXME). Bail out early to prevent panic.
302+
return unknown_const(infer[expr].clone());
303+
}
291304
if let Expr::Path(p) = &ctx.body.exprs[expr] {
292305
let resolver = &ctx.resolver;
293306
if let Some(c) = path_to_const(db, resolver, p, mode, args, debruijn, infer[expr].clone()) {
294307
return c;
295308
}
296309
}
297-
let infer = ctx.clone().resolve_all();
298310
if let Ok(mir_body) = lower_to_mir(ctx.db, ctx.owner, &ctx.body, &infer, expr) {
299311
if let Ok(result) = interpret_mir(db, Arc::new(mir_body), true, None).0 {
300312
return result;

crates/hir-ty/src/display.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,9 +448,8 @@ fn render_const_scalar(
448448
) -> Result<(), HirDisplayError> {
449449
// FIXME: We need to get krate from the final callers of the hir display
450450
// infrastructure and have it here as a field on `f`.
451-
let trait_env = Arc::new(TraitEnvironment::empty(
452-
*f.db.crate_graph().crates_in_topological_order().last().unwrap(),
453-
));
451+
let trait_env =
452+
TraitEnvironment::empty(*f.db.crate_graph().crates_in_topological_order().last().unwrap());
454453
match ty.kind(Interner) {
455454
TyKind::Scalar(s) => match s {
456455
Scalar::Bool => write!(f, "{}", if b[0] == 0 { false } else { true }),

0 commit comments

Comments
 (0)