diff options
| author | bors <bors@rust-lang.org> | 2021-08-31 16:47:06 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2021-08-31 16:47:06 +0000 |
| commit | 0a84708edca7c275cb99ad080317fbc7637516d8 (patch) | |
| tree | e2cc9552e3d5ae365f6c373b60e6f6d87080c1f4 /compiler | |
| parent | 76d18cfb8945f824c8777e04981e930d2037954e (diff) | |
| parent | f5cf9678c23bea8a9a865f0ced879714ec107871 (diff) | |
| download | rust-0a84708edca7c275cb99ad080317fbc7637516d8.tar.gz rust-0a84708edca7c275cb99ad080317fbc7637516d8.zip | |
Auto merge of #88535 - m-ou-se:rollup-jeusxbo, r=m-ou-se
Rollup of 10 pull requests Successful merges: - #85017 (Add carrying_add, borrowing_sub, widening_mul, carrying_mul methods to integers) - #86362 (Avoid cloning LocalDecls) - #88391 (Fix json tuple struct enum variant ) - #88399 (Disallow the aapcs CC on Aarch64) - #88418 (Allow `~const` bounds on trait assoc functions) - #88445 (Clean up the lowering of AST items) - #88495 (Add `TcpStream::set_linger` and `TcpStream::linger`) - #88501 (Use right span in prelude collision suggestions with macros. ) - #88504 (Keep turbofish in prelude collision lint.) - #88524 (Remove unnecessary `mut` from udp doctests) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
Diffstat (limited to 'compiler')
| -rw-r--r-- | compiler/rustc_ast_lowering/src/item.rs | 62 | ||||
| -rw-r--r-- | compiler/rustc_ast_passes/src/ast_validation.rs | 4 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/mir/mod.rs | 5 | ||||
| -rw-r--r-- | compiler/rustc_mir/src/transform/inline.rs | 8 | ||||
| -rw-r--r-- | compiler/rustc_span/src/lib.rs | 8 | ||||
| -rw-r--r-- | compiler/rustc_target/src/spec/mod.rs | 3 | ||||
| -rw-r--r-- | compiler/rustc_typeck/src/check/method/prelude2021.rs | 54 | ||||
| -rw-r--r-- | compiler/rustc_typeck/src/check/upvar.rs | 8 |
8 files changed, 88 insertions, 64 deletions
diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index 014c240f11e..8daeef0cbd9 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -26,44 +26,43 @@ pub(super) struct ItemLowerer<'a, 'lowering, 'hir> { } impl ItemLowerer<'_, '_, '_> { - fn with_trait_impl_ref(&mut self, impl_ref: &Option<TraitRef>, f: impl FnOnce(&mut Self)) { + fn with_trait_impl_ref<T>( + &mut self, + impl_ref: &Option<TraitRef>, + f: impl FnOnce(&mut Self) -> T, + ) -> T { let old = self.lctx.is_in_trait_impl; self.lctx.is_in_trait_impl = impl_ref.is_some(); - f(self); + let ret = f(self); self.lctx.is_in_trait_impl = old; + ret } } impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> { fn visit_item(&mut self, item: &'a Item) { - let mut item_hir_id = None; - self.lctx.with_hir_id_owner(item.id, |lctx| { + let hir_id = self.lctx.with_hir_id_owner(item.id, |lctx| { lctx.without_in_scope_lifetime_defs(|lctx| { - if let Some(hir_item) = lctx.lower_item(item) { - let id = lctx.insert_item(hir_item); - item_hir_id = Some(id); - } + let hir_item = lctx.lower_item(item); + lctx.insert_item(hir_item) }) }); - if let Some(hir_id) = item_hir_id { - self.lctx.with_parent_item_lifetime_defs(hir_id, |this| { - let this = &mut ItemLowerer { lctx: this }; - match item.kind { - ItemKind::Mod(..) => { - let def_id = this.lctx.lower_node_id(item.id).expect_owner(); - let old_current_module = - mem::replace(&mut this.lctx.current_module, def_id); - visit::walk_item(this, item); - this.lctx.current_module = old_current_module; - } - ItemKind::Impl(box ImplKind { ref of_trait, .. }) => { - this.with_trait_impl_ref(of_trait, |this| visit::walk_item(this, item)); - } - _ => visit::walk_item(this, item), + self.lctx.with_parent_item_lifetime_defs(hir_id, |this| { + let this = &mut ItemLowerer { lctx: this }; + match item.kind { + ItemKind::Mod(..) => { + let def_id = this.lctx.lower_node_id(item.id).expect_owner(); + let old_current_module = mem::replace(&mut this.lctx.current_module, def_id); + visit::walk_item(this, item); + this.lctx.current_module = old_current_module; } - }); - } + ItemKind::Impl(box ImplKind { ref of_trait, .. }) => { + this.with_trait_impl_ref(of_trait, |this| visit::walk_item(this, item)); + } + _ => visit::walk_item(this, item), + } + }); } fn visit_fn(&mut self, fk: FnKind<'a>, sp: Span, _: NodeId) { @@ -113,7 +112,7 @@ impl<'hir> LoweringContext<'_, 'hir> { fn with_parent_item_lifetime_defs<T>( &mut self, parent_hir_id: hir::ItemId, - f: impl FnOnce(&mut LoweringContext<'_, '_>) -> T, + f: impl FnOnce(&mut Self) -> T, ) -> T { let old_len = self.in_scope_lifetimes.len(); @@ -137,10 +136,7 @@ impl<'hir> LoweringContext<'_, 'hir> { // Clears (and restores) the `in_scope_lifetimes` field. Used when // visiting nested items, which never inherit in-scope lifetimes // from their surrounding environment. - fn without_in_scope_lifetime_defs<T>( - &mut self, - f: impl FnOnce(&mut LoweringContext<'_, '_>) -> T, - ) -> T { + fn without_in_scope_lifetime_defs<T>(&mut self, f: impl FnOnce(&mut Self) -> T) -> T { let old_in_scope_lifetimes = mem::replace(&mut self.in_scope_lifetimes, vec![]); // this vector is only used when walking over impl headers, @@ -208,19 +204,19 @@ impl<'hir> LoweringContext<'_, 'hir> { } } - pub fn lower_item(&mut self, i: &Item) -> Option<hir::Item<'hir>> { + pub fn lower_item(&mut self, i: &Item) -> hir::Item<'hir> { let mut ident = i.ident; let mut vis = self.lower_visibility(&i.vis, None); let hir_id = self.lower_node_id(i.id); let attrs = self.lower_attrs(hir_id, &i.attrs); let kind = self.lower_item_kind(i.span, i.id, hir_id, &mut ident, attrs, &mut vis, &i.kind); - Some(hir::Item { + hir::Item { def_id: hir_id.expect_owner(), ident: self.lower_ident(ident), kind, vis, span: self.lower_span(i.span), - }) + } } fn lower_item_kind( diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index 73abd2bb83b..a243300edd9 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -1442,7 +1442,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { if !self.is_tilde_const_allowed { self.err_handler() .struct_span_err(bound.span(), "`~const` is not allowed here") - .note("only allowed on bounds on traits' associated types, const fns, const impls and its associated functions") + .note("only allowed on bounds on traits' associated types and functions, const fns, const impls and its associated functions") .emit(); } } @@ -1616,7 +1616,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { walk_list!(self, visit_ty, ty); } AssocItemKind::Fn(box FnKind(_, ref sig, ref generics, ref body)) - if self.in_const_trait_impl => + if self.in_const_trait_impl || ctxt == AssocCtxt::Trait => { self.visit_vis(&item.vis); self.visit_ident(item.ident); diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index 8e2917ee5b4..346a9e80217 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -426,6 +426,11 @@ impl<'tcx> Body<'tcx> { (arg_count + 1..local_count).map(Local::new) } + #[inline] + pub fn drain_vars_and_temps<'a>(&'a mut self) -> impl Iterator<Item = LocalDecl<'tcx>> + 'a { + self.local_decls.drain(self.arg_count + 1..) + } + /// Changes a statement to a nop. This is both faster than deleting instructions and avoids /// invalidating statement indices in `Location`s. pub fn make_statement_nop(&mut self, location: Location) { diff --git a/compiler/rustc_mir/src/transform/inline.rs b/compiler/rustc_mir/src/transform/inline.rs index c333667b3ad..8e9da31eba1 100644 --- a/compiler/rustc_mir/src/transform/inline.rs +++ b/compiler/rustc_mir/src/transform/inline.rs @@ -607,13 +607,7 @@ impl Inliner<'tcx> { } // Insert all of the (mapped) parts of the callee body into the caller. - caller_body.local_decls.extend( - // FIXME(eddyb) make `Range<Local>` iterable so that we can use - // `callee_body.local_decls.drain(callee_body.vars_and_temps())` - callee_body - .vars_and_temps_iter() - .map(|local| callee_body.local_decls[local].clone()), - ); + caller_body.local_decls.extend(callee_body.drain_vars_and_temps()); caller_body.source_scopes.extend(&mut callee_body.source_scopes.drain(..)); caller_body.var_debug_info.append(&mut callee_body.var_debug_info); caller_body.basic_blocks_mut().extend(callee_body.basic_blocks_mut().drain(..)); diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index 89e032b222f..10e9bde0d97 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -597,6 +597,14 @@ impl Span { if !expn_data.is_root() { Some(expn_data.call_site) } else { None } } + /// Walk down the expansion ancestors to find a span that's contained within `outer`. + pub fn find_ancestor_inside(mut self, outer: Span) -> Option<Span> { + while !outer.contains(self) { + self = self.parent()?; + } + Some(self) + } + /// Edition of the crate from which this span came. pub fn edition(self) -> edition::Edition { self.ctxt().edition() diff --git a/compiler/rustc_target/src/spec/mod.rs b/compiler/rustc_target/src/spec/mod.rs index 911956859b8..4d0cad80764 100644 --- a/compiler/rustc_target/src/spec/mod.rs +++ b/compiler/rustc_target/src/spec/mod.rs @@ -1501,7 +1501,8 @@ impl Target { | Cdecl | EfiApi => true, X86Interrupt => ["x86", "x86_64"].contains(&&self.arch[..]), - Aapcs | CCmseNonSecureCall => ["arm", "aarch64"].contains(&&self.arch[..]), + Aapcs => "arm" == self.arch, + CCmseNonSecureCall => ["arm", "aarch64"].contains(&&self.arch[..]), Win64 | SysV64 => self.arch == "x86_64", PtxKernel => self.arch == "nvptx64", Msp430Interrupt => self.arch == "msp430", diff --git a/compiler/rustc_typeck/src/check/method/prelude2021.rs b/compiler/rustc_typeck/src/check/method/prelude2021.rs index 1347f56258e..5c8056b2442 100644 --- a/compiler/rustc_typeck/src/check/method/prelude2021.rs +++ b/compiler/rustc_typeck/src/check/method/prelude2021.rs @@ -156,15 +156,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { segment.ident.name )); - let (self_adjusted, precise) = self.adjust_expr(pick, self_expr); + let (self_adjusted, precise) = self.adjust_expr(pick, self_expr, sp); if precise { let args = args .iter() .skip(1) .map(|arg| { + let span = arg.span.find_ancestor_inside(sp).unwrap_or_default(); format!( ", {}", - self.sess().source_map().span_to_snippet(arg.span).unwrap() + self.sess().source_map().span_to_snippet(span).unwrap() ) }) .collect::<String>(); @@ -173,8 +174,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { sp, "disambiguate the associated function", format!( - "{}::{}({}{})", - trait_name, segment.ident.name, self_adjusted, args + "{}::{}{}({}{})", + trait_name, + segment.ident.name, + if let Some(args) = segment.args.as_ref().and_then(|args| self + .sess() + .source_map() + .span_to_snippet(args.span_ext) + .ok()) + { + // Keep turbofish. + format!("::{}", args) + } else { + String::new() + }, + self_adjusted, + args, ), Applicability::MachineApplicable, ); @@ -272,11 +287,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { method_name.name )); - let mut self_ty_name = self - .sess() - .source_map() - .span_to_snippet(self_ty_span) - .unwrap_or_else(|_| self_ty.to_string()); + let mut self_ty_name = self_ty_span + .find_ancestor_inside(span) + .and_then(|span| self.sess().source_map().span_to_snippet(span).ok()) + .unwrap_or_else(|| self_ty.to_string()); // Get the number of generics the self type has (if an Adt) unless we can determine that // the user has written the self type with generics already which we (naively) do by looking @@ -370,7 +384,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { /// Creates a string version of the `expr` that includes explicit adjustments. /// Returns the string and also a bool indicating whther this is a *precise* /// suggestion. - fn adjust_expr(&self, pick: &Pick<'tcx>, expr: &hir::Expr<'tcx>) -> (String, bool) { + fn adjust_expr( + &self, + pick: &Pick<'tcx>, + expr: &hir::Expr<'tcx>, + outer: Span, + ) -> (String, bool) { let derefs = "*".repeat(pick.autoderefs); let autoref = match pick.autoref_or_ptr_adjustment { @@ -379,12 +398,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { Some(probe::AutorefOrPtrAdjustment::ToConstPtr) | None => "", }; - let (expr_text, precise) = - if let Ok(expr_text) = self.sess().source_map().span_to_snippet(expr.span) { - (expr_text, true) - } else { - ("(..)".to_string(), false) - }; + let (expr_text, precise) = if let Some(expr_text) = expr + .span + .find_ancestor_inside(outer) + .and_then(|span| self.sess().source_map().span_to_snippet(span).ok()) + { + (expr_text, true) + } else { + ("(..)".to_string(), false) + }; let adjusted_text = if let Some(probe::AutorefOrPtrAdjustment::ToConstPtr) = pick.autoref_or_ptr_adjustment diff --git a/compiler/rustc_typeck/src/check/upvar.rs b/compiler/rustc_typeck/src/check/upvar.rs index 75fd545060c..a25d0f80644 100644 --- a/compiler/rustc_typeck/src/check/upvar.rs +++ b/compiler/rustc_typeck/src/check/upvar.rs @@ -680,15 +680,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { migrated_variables_concat ); - let mut closure_body_span = self.tcx.hir().span(body_id.hir_id); - // If the body was entirely expanded from a macro // invocation, i.e. the body is not contained inside the // closure span, then we walk up the expansion until we // find the span before the expansion. - while !closure_body_span.is_dummy() && !closure_span.contains(closure_body_span) { - closure_body_span = closure_body_span.parent().unwrap_or(DUMMY_SP); - } + let closure_body_span = self.tcx.hir().span(body_id.hir_id) + .find_ancestor_inside(closure_span) + .unwrap_or(DUMMY_SP); if let Ok(s) = self.tcx.sess.source_map().span_to_snippet(closure_body_span) { let mut lines = s.lines(); |
