From 5b32ab671a03e0804e864907d7be4c775b4c6403 Mon Sep 17 00:00:00 2001 From: Camelid Date: Tue, 22 Dec 2020 19:37:23 -0800 Subject: [PATCH 01/23] Update and improve `rustc_codegen_{llvm,ssa}` docs These docs were very out of date and misleading. They even said that they codegen'd the *AST*! For some reason, the `rustc_codegen_ssa::base` docs were exactly identical to the `rustc_codegen_llvm::base` docs. They didn't really make sense, because they had LLVM-specific information even though `rustc_codegen_ssa` is supposed to be somewhat generic. So I removed them as they were misleading. --- compiler/rustc_codegen_llvm/src/base.rs | 18 ++++++++---------- compiler/rustc_codegen_llvm/src/common.rs | 1 + compiler/rustc_codegen_ssa/src/base.rs | 15 --------------- 3 files changed, 9 insertions(+), 25 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/base.rs b/compiler/rustc_codegen_llvm/src/base.rs index 7d01f6a54995a..d5be3132dee10 100644 --- a/compiler/rustc_codegen_llvm/src/base.rs +++ b/compiler/rustc_codegen_llvm/src/base.rs @@ -1,17 +1,15 @@ -//! Codegen the completed AST to the LLVM IR. -//! -//! Some functions here, such as codegen_block and codegen_expr, return a value -- -//! the result of the codegen to LLVM -- while others, such as codegen_fn -//! and mono_item, are called only for the side effect of adding a -//! particular definition to the LLVM IR output we're producing. +//! Codegen the MIR to the LLVM IR. //! //! Hopefully useful general knowledge about codegen: //! -//! * There's no way to find out the `Ty` type of a Value. Doing so +//! * There's no way to find out the [`Ty`] type of a [`Value`]. Doing so //! would be "trying to get the eggs out of an omelette" (credit: -//! pcwalton). You can, instead, find out its `llvm::Type` by calling `val_ty`, -//! but one `llvm::Type` corresponds to many `Ty`s; for instance, `tup(int, int, -//! int)` and `rec(x=int, y=int, z=int)` will have the same `llvm::Type`. +//! pcwalton). You can, instead, find out its [`llvm::Type`] by calling [`val_ty`], +//! but one [`llvm::Type`] corresponds to many [`Ty`]s; for instance, `tup(int, int, +//! int)` and `rec(x=int, y=int, z=int)` will have the same [`llvm::Type`]. +//! +//! [`Ty`]: rustc_middle::ty::Ty +//! [`val_ty`]: common::val_ty use super::ModuleLlvm; diff --git a/compiler/rustc_codegen_llvm/src/common.rs b/compiler/rustc_codegen_llvm/src/common.rs index 34e1b7a60451e..58af9d4cd04a9 100644 --- a/compiler/rustc_codegen_llvm/src/common.rs +++ b/compiler/rustc_codegen_llvm/src/common.rs @@ -314,6 +314,7 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> { } } +/// Get the [LLVM type][Type] of a [`Value`]. pub fn val_ty(v: &Value) -> &Type { unsafe { llvm::LLVMTypeOf(v) } } diff --git a/compiler/rustc_codegen_ssa/src/base.rs b/compiler/rustc_codegen_ssa/src/base.rs index 21138f967a273..a10e35e02f3c8 100644 --- a/compiler/rustc_codegen_ssa/src/base.rs +++ b/compiler/rustc_codegen_ssa/src/base.rs @@ -1,18 +1,3 @@ -//! Codegen the completed AST to the LLVM IR. -//! -//! Some functions here, such as `codegen_block` and `codegen_expr`, return a value -- -//! the result of the codegen to LLVM -- while others, such as `codegen_fn` -//! and `mono_item`, are called only for the side effect of adding a -//! particular definition to the LLVM IR output we're producing. -//! -//! Hopefully useful general knowledge about codegen: -//! -//! * There's no way to find out the `Ty` type of a `Value`. Doing so -//! would be "trying to get the eggs out of an omelette" (credit: -//! pcwalton). You can, instead, find out its `llvm::Type` by calling `val_ty`, -//! but one `llvm::Type` corresponds to many `Ty`s; for instance, `tup(int, int, -//! int)` and `rec(x=int, y=int, z=int)` will have the same `llvm::Type`. - use crate::back::write::{ compute_per_cgu_lto_type, start_async_codegen, submit_codegened_module_to_llvm, submit_post_lto_module_to_llvm, submit_pre_lto_module_to_llvm, ComputedLtoType, OngoingCodegen, From d3f4c48b49929b9d5de7d65c608a244a1b5f9e81 Mon Sep 17 00:00:00 2001 From: Camelid Date: Fri, 25 Dec 2020 11:48:12 -0800 Subject: [PATCH 02/23] rustdoc: Render visibilities succinctly --- src/librustdoc/clean/mod.rs | 4 +-- src/librustdoc/html/format.rs | 13 ++++++++-- src/librustdoc/html/render/mod.rs | 43 ++++++++++++++++++------------- 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index de53ce8d95c12..4fca6416ce293 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -2299,14 +2299,14 @@ impl Clean for (&hir::MacroDef<'_>, Option) { if matchers.len() <= 1 { format!( "{}macro {}{} {{\n ...\n}}", - vis.print_with_space(cx.tcx), + vis.print_with_space(cx.tcx, item.hir_id.owner), name, matchers.iter().map(|span| span.to_src(cx)).collect::(), ) } else { format!( "{}macro {} {{\n{}}}", - vis.print_with_space(cx.tcx), + vis.print_with_space(cx.tcx, item.hir_id.owner), name, matchers .iter() diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 7b0b219570b98..bd5f5a3c6cc88 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -12,7 +12,7 @@ use std::fmt; use rustc_data_structures::fx::FxHashSet; use rustc_hir as hir; use rustc_middle::ty::TyCtxt; -use rustc_span::def_id::{DefId, CRATE_DEF_INDEX}; +use rustc_span::def_id::{DefId, LocalDefId, CRATE_DEF_INDEX}; use rustc_target::spec::abi::Abi; use crate::clean::{self, PrimitiveType}; @@ -1085,12 +1085,21 @@ impl Function<'_> { } impl clean::Visibility { - crate fn print_with_space<'tcx>(self, tcx: TyCtxt<'tcx>) -> impl fmt::Display + 'tcx { + crate fn print_with_space<'tcx>( + self, + tcx: TyCtxt<'tcx>, + item_did: LocalDefId, + ) -> impl fmt::Display + 'tcx { use rustc_span::symbol::kw; display_fn(move |f| match self { clean::Public => f.write_str("pub "), clean::Inherited => Ok(()), + clean::Visibility::Restricted(did) + if did.index == tcx.parent_module_from_def_id(item_did).local_def_index => + { + Ok(()) + } clean::Visibility::Restricted(did) if did.index == CRATE_DEF_INDEX => { write!(f, "pub(crate) ") } diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 97e7c38ecb8c5..2d87b0c104f28 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -2157,14 +2157,14 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl Some(ref src) => write!( w, "{}extern crate {} as {};", - myitem.visibility.print_with_space(cx.tcx()), + myitem.visibility.print_with_space(cx.tcx(), myitem.def_id.expect_local()), anchor(myitem.def_id, &*src.as_str()), name ), None => write!( w, "{}extern crate {};", - myitem.visibility.print_with_space(cx.tcx()), + myitem.visibility.print_with_space(cx.tcx(), myitem.def_id.expect_local()), anchor(myitem.def_id, &*name.as_str()) ), } @@ -2175,7 +2175,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl write!( w, "{}{}", - myitem.visibility.print_with_space(cx.tcx()), + myitem.visibility.print_with_space(cx.tcx(), myitem.def_id.expect_local()), import.print() ); } @@ -2392,7 +2392,7 @@ fn item_constant(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, c: &clean:: write!( w, "{vis}const {name}: {typ}", - vis = it.visibility.print_with_space(cx.tcx()), + vis = it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()), name = it.name.as_ref().unwrap(), typ = c.type_.print(), ); @@ -2426,7 +2426,7 @@ fn item_static(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St write!( w, "{vis}static {mutability}{name}: {typ}", - vis = it.visibility.print_with_space(cx.tcx()), + vis = it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()), mutability = s.mutability.print_with_space(), name = it.name.as_ref().unwrap(), typ = s.type_.print() @@ -2437,7 +2437,7 @@ fn item_static(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St fn item_function(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, f: &clean::Function) { let header_len = format!( "{}{}{}{}{:#}fn {}{:#}", - it.visibility.print_with_space(cx.tcx()), + it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()), f.header.constness.print_with_space(), f.header.asyncness.print_with_space(), f.header.unsafety.print_with_space(), @@ -2452,7 +2452,7 @@ fn item_function(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, f: &clean:: w, "{vis}{constness}{asyncness}{unsafety}{abi}fn \ {name}{generics}{decl}{spotlight}{where_clause}", - vis = it.visibility.print_with_space(cx.tcx()), + vis = it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()), constness = f.header.constness.print_with_space(), asyncness = f.header.asyncness.print_with_space(), unsafety = f.header.unsafety.print_with_space(), @@ -2578,7 +2578,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra write!( w, "{}{}{}trait {}{}{}", - it.visibility.print_with_space(cx.tcx()), + it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()), t.unsafety.print_with_space(), if t.is_auto { "auto " } else { "" }, it.name.as_ref().unwrap(), @@ -2896,7 +2896,7 @@ fn assoc_const( w, "{}{}const {}: {}", extra, - it.visibility.print_with_space(cx.tcx()), + it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()), naive_assoc_href(it, link), it.name.as_ref().unwrap(), ty.print() @@ -3015,7 +3015,7 @@ fn render_assoc_item( }; let mut header_len = format!( "{}{}{}{}{}{:#}fn {}{:#}", - meth.visibility.print_with_space(cx.tcx()), + meth.visibility.print_with_space(cx.tcx(), meth.def_id.expect_local()), header.constness.print_with_space(), header.asyncness.print_with_space(), header.unsafety.print_with_space(), @@ -3037,7 +3037,7 @@ fn render_assoc_item( "{}{}{}{}{}{}{}fn {name}\ {generics}{decl}{spotlight}{where_clause}", if parent == ItemType::Trait { " " } else { "" }, - meth.visibility.print_with_space(cx.tcx()), + meth.visibility.print_with_space(cx.tcx(), meth.def_id.expect_local()), header.constness.print_with_space(), header.asyncness.print_with_space(), header.unsafety.print_with_space(), @@ -3189,7 +3189,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum write!( w, "{}enum {}{}{}", - it.visibility.print_with_space(cx.tcx()), + it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()), it.name.as_ref().unwrap(), e.generics.print(), WhereClause { gens: &e.generics, indent: 0, end_newline: true } @@ -3364,7 +3364,7 @@ fn render_struct( write!( w, "{}{}{}", - it.visibility.print_with_space(cx.tcx()), + it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()), if structhead { "struct " } else { "" }, it.name.as_ref().unwrap() ); @@ -3384,7 +3384,7 @@ fn render_struct( w, "\n{} {}{}: {},", tab, - field.visibility.print_with_space(cx.tcx()), + field.visibility.print_with_space(cx.tcx(), field.def_id.expect_local()), field.name.as_ref().unwrap(), ty.print() ); @@ -3413,7 +3413,14 @@ fn render_struct( match field.kind { clean::StrippedItem(box clean::StructFieldItem(..)) => write!(w, "_"), clean::StructFieldItem(ref ty) => { - write!(w, "{}{}", field.visibility.print_with_space(cx.tcx()), ty.print()) + write!( + w, + "{}{}", + field + .visibility + .print_with_space(cx.tcx(), field.def_id.expect_local()), + ty.print() + ) } _ => unreachable!(), } @@ -3446,7 +3453,7 @@ fn render_union( write!( w, "{}{}{}", - it.visibility.print_with_space(cx.tcx()), + it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()), if structhead { "union " } else { "" }, it.name.as_ref().unwrap() ); @@ -3461,7 +3468,7 @@ fn render_union( write!( w, " {}{}: {},\n{}", - field.visibility.print_with_space(cx.tcx()), + field.visibility.print_with_space(cx.tcx(), field.def_id.expect_local()), field.name.as_ref().unwrap(), ty.print(), tab @@ -4100,7 +4107,7 @@ fn item_foreign_type(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, cache: write!( w, " {}type {};\n}}", - it.visibility.print_with_space(cx.tcx()), + it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()), it.name.as_ref().unwrap(), ); From 50c1c27fa6adbb20ce61fce7caaea489e3984265 Mon Sep 17 00:00:00 2001 From: Camelid Date: Fri, 25 Dec 2020 15:38:46 -0800 Subject: [PATCH 03/23] Fix bugs; fix and add tests --- src/librustdoc/clean/mod.rs | 10 +++- src/librustdoc/clean/utils.rs | 24 +++++++- src/librustdoc/html/format.rs | 56 ++++++++++--------- src/librustdoc/html/render/mod.rs | 38 ++++++------- .../passes/collect_intra_doc_links.rs | 22 +------- src/test/rustdoc/decl_macro_priv.rs | 2 +- src/test/rustdoc/pub-restricted.rs | 32 +++++------ src/test/rustdoc/visibility.rs | 13 +++++ 8 files changed, 110 insertions(+), 87 deletions(-) create mode 100644 src/test/rustdoc/visibility.rs diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 4fca6416ce293..548cca0761e70 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -2299,14 +2299,20 @@ impl Clean for (&hir::MacroDef<'_>, Option) { if matchers.len() <= 1 { format!( "{}macro {}{} {{\n ...\n}}", - vis.print_with_space(cx.tcx, item.hir_id.owner), + vis.print_with_space( + cx.tcx, + cx.tcx.hir().local_def_id(item.hir_id).to_def_id() + ), name, matchers.iter().map(|span| span.to_src(cx)).collect::(), ) } else { format!( "{}macro {} {{\n{}}}", - vis.print_with_space(cx.tcx, item.hir_id.owner), + vis.print_with_space( + cx.tcx, + cx.tcx.hir().local_def_id(item.hir_id).to_def_id() + ), name, matchers .iter() diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs index 270321aaa1188..fe1b58164474a 100644 --- a/src/librustdoc/clean/utils.rs +++ b/src/librustdoc/clean/utils.rs @@ -15,7 +15,7 @@ use rustc_hir::def::{DefKind, Res}; use rustc_hir::def_id::{DefId, LOCAL_CRATE}; use rustc_middle::mir::interpret::ConstValue; use rustc_middle::ty::subst::{GenericArgKind, SubstsRef}; -use rustc_middle::ty::{self, DefIdTree, Ty}; +use rustc_middle::ty::{self, DefIdTree, Ty, TyCtxt}; use rustc_span::symbol::{kw, sym, Symbol}; use std::mem; @@ -624,3 +624,25 @@ where *cx.impl_trait_bounds.borrow_mut() = old_bounds; r } + +crate fn find_closest_parent_module(tcx: TyCtxt<'_>, def_id: DefId) -> Option { + let mut current = def_id; + // The immediate parent might not always be a module. + // Find the first parent which is. + loop { + if let Some(parent) = tcx.parent(current) { + if tcx.def_kind(parent) == DefKind::Mod { + break Some(parent); + } + current = parent; + } else { + debug!( + "{:?} has no parent (kind={:?}, original was {:?})", + current, + tcx.def_kind(current), + def_id + ); + break None; + } + } +} diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index bd5f5a3c6cc88..6a611ccf58ec0 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -12,10 +12,10 @@ use std::fmt; use rustc_data_structures::fx::FxHashSet; use rustc_hir as hir; use rustc_middle::ty::TyCtxt; -use rustc_span::def_id::{DefId, LocalDefId, CRATE_DEF_INDEX}; +use rustc_span::def_id::{DefId, CRATE_DEF_INDEX}; use rustc_target::spec::abi::Abi; -use crate::clean::{self, PrimitiveType}; +use crate::clean::{self, utils::find_closest_parent_module, PrimitiveType}; use crate::formats::cache::cache; use crate::formats::item_type::ItemType; use crate::html::escape::Escape; @@ -1088,38 +1088,40 @@ impl clean::Visibility { crate fn print_with_space<'tcx>( self, tcx: TyCtxt<'tcx>, - item_did: LocalDefId, + item_did: DefId, ) -> impl fmt::Display + 'tcx { use rustc_span::symbol::kw; display_fn(move |f| match self { clean::Public => f.write_str("pub "), clean::Inherited => Ok(()), - clean::Visibility::Restricted(did) - if did.index == tcx.parent_module_from_def_id(item_did).local_def_index => - { - Ok(()) - } - clean::Visibility::Restricted(did) if did.index == CRATE_DEF_INDEX => { - write!(f, "pub(crate) ") - } - clean::Visibility::Restricted(did) => { - f.write_str("pub(")?; - let path = tcx.def_path(did); - debug!("path={:?}", path); - let first_name = - path.data[0].data.get_opt_name().expect("modules are always named"); - if path.data.len() != 1 || (first_name != kw::SelfLower && first_name != kw::Super) - { - f.write_str("in ")?; - } - // modified from `resolved_path()` to work with `DefPathData` - let last_name = path.data.last().unwrap().data.get_opt_name().unwrap(); - for seg in &path.data[..path.data.len() - 1] { - write!(f, "{}::", seg.data.get_opt_name().unwrap())?; + + clean::Visibility::Restricted(vis_did) => { + if find_closest_parent_module(tcx, item_did) == Some(vis_did) { + // `pub(in foo)` where `foo` is the parent module + // is the same as no visibility modifier + Ok(()) + } else if vis_did.index == CRATE_DEF_INDEX { + write!(f, "pub(crate) ") + } else { + f.write_str("pub(")?; + let path = tcx.def_path(vis_did); + debug!("path={:?}", path); + let first_name = + path.data[0].data.get_opt_name().expect("modules are always named"); + if path.data.len() != 1 + || (first_name != kw::SelfLower && first_name != kw::Super) + { + f.write_str("in ")?; + } + // modified from `resolved_path()` to work with `DefPathData` + let last_name = path.data.last().unwrap().data.get_opt_name().unwrap(); + for seg in &path.data[..path.data.len() - 1] { + write!(f, "{}::", seg.data.get_opt_name().unwrap())?; + } + let path = anchor(vis_did, &last_name.as_str()).to_string(); + write!(f, "{}) ", path) } - let path = anchor(did, &last_name.as_str()).to_string(); - write!(f, "{}) ", path) } }) } diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 2d87b0c104f28..190a09d124008 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -2157,14 +2157,14 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl Some(ref src) => write!( w, "{}extern crate {} as {};", - myitem.visibility.print_with_space(cx.tcx(), myitem.def_id.expect_local()), + myitem.visibility.print_with_space(cx.tcx(), myitem.def_id), anchor(myitem.def_id, &*src.as_str()), name ), None => write!( w, "{}extern crate {};", - myitem.visibility.print_with_space(cx.tcx(), myitem.def_id.expect_local()), + myitem.visibility.print_with_space(cx.tcx(), myitem.def_id), anchor(myitem.def_id, &*name.as_str()) ), } @@ -2175,7 +2175,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl write!( w, "{}{}", - myitem.visibility.print_with_space(cx.tcx(), myitem.def_id.expect_local()), + myitem.visibility.print_with_space(cx.tcx(), myitem.def_id), import.print() ); } @@ -2392,7 +2392,7 @@ fn item_constant(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, c: &clean:: write!( w, "{vis}const {name}: {typ}", - vis = it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()), + vis = it.visibility.print_with_space(cx.tcx(), it.def_id), name = it.name.as_ref().unwrap(), typ = c.type_.print(), ); @@ -2426,7 +2426,7 @@ fn item_static(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St write!( w, "{vis}static {mutability}{name}: {typ}", - vis = it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()), + vis = it.visibility.print_with_space(cx.tcx(), it.def_id), mutability = s.mutability.print_with_space(), name = it.name.as_ref().unwrap(), typ = s.type_.print() @@ -2437,7 +2437,7 @@ fn item_static(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St fn item_function(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, f: &clean::Function) { let header_len = format!( "{}{}{}{}{:#}fn {}{:#}", - it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()), + it.visibility.print_with_space(cx.tcx(), it.def_id), f.header.constness.print_with_space(), f.header.asyncness.print_with_space(), f.header.unsafety.print_with_space(), @@ -2452,7 +2452,7 @@ fn item_function(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, f: &clean:: w, "{vis}{constness}{asyncness}{unsafety}{abi}fn \ {name}{generics}{decl}{spotlight}{where_clause}", - vis = it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()), + vis = it.visibility.print_with_space(cx.tcx(), it.def_id), constness = f.header.constness.print_with_space(), asyncness = f.header.asyncness.print_with_space(), unsafety = f.header.unsafety.print_with_space(), @@ -2578,7 +2578,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra write!( w, "{}{}{}trait {}{}{}", - it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()), + it.visibility.print_with_space(cx.tcx(), it.def_id), t.unsafety.print_with_space(), if t.is_auto { "auto " } else { "" }, it.name.as_ref().unwrap(), @@ -2896,7 +2896,7 @@ fn assoc_const( w, "{}{}const {}: {}", extra, - it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()), + it.visibility.print_with_space(cx.tcx(), it.def_id), naive_assoc_href(it, link), it.name.as_ref().unwrap(), ty.print() @@ -3015,7 +3015,7 @@ fn render_assoc_item( }; let mut header_len = format!( "{}{}{}{}{}{:#}fn {}{:#}", - meth.visibility.print_with_space(cx.tcx(), meth.def_id.expect_local()), + meth.visibility.print_with_space(cx.tcx(), meth.def_id), header.constness.print_with_space(), header.asyncness.print_with_space(), header.unsafety.print_with_space(), @@ -3037,7 +3037,7 @@ fn render_assoc_item( "{}{}{}{}{}{}{}fn {name}\ {generics}{decl}{spotlight}{where_clause}", if parent == ItemType::Trait { " " } else { "" }, - meth.visibility.print_with_space(cx.tcx(), meth.def_id.expect_local()), + meth.visibility.print_with_space(cx.tcx(), meth.def_id), header.constness.print_with_space(), header.asyncness.print_with_space(), header.unsafety.print_with_space(), @@ -3189,7 +3189,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum write!( w, "{}enum {}{}{}", - it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()), + it.visibility.print_with_space(cx.tcx(), it.def_id), it.name.as_ref().unwrap(), e.generics.print(), WhereClause { gens: &e.generics, indent: 0, end_newline: true } @@ -3364,7 +3364,7 @@ fn render_struct( write!( w, "{}{}{}", - it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()), + it.visibility.print_with_space(cx.tcx(), it.def_id), if structhead { "struct " } else { "" }, it.name.as_ref().unwrap() ); @@ -3384,7 +3384,7 @@ fn render_struct( w, "\n{} {}{}: {},", tab, - field.visibility.print_with_space(cx.tcx(), field.def_id.expect_local()), + field.visibility.print_with_space(cx.tcx(), field.def_id), field.name.as_ref().unwrap(), ty.print() ); @@ -3416,9 +3416,7 @@ fn render_struct( write!( w, "{}{}", - field - .visibility - .print_with_space(cx.tcx(), field.def_id.expect_local()), + field.visibility.print_with_space(cx.tcx(), field.def_id), ty.print() ) } @@ -3453,7 +3451,7 @@ fn render_union( write!( w, "{}{}{}", - it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()), + it.visibility.print_with_space(cx.tcx(), it.def_id), if structhead { "union " } else { "" }, it.name.as_ref().unwrap() ); @@ -3468,7 +3466,7 @@ fn render_union( write!( w, " {}{}: {},\n{}", - field.visibility.print_with_space(cx.tcx(), field.def_id.expect_local()), + field.visibility.print_with_space(cx.tcx(), field.def_id), field.name.as_ref().unwrap(), ty.print(), tab @@ -4107,7 +4105,7 @@ fn item_foreign_type(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, cache: write!( w, " {}type {};\n}}", - it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()), + it.visibility.print_with_space(cx.tcx(), it.def_id), it.name.as_ref().unwrap(), ); diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index a8adfe08b2561..f392f321fbfc3 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -31,7 +31,7 @@ use std::cell::Cell; use std::mem; use std::ops::Range; -use crate::clean::{self, Crate, Item, ItemLink, PrimitiveType}; +use crate::clean::{self, utils::find_closest_parent_module, Crate, Item, ItemLink, PrimitiveType}; use crate::core::DocContext; use crate::fold::DocFolder; use crate::html::markdown::markdown_links; @@ -774,25 +774,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> { } else if item.def_id.is_top_level_module() { Some(item.def_id) } else { - let mut current = item.def_id; - // The immediate parent might not always be a module. - // Find the first parent which is. - loop { - if let Some(parent) = self.cx.tcx.parent(current) { - if self.cx.tcx.def_kind(parent) == DefKind::Mod { - break Some(parent); - } - current = parent; - } else { - debug!( - "{:?} has no parent (kind={:?}, original was {:?})", - current, - self.cx.tcx.def_kind(current), - item.def_id - ); - break None; - } - } + find_closest_parent_module(self.cx.tcx, item.def_id) }; if parent_node.is_some() { diff --git a/src/test/rustdoc/decl_macro_priv.rs b/src/test/rustdoc/decl_macro_priv.rs index 4e1279e34d933..cb71bca9cf201 100644 --- a/src/test/rustdoc/decl_macro_priv.rs +++ b/src/test/rustdoc/decl_macro_priv.rs @@ -2,7 +2,7 @@ #![feature(decl_macro)] -// @has decl_macro_priv/macro.crate_macro.html //pre 'pub(crate) macro crate_macro() {' +// @has decl_macro_priv/macro.crate_macro.html //pre 'macro crate_macro() {' // @has - //pre '...' // @has - //pre '}' pub(crate) macro crate_macro() {} diff --git a/src/test/rustdoc/pub-restricted.rs b/src/test/rustdoc/pub-restricted.rs index 6720d848ac3b5..9ff3cad070d82 100644 --- a/src/test/rustdoc/pub-restricted.rs +++ b/src/test/rustdoc/pub-restricted.rs @@ -6,27 +6,27 @@ // @has 'foo/struct.FooPublic.html' '//pre' 'pub struct FooPublic' pub struct FooPublic; -// @has 'foo/struct.FooJustCrate.html' '//pre' 'pub(crate) struct FooJustCrate' +// @has 'foo/struct.FooJustCrate.html' '//pre' 'struct FooJustCrate' crate struct FooJustCrate; -// @has 'foo/struct.FooPubCrate.html' '//pre' 'pub(crate) struct FooPubCrate' +// @has 'foo/struct.FooPubCrate.html' '//pre' 'struct FooPubCrate' pub(crate) struct FooPubCrate; -// @has 'foo/struct.FooSelf.html' '//pre' 'pub(crate) struct FooSelf' +// @has 'foo/struct.FooSelf.html' '//pre' 'struct FooSelf' pub(self) struct FooSelf; -// @has 'foo/struct.FooInSelf.html' '//pre' 'pub(crate) struct FooInSelf' +// @has 'foo/struct.FooInSelf.html' '//pre' 'struct FooInSelf' pub(in self) struct FooInSelf; mod a { - // @has 'foo/a/struct.FooSuper.html' '//pre' 'pub(crate) struct FooSuper' - pub(super) struct FooSuper; - // @has 'foo/a/struct.FooInSuper.html' '//pre' 'pub(crate) struct FooInSuper' - pub(in super) struct FooInSuper; - // @has 'foo/a/struct.FooInA.html' '//pre' 'pub(in a) struct FooInA' - pub(in a) struct FooInA; + // @has 'foo/a/struct.FooASuper.html' '//pre' 'pub(crate) struct FooASuper' + pub(super) struct FooASuper; + // @has 'foo/a/struct.FooAInSuper.html' '//pre' 'pub(crate) struct FooAInSuper' + pub(in super) struct FooAInSuper; + // @has 'foo/a/struct.FooAInA.html' '//pre' 'struct FooAInA' + pub(in a) struct FooAInA; mod b { - // @has 'foo/a/b/struct.FooInSelfSuperB.html' '//pre' 'pub(in a::b) struct FooInSelfSuperB' - pub(in a::b) struct FooInSelfSuperB; - // @has 'foo/a/b/struct.FooInSuperSuper.html' '//pre' 'pub(crate) struct FooInSuperSuper' - pub(in super::super) struct FooInSuperSuper; - // @has 'foo/a/b/struct.FooInAB.html' '//pre' 'pub(in a::b) struct FooInAB' - pub(in a::b) struct FooInAB; + // @has 'foo/a/b/struct.FooBSuper.html' '//pre' 'pub(super) struct FooBSuper' + pub(super) struct FooBSuper; + // @has 'foo/a/b/struct.FooBInSuperSuper.html' '//pre' 'pub(crate) struct FooBInSuperSuper' + pub(in super::super) struct FooBInSuperSuper; + // @has 'foo/a/b/struct.FooBInAB.html' '//pre' 'struct FooBInAB' + pub(in a::b) struct FooBInAB; } } diff --git a/src/test/rustdoc/visibility.rs b/src/test/rustdoc/visibility.rs new file mode 100644 index 0000000000000..9dd0b68b1d95e --- /dev/null +++ b/src/test/rustdoc/visibility.rs @@ -0,0 +1,13 @@ +// compile-flags: --document-private-items + +#![crate_name = "foo"] + +// @has 'foo/fn.foo.html' '//pre' 'fn foo' +// !@has 'foo/fn.foo.html' '//pre' 'pub' +fn foo() {} + +mod bar { + // @has 'foo/bar/fn.baz.html' '//pre' 'fn baz' + // !@has 'foo/bar/fn.baz.html' '//pre' 'pub' + fn baz() {} +} From 00652e429aaa2250f2561a457e46387bb5b16737 Mon Sep 17 00:00:00 2001 From: Camelid Date: Fri, 25 Dec 2020 15:54:04 -0800 Subject: [PATCH 04/23] Handle `pub(super)` --- src/librustdoc/html/format.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 6a611ccf58ec0..a8eae52fc565e 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -1097,12 +1097,20 @@ impl clean::Visibility { clean::Inherited => Ok(()), clean::Visibility::Restricted(vis_did) => { - if find_closest_parent_module(tcx, item_did) == Some(vis_did) { + let parent_module = find_closest_parent_module(tcx, item_did); + + if parent_module == Some(vis_did) { // `pub(in foo)` where `foo` is the parent module // is the same as no visibility modifier Ok(()) } else if vis_did.index == CRATE_DEF_INDEX { write!(f, "pub(crate) ") + } else if parent_module + .map(|parent| find_closest_parent_module(tcx, parent)) + .flatten() + == Some(vis_did) + { + write!(f, "pub(super) ") } else { f.write_str("pub(")?; let path = tcx.def_path(vis_did); From b959c75b3135189a90a1d3f630d702266270763f Mon Sep 17 00:00:00 2001 From: Camelid Date: Fri, 25 Dec 2020 16:16:40 -0800 Subject: [PATCH 05/23] Prefer `pub(crate)` over no modifier --- src/librustdoc/html/format.rs | 6 +++--- src/test/rustdoc/decl_macro_priv.rs | 2 +- src/test/rustdoc/pub-restricted.rs | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index a8eae52fc565e..90f4aaefc9bc1 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -1099,12 +1099,12 @@ impl clean::Visibility { clean::Visibility::Restricted(vis_did) => { let parent_module = find_closest_parent_module(tcx, item_did); - if parent_module == Some(vis_did) { + if vis_did.index == CRATE_DEF_INDEX { + write!(f, "pub(crate) ") + } else if parent_module == Some(vis_did) { // `pub(in foo)` where `foo` is the parent module // is the same as no visibility modifier Ok(()) - } else if vis_did.index == CRATE_DEF_INDEX { - write!(f, "pub(crate) ") } else if parent_module .map(|parent| find_closest_parent_module(tcx, parent)) .flatten() diff --git a/src/test/rustdoc/decl_macro_priv.rs b/src/test/rustdoc/decl_macro_priv.rs index cb71bca9cf201..4e1279e34d933 100644 --- a/src/test/rustdoc/decl_macro_priv.rs +++ b/src/test/rustdoc/decl_macro_priv.rs @@ -2,7 +2,7 @@ #![feature(decl_macro)] -// @has decl_macro_priv/macro.crate_macro.html //pre 'macro crate_macro() {' +// @has decl_macro_priv/macro.crate_macro.html //pre 'pub(crate) macro crate_macro() {' // @has - //pre '...' // @has - //pre '}' pub(crate) macro crate_macro() {} diff --git a/src/test/rustdoc/pub-restricted.rs b/src/test/rustdoc/pub-restricted.rs index 9ff3cad070d82..f828e642abdca 100644 --- a/src/test/rustdoc/pub-restricted.rs +++ b/src/test/rustdoc/pub-restricted.rs @@ -6,13 +6,13 @@ // @has 'foo/struct.FooPublic.html' '//pre' 'pub struct FooPublic' pub struct FooPublic; -// @has 'foo/struct.FooJustCrate.html' '//pre' 'struct FooJustCrate' +// @has 'foo/struct.FooJustCrate.html' '//pre' 'pub(crate) struct FooJustCrate' crate struct FooJustCrate; -// @has 'foo/struct.FooPubCrate.html' '//pre' 'struct FooPubCrate' +// @has 'foo/struct.FooPubCrate.html' '//pre' 'pub(crate) struct FooPubCrate' pub(crate) struct FooPubCrate; -// @has 'foo/struct.FooSelf.html' '//pre' 'struct FooSelf' +// @has 'foo/struct.FooSelf.html' '//pre' 'pub(crate) struct FooSelf' pub(self) struct FooSelf; -// @has 'foo/struct.FooInSelf.html' '//pre' 'struct FooInSelf' +// @has 'foo/struct.FooInSelf.html' '//pre' 'pub(crate) struct FooInSelf' pub(in self) struct FooInSelf; mod a { // @has 'foo/a/struct.FooASuper.html' '//pre' 'pub(crate) struct FooASuper' From bb4761d1ebd47ec966a4774c087c645dc3a0227d Mon Sep 17 00:00:00 2001 From: Camelid Date: Fri, 25 Dec 2020 16:27:56 -0800 Subject: [PATCH 06/23] Merge `pub-restricted` and `visibility` test --- src/test/rustdoc/pub-restricted.rs | 32 ----------------------- src/test/rustdoc/visibility.rs | 41 +++++++++++++++++++++++++----- 2 files changed, 34 insertions(+), 39 deletions(-) delete mode 100644 src/test/rustdoc/pub-restricted.rs diff --git a/src/test/rustdoc/pub-restricted.rs b/src/test/rustdoc/pub-restricted.rs deleted file mode 100644 index f828e642abdca..0000000000000 --- a/src/test/rustdoc/pub-restricted.rs +++ /dev/null @@ -1,32 +0,0 @@ -// compile-flags: --document-private-items - -#![feature(crate_visibility_modifier)] - -#![crate_name = "foo"] - -// @has 'foo/struct.FooPublic.html' '//pre' 'pub struct FooPublic' -pub struct FooPublic; -// @has 'foo/struct.FooJustCrate.html' '//pre' 'pub(crate) struct FooJustCrate' -crate struct FooJustCrate; -// @has 'foo/struct.FooPubCrate.html' '//pre' 'pub(crate) struct FooPubCrate' -pub(crate) struct FooPubCrate; -// @has 'foo/struct.FooSelf.html' '//pre' 'pub(crate) struct FooSelf' -pub(self) struct FooSelf; -// @has 'foo/struct.FooInSelf.html' '//pre' 'pub(crate) struct FooInSelf' -pub(in self) struct FooInSelf; -mod a { - // @has 'foo/a/struct.FooASuper.html' '//pre' 'pub(crate) struct FooASuper' - pub(super) struct FooASuper; - // @has 'foo/a/struct.FooAInSuper.html' '//pre' 'pub(crate) struct FooAInSuper' - pub(in super) struct FooAInSuper; - // @has 'foo/a/struct.FooAInA.html' '//pre' 'struct FooAInA' - pub(in a) struct FooAInA; - mod b { - // @has 'foo/a/b/struct.FooBSuper.html' '//pre' 'pub(super) struct FooBSuper' - pub(super) struct FooBSuper; - // @has 'foo/a/b/struct.FooBInSuperSuper.html' '//pre' 'pub(crate) struct FooBInSuperSuper' - pub(in super::super) struct FooBInSuperSuper; - // @has 'foo/a/b/struct.FooBInAB.html' '//pre' 'struct FooBInAB' - pub(in a::b) struct FooBInAB; - } -} diff --git a/src/test/rustdoc/visibility.rs b/src/test/rustdoc/visibility.rs index 9dd0b68b1d95e..ebb314a7941b6 100644 --- a/src/test/rustdoc/visibility.rs +++ b/src/test/rustdoc/visibility.rs @@ -1,13 +1,40 @@ // compile-flags: --document-private-items +#![feature(crate_visibility_modifier)] + #![crate_name = "foo"] -// @has 'foo/fn.foo.html' '//pre' 'fn foo' -// !@has 'foo/fn.foo.html' '//pre' 'pub' -fn foo() {} +// @has 'foo/struct.FooPublic.html' '//pre' 'pub struct FooPublic' +pub struct FooPublic; +// @has 'foo/struct.FooJustCrate.html' '//pre' 'pub(crate) struct FooJustCrate' +crate struct FooJustCrate; +// @has 'foo/struct.FooPubCrate.html' '//pre' 'pub(crate) struct FooPubCrate' +pub(crate) struct FooPubCrate; +// @has 'foo/struct.FooSelf.html' '//pre' 'pub(crate) struct FooSelf' +pub(self) struct FooSelf; +// @has 'foo/struct.FooInSelf.html' '//pre' 'pub(crate) struct FooInSelf' +pub(in self) struct FooInSelf; +// @has 'foo/struct.FooPriv.html' '//pre' 'pub(crate) struct FooPriv' +struct FooPriv; + +mod a { + // @has 'foo/a/struct.FooASuper.html' '//pre' 'pub(crate) struct FooASuper' + pub(super) struct FooASuper; + // @has 'foo/a/struct.FooAInSuper.html' '//pre' 'pub(crate) struct FooAInSuper' + pub(in super) struct FooAInSuper; + // @has 'foo/a/struct.FooAInA.html' '//pre' 'struct FooAInA' + pub(in a) struct FooAInA; + // @has 'foo/a/struct.FooAPriv.html' '//pre' 'struct FooAPriv' + struct FooAPriv; -mod bar { - // @has 'foo/bar/fn.baz.html' '//pre' 'fn baz' - // !@has 'foo/bar/fn.baz.html' '//pre' 'pub' - fn baz() {} + mod b { + // @has 'foo/a/b/struct.FooBSuper.html' '//pre' 'pub(super) struct FooBSuper' + pub(super) struct FooBSuper; + // @has 'foo/a/b/struct.FooBInSuperSuper.html' '//pre' 'pub(crate) struct FooBInSuperSuper' + pub(in super::super) struct FooBInSuperSuper; + // @has 'foo/a/b/struct.FooBInAB.html' '//pre' 'struct FooBInAB' + pub(in a::b) struct FooBInAB; + // @has 'foo/a/b/struct.FooBPriv.html' '//pre' 'struct FooBPriv' + struct FooBPriv; + } } From 4b1b277cf90886466e2e0dc5017ea535ef5e00ce Mon Sep 17 00:00:00 2001 From: Camelid Date: Fri, 25 Dec 2020 16:33:15 -0800 Subject: [PATCH 07/23] Add missing code to `find_closest_parent_module` --- src/librustdoc/clean/utils.rs | 40 +++++++++++-------- .../passes/collect_intra_doc_links.rs | 10 +---- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs index fe1b58164474a..4b9541b7e1421 100644 --- a/src/librustdoc/clean/utils.rs +++ b/src/librustdoc/clean/utils.rs @@ -626,23 +626,31 @@ where } crate fn find_closest_parent_module(tcx: TyCtxt<'_>, def_id: DefId) -> Option { - let mut current = def_id; - // The immediate parent might not always be a module. - // Find the first parent which is. - loop { - if let Some(parent) = tcx.parent(current) { - if tcx.def_kind(parent) == DefKind::Mod { - break Some(parent); + if item.is_fake() { + // FIXME: is this correct? + None + // If we're documenting the crate root itself, it has no parent. Use the root instead. + } else if item.def_id.is_top_level_module() { + Some(item.def_id) + } else { + let mut current = def_id; + // The immediate parent might not always be a module. + // Find the first parent which is. + loop { + if let Some(parent) = tcx.parent(current) { + if tcx.def_kind(parent) == DefKind::Mod { + break Some(parent); + } + current = parent; + } else { + debug!( + "{:?} has no parent (kind={:?}, original was {:?})", + current, + tcx.def_kind(current), + def_id + ); + break None; } - current = parent; - } else { - debug!( - "{:?} has no parent (kind={:?}, original was {:?})", - current, - tcx.def_kind(current), - def_id - ); - break None; } } } diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index f392f321fbfc3..4e261c3fd1934 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -767,15 +767,7 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> { fn fold_item(&mut self, mut item: Item) -> Option { use rustc_middle::ty::DefIdTree; - let parent_node = if item.is_fake() { - // FIXME: is this correct? - None - // If we're documenting the crate root itself, it has no parent. Use the root instead. - } else if item.def_id.is_top_level_module() { - Some(item.def_id) - } else { - find_closest_parent_module(self.cx.tcx, item.def_id) - }; + let parent_node = find_closest_parent_module(self.cx.tcx, item.def_id); if parent_node.is_some() { trace!("got parent node for {:?} {:?}, id {:?}", item.type_(), item.name, item.def_id); From eaf851288ed31090afd924eeb22f5e0f202151d4 Mon Sep 17 00:00:00 2001 From: Camelid Date: Fri, 25 Dec 2020 16:35:28 -0800 Subject: [PATCH 08/23] Simplify loop and remove old debugging code Co-authored-by: Joshua Nelson --- src/librustdoc/clean/utils.rs | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs index 4b9541b7e1421..b49ed07f8e83f 100644 --- a/src/librustdoc/clean/utils.rs +++ b/src/librustdoc/clean/utils.rs @@ -636,21 +636,12 @@ crate fn find_closest_parent_module(tcx: TyCtxt<'_>, def_id: DefId) -> Option Date: Fri, 25 Dec 2020 16:37:09 -0800 Subject: [PATCH 09/23] Extract local variable --- src/librustdoc/clean/mod.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 548cca0761e70..666d418cc0e2f 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -2295,24 +2295,20 @@ impl Clean for (&hir::MacroDef<'_>, Option) { ) } else { let vis = item.vis.clean(cx); + let vis_printed_with_space = + vis.print_with_space(cx.tcx, cx.tcx.hir().local_def_id(item.hir_id).to_def_id()); if matchers.len() <= 1 { format!( "{}macro {}{} {{\n ...\n}}", - vis.print_with_space( - cx.tcx, - cx.tcx.hir().local_def_id(item.hir_id).to_def_id() - ), + vis_printed_with_space, name, matchers.iter().map(|span| span.to_src(cx)).collect::(), ) } else { format!( "{}macro {} {{\n{}}}", - vis.print_with_space( - cx.tcx, - cx.tcx.hir().local_def_id(item.hir_id).to_def_id() - ), + vis_printed_with_space, name, matchers .iter() From 83322c067ec752a4e737fff3c88129ee4a7c9fe1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Wed, 30 Dec 2020 00:00:00 +0000 Subject: [PATCH 10/23] remove unnecessary trailing semicolon from bootstrap --- src/bootstrap/config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bootstrap/config.rs b/src/bootstrap/config.rs index def8f21543626..a8b1082edebd0 100644 --- a/src/bootstrap/config.rs +++ b/src/bootstrap/config.rs @@ -334,7 +334,7 @@ impl Merge for TomlConfig { *x = Some(new); } } - }; + } do_merge(&mut self.build, build); do_merge(&mut self.install, install); do_merge(&mut self.llvm, llvm); From 26daa656624deb54a8f3c754893635ebe46eed10 Mon Sep 17 00:00:00 2001 From: Tyler Mandry Date: Wed, 30 Dec 2020 19:14:15 +0000 Subject: [PATCH 11/23] Update LLVM - [GlobalISel][IRTranslator] Fix a crash when the use of an extractvalue is a non-dominated metadata use. - [asan] Use dynamic shadow memory position on Apple Silicon macOS --- src/llvm-project | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/llvm-project b/src/llvm-project index 8d78ad13896b9..fb115ee43b776 160000 --- a/src/llvm-project +++ b/src/llvm-project @@ -1 +1 @@ -Subproject commit 8d78ad13896b955f630714f386a95ed91b237e3d +Subproject commit fb115ee43b77601b237717c21ab0a8f5b5b9d50a From 947b279bc6cc32d298b16f22b76bfd272b73b6bb Mon Sep 17 00:00:00 2001 From: max-heller Date: Wed, 30 Dec 2020 11:43:30 -0500 Subject: [PATCH 12/23] Take type defaults into account in suggestions to reorder generic parameters --- .../rustc_ast_passes/src/ast_validation.rs | 56 ++++++++++--------- .../defaults/complex-unord-param.min.stderr | 2 +- .../defaults/intermixed-lifetime.full.stderr | 4 +- .../defaults/intermixed-lifetime.min.stderr | 8 +-- .../defaults/needs-feature.min.stderr | 2 +- .../defaults/needs-feature.none.stderr | 2 +- .../defaults/simple-defaults.min.stderr | 2 +- ...ue-80512-param-reordering-with-defaults.rs | 4 ++ ...0512-param-reordering-with-defaults.stderr | 8 +++ 9 files changed, 53 insertions(+), 35 deletions(-) create mode 100644 src/test/ui/issues/issue-80512-param-reordering-with-defaults.rs create mode 100644 src/test/ui/issues/issue-80512-param-reordering-with-defaults.stderr diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index 686300c7c5fec..0fcffda309bb0 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -717,35 +717,46 @@ impl<'a> AstValidator<'a> { /// Checks that generic parameters are in the correct order, /// which is lifetimes, then types and then consts. (`<'a, T, const N: usize>`) -fn validate_generic_param_order<'a>( +fn validate_generic_param_order( sess: &Session, handler: &rustc_errors::Handler, - generics: impl Iterator, Span, Option)>, + generics: &[GenericParam], span: Span, ) { let mut max_param: Option = None; let mut out_of_order = FxHashMap::default(); let mut param_idents = vec![]; - for (kind, bounds, span, ident) in generics { + for param in generics { + let ident = Some(param.ident.to_string()); + let (kind, bounds, span) = (¶m.kind, Some(&*param.bounds), param.ident.span); + let (ord_kind, ident) = match ¶m.kind { + GenericParamKind::Lifetime => (ParamKindOrd::Lifetime, ident), + GenericParamKind::Type { default: _ } => (ParamKindOrd::Type, ident), + GenericParamKind::Const { ref ty, kw_span: _ } => { + let ty = pprust::ty_to_string(ty); + let unordered = sess.features_untracked().const_generics; + (ParamKindOrd::Const { unordered }, Some(format!("const {}: {}", param.ident, ty))) + } + }; if let Some(ident) = ident { - param_idents.push((kind, bounds, param_idents.len(), ident)); + param_idents.push((kind, ord_kind, bounds, param_idents.len(), ident)); } let max_param = &mut max_param; match max_param { - Some(max_param) if *max_param > kind => { - let entry = out_of_order.entry(kind).or_insert((*max_param, vec![])); + Some(max_param) if *max_param > ord_kind => { + let entry = out_of_order.entry(ord_kind).or_insert((*max_param, vec![])); entry.1.push(span); } - Some(_) | None => *max_param = Some(kind), + Some(_) | None => *max_param = Some(ord_kind), }; } let mut ordered_params = "<".to_string(); if !out_of_order.is_empty() { - param_idents.sort_by_key(|&(po, _, i, _)| (po, i)); + param_idents.sort_by_key(|&(_, po, _, i, _)| (po, i)); let mut first = true; - for (_, bounds, _, ident) in param_idents { + for (kind, _, bounds, _, ident) in param_idents { if !first { ordered_params += ", "; } @@ -756,6 +767,16 @@ fn validate_generic_param_order<'a>( ordered_params += &pprust::bounds_to_string(&bounds); } } + match kind { + GenericParamKind::Type { default: Some(default) } => { + ordered_params += " = "; + ordered_params += &pprust::ty_to_string(default); + } + GenericParamKind::Type { default: None } => (), + GenericParamKind::Lifetime => (), + // FIXME(const_generics:defaults) + GenericParamKind::Const { ty: _, kw_span: _ } => (), + } first = false; } } @@ -1150,22 +1171,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { validate_generic_param_order( self.session, self.err_handler(), - generics.params.iter().map(|param| { - let ident = Some(param.ident.to_string()); - let (kind, ident) = match ¶m.kind { - GenericParamKind::Lifetime => (ParamKindOrd::Lifetime, ident), - GenericParamKind::Type { default: _ } => (ParamKindOrd::Type, ident), - GenericParamKind::Const { ref ty, kw_span: _ } => { - let ty = pprust::ty_to_string(ty); - let unordered = self.session.features_untracked().const_generics; - ( - ParamKindOrd::Const { unordered }, - Some(format!("const {}: {}", param.ident, ty)), - ) - } - }; - (kind, Some(&*param.bounds), param.ident.span, ident) - }), + &generics.params, generics.span, ); diff --git a/src/test/ui/const-generics/defaults/complex-unord-param.min.stderr b/src/test/ui/const-generics/defaults/complex-unord-param.min.stderr index cec56d7038a48..8e8d26a00043d 100644 --- a/src/test/ui/const-generics/defaults/complex-unord-param.min.stderr +++ b/src/test/ui/const-generics/defaults/complex-unord-param.min.stderr @@ -2,7 +2,7 @@ error: type parameters must be declared prior to const parameters --> $DIR/complex-unord-param.rs:8:41 | LL | struct NestedArrays<'a, const N: usize, A: 'a, const M: usize, T:'a =u32> { - | ---------------------^----------------------^--------- help: reorder the parameters: lifetimes, then types, then consts: `<'a, A: 'a, T: 'a, const N: usize, const M: usize>` + | ---------------------^----------------------^--------- help: reorder the parameters: lifetimes, then types, then consts: `<'a, A: 'a, T: 'a = u32, const N: usize, const M: usize>` error: aborting due to previous error diff --git a/src/test/ui/const-generics/defaults/intermixed-lifetime.full.stderr b/src/test/ui/const-generics/defaults/intermixed-lifetime.full.stderr index 98352addaef15..c4a666a829d8c 100644 --- a/src/test/ui/const-generics/defaults/intermixed-lifetime.full.stderr +++ b/src/test/ui/const-generics/defaults/intermixed-lifetime.full.stderr @@ -2,13 +2,13 @@ error: lifetime parameters must be declared prior to const parameters --> $DIR/intermixed-lifetime.rs:6:28 | LL | struct Foo(&'a (), T); - | -----------------^^---------- help: reorder the parameters: lifetimes, then consts and types: `<'a, const N: usize, T>` + | -----------------^^---------- help: reorder the parameters: lifetimes, then consts and types: `<'a, const N: usize, T = u32>` error: lifetime parameters must be declared prior to type parameters --> $DIR/intermixed-lifetime.rs:10:37 | LL | struct Bar(&'a (), T); - | --------------------------^^- help: reorder the parameters: lifetimes, then consts and types: `<'a, const N: usize, T>` + | --------------------------^^- help: reorder the parameters: lifetimes, then consts and types: `<'a, const N: usize, T = u32>` error: aborting due to 2 previous errors diff --git a/src/test/ui/const-generics/defaults/intermixed-lifetime.min.stderr b/src/test/ui/const-generics/defaults/intermixed-lifetime.min.stderr index 532f6d700b28f..69a490978d1df 100644 --- a/src/test/ui/const-generics/defaults/intermixed-lifetime.min.stderr +++ b/src/test/ui/const-generics/defaults/intermixed-lifetime.min.stderr @@ -2,25 +2,25 @@ error: lifetime parameters must be declared prior to const parameters --> $DIR/intermixed-lifetime.rs:6:28 | LL | struct Foo(&'a (), T); - | -----------------^^---------- help: reorder the parameters: lifetimes, then types, then consts: `<'a, T, const N: usize>` + | -----------------^^---------- help: reorder the parameters: lifetimes, then types, then consts: `<'a, T = u32, const N: usize>` error: type parameters must be declared prior to const parameters --> $DIR/intermixed-lifetime.rs:6:32 | LL | struct Foo(&'a (), T); - | ---------------------^------- help: reorder the parameters: lifetimes, then types, then consts: `<'a, T, const N: usize>` + | ---------------------^------- help: reorder the parameters: lifetimes, then types, then consts: `<'a, T = u32, const N: usize>` error: lifetime parameters must be declared prior to const parameters --> $DIR/intermixed-lifetime.rs:10:37 | LL | struct Bar(&'a (), T); - | --------------------------^^- help: reorder the parameters: lifetimes, then types, then consts: `<'a, T, const N: usize>` + | --------------------------^^- help: reorder the parameters: lifetimes, then types, then consts: `<'a, T = u32, const N: usize>` error: type parameters must be declared prior to const parameters --> $DIR/intermixed-lifetime.rs:10:28 | LL | struct Bar(&'a (), T); - | -----------------^----------- help: reorder the parameters: lifetimes, then types, then consts: `<'a, T, const N: usize>` + | -----------------^----------- help: reorder the parameters: lifetimes, then types, then consts: `<'a, T = u32, const N: usize>` error: aborting due to 4 previous errors diff --git a/src/test/ui/const-generics/defaults/needs-feature.min.stderr b/src/test/ui/const-generics/defaults/needs-feature.min.stderr index 86d6173fa0273..a4006203e4a27 100644 --- a/src/test/ui/const-generics/defaults/needs-feature.min.stderr +++ b/src/test/ui/const-generics/defaults/needs-feature.min.stderr @@ -2,7 +2,7 @@ error: type parameters must be declared prior to const parameters --> $DIR/needs-feature.rs:9:26 | LL | struct A(T); - | -----------------^----- help: reorder the parameters: lifetimes, then types, then consts: `` + | -----------------^----- help: reorder the parameters: lifetimes, then types, then consts: `` error: aborting due to previous error diff --git a/src/test/ui/const-generics/defaults/needs-feature.none.stderr b/src/test/ui/const-generics/defaults/needs-feature.none.stderr index 86d6173fa0273..a4006203e4a27 100644 --- a/src/test/ui/const-generics/defaults/needs-feature.none.stderr +++ b/src/test/ui/const-generics/defaults/needs-feature.none.stderr @@ -2,7 +2,7 @@ error: type parameters must be declared prior to const parameters --> $DIR/needs-feature.rs:9:26 | LL | struct A(T); - | -----------------^----- help: reorder the parameters: lifetimes, then types, then consts: `` + | -----------------^----- help: reorder the parameters: lifetimes, then types, then consts: `` error: aborting due to previous error diff --git a/src/test/ui/const-generics/defaults/simple-defaults.min.stderr b/src/test/ui/const-generics/defaults/simple-defaults.min.stderr index 01fb4210dd63f..0746c64ac8cf4 100644 --- a/src/test/ui/const-generics/defaults/simple-defaults.min.stderr +++ b/src/test/ui/const-generics/defaults/simple-defaults.min.stderr @@ -2,7 +2,7 @@ error: type parameters must be declared prior to const parameters --> $DIR/simple-defaults.rs:8:40 | LL | struct FixedOutput<'a, const N: usize, T=u32> { - | ---------------------^----- help: reorder the parameters: lifetimes, then types, then consts: `<'a, T, const N: usize>` + | ---------------------^----- help: reorder the parameters: lifetimes, then types, then consts: `<'a, T = u32, const N: usize>` error: aborting due to previous error diff --git a/src/test/ui/issues/issue-80512-param-reordering-with-defaults.rs b/src/test/ui/issues/issue-80512-param-reordering-with-defaults.rs new file mode 100644 index 0000000000000..fe3e4fbc7e0b6 --- /dev/null +++ b/src/test/ui/issues/issue-80512-param-reordering-with-defaults.rs @@ -0,0 +1,4 @@ +#![crate_type = "lib"] + +struct S(&'a T); +//~^ ERROR lifetime parameters must be declared prior to type parameters diff --git a/src/test/ui/issues/issue-80512-param-reordering-with-defaults.stderr b/src/test/ui/issues/issue-80512-param-reordering-with-defaults.stderr new file mode 100644 index 0000000000000..a1e9a903f8102 --- /dev/null +++ b/src/test/ui/issues/issue-80512-param-reordering-with-defaults.stderr @@ -0,0 +1,8 @@ +error: lifetime parameters must be declared prior to type parameters + --> $DIR/issue-80512-param-reordering-with-defaults.rs:3:18 + | +LL | struct S(&'a T); + | ---------^^- help: reorder the parameters: lifetimes, then types, then consts: `<'a, T = ()>` + +error: aborting due to previous error + From 75705ab3a92bdc5bd5de1aba93013ecd852be6f7 Mon Sep 17 00:00:00 2001 From: Camelid Date: Wed, 30 Dec 2020 16:38:25 -0800 Subject: [PATCH 13/23] Update `find_nearest_parent_module` --- src/librustdoc/clean/utils.rs | 12 +++++------- src/librustdoc/html/format.rs | 6 +++--- src/librustdoc/passes/collect_intra_doc_links.rs | 9 +++++++-- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs index b49ed07f8e83f..4009a42955f87 100644 --- a/src/librustdoc/clean/utils.rs +++ b/src/librustdoc/clean/utils.rs @@ -625,13 +625,11 @@ where r } -crate fn find_closest_parent_module(tcx: TyCtxt<'_>, def_id: DefId) -> Option { - if item.is_fake() { - // FIXME: is this correct? - None - // If we're documenting the crate root itself, it has no parent. Use the root instead. - } else if item.def_id.is_top_level_module() { - Some(item.def_id) +/// Find the nearest parent module of a [`DefId`]. +crate fn find_nearest_parent_module(tcx: TyCtxt<'_>, def_id: DefId) -> Option { + if def_id.is_top_level_module() { + // The crate root has no parent. Use it as the root instead. + Some(def_id) } else { let mut current = def_id; // The immediate parent might not always be a module. diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 90f4aaefc9bc1..9fca005c34ee0 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -15,7 +15,7 @@ use rustc_middle::ty::TyCtxt; use rustc_span::def_id::{DefId, CRATE_DEF_INDEX}; use rustc_target::spec::abi::Abi; -use crate::clean::{self, utils::find_closest_parent_module, PrimitiveType}; +use crate::clean::{self, utils::find_nearest_parent_module, PrimitiveType}; use crate::formats::cache::cache; use crate::formats::item_type::ItemType; use crate::html::escape::Escape; @@ -1097,7 +1097,7 @@ impl clean::Visibility { clean::Inherited => Ok(()), clean::Visibility::Restricted(vis_did) => { - let parent_module = find_closest_parent_module(tcx, item_did); + let parent_module = find_nearest_parent_module(tcx, item_did); if vis_did.index == CRATE_DEF_INDEX { write!(f, "pub(crate) ") @@ -1106,7 +1106,7 @@ impl clean::Visibility { // is the same as no visibility modifier Ok(()) } else if parent_module - .map(|parent| find_closest_parent_module(tcx, parent)) + .map(|parent| find_nearest_parent_module(tcx, parent)) .flatten() == Some(vis_did) { diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index 4e261c3fd1934..e225eb47b12be 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -31,7 +31,7 @@ use std::cell::Cell; use std::mem; use std::ops::Range; -use crate::clean::{self, utils::find_closest_parent_module, Crate, Item, ItemLink, PrimitiveType}; +use crate::clean::{self, utils::find_nearest_parent_module, Crate, Item, ItemLink, PrimitiveType}; use crate::core::DocContext; use crate::fold::DocFolder; use crate::html::markdown::markdown_links; @@ -767,7 +767,12 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> { fn fold_item(&mut self, mut item: Item) -> Option { use rustc_middle::ty::DefIdTree; - let parent_node = find_closest_parent_module(self.cx.tcx, item.def_id); + let parent_node = if item.is_fake() { + // FIXME: is this correct? + None + } else { + find_nearest_parent_module(self.cx.tcx, item.def_id) + }; if parent_node.is_some() { trace!("got parent node for {:?} {:?}, id {:?}", item.type_(), item.name, item.def_id); From 6c86ebab7491b34f4048af07722afa9301326dd1 Mon Sep 17 00:00:00 2001 From: Camelid Date: Wed, 30 Dec 2020 16:29:47 -0800 Subject: [PATCH 14/23] Remove FIXME Co-authored-by: Joshua Nelson --- src/librustdoc/passes/collect_intra_doc_links.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index e225eb47b12be..2e116da6ff5dd 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -768,7 +768,6 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> { use rustc_middle::ty::DefIdTree; let parent_node = if item.is_fake() { - // FIXME: is this correct? None } else { find_nearest_parent_module(self.cx.tcx, item.def_id) From 6f6afae41aee721fd259a8a764ec96f93bc07911 Mon Sep 17 00:00:00 2001 From: Camelid Date: Wed, 30 Dec 2020 16:41:18 -0800 Subject: [PATCH 15/23] Small refactor --- src/librustdoc/clean/mod.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 666d418cc0e2f..ae329dc62f142 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -2295,20 +2295,19 @@ impl Clean for (&hir::MacroDef<'_>, Option) { ) } else { let vis = item.vis.clean(cx); - let vis_printed_with_space = - vis.print_with_space(cx.tcx, cx.tcx.hir().local_def_id(item.hir_id).to_def_id()); + let def_id = cx.tcx.hir().local_def_id(item.hir_id).to_def_id(); if matchers.len() <= 1 { format!( "{}macro {}{} {{\n ...\n}}", - vis_printed_with_space, + vis.print_with_space(cx.tcx, def_id), name, matchers.iter().map(|span| span.to_src(cx)).collect::(), ) } else { format!( "{}macro {} {{\n{}}}", - vis_printed_with_space, + vis.print_with_space(cx.tcx, def_id), name, matchers .iter() From f7f14f6f0b6cde5ab9f5b644d0939fb1232b9c15 Mon Sep 17 00:00:00 2001 From: Camelid Date: Wed, 30 Dec 2020 17:39:03 -0800 Subject: [PATCH 16/23] Add note on panic behavior --- src/librustdoc/clean/utils.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs index 4009a42955f87..17f12d0a82f2e 100644 --- a/src/librustdoc/clean/utils.rs +++ b/src/librustdoc/clean/utils.rs @@ -626,6 +626,8 @@ where } /// Find the nearest parent module of a [`DefId`]. +/// +/// **Panics if the item it belongs to [is fake][Item::is_fake].** crate fn find_nearest_parent_module(tcx: TyCtxt<'_>, def_id: DefId) -> Option { if def_id.is_top_level_module() { // The crate root has no parent. Use it as the root instead. From 70089110803f7548e161503964794667c33547cc Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Thu, 31 Dec 2020 11:25:53 +0900 Subject: [PATCH 17/23] FIx ICE on wf check for foreign fns --- .../src/infer/error_reporting/mod.rs | 8 +++++++ compiler/rustc_typeck/src/check/wfcheck.rs | 2 +- .../wf/wf-in-foreign-fn-decls-issue-80468.rs | 17 +++++++++++++ .../wf-in-foreign-fn-decls-issue-80468.stderr | 24 +++++++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/wf/wf-in-foreign-fn-decls-issue-80468.rs create mode 100644 src/test/ui/wf/wf-in-foreign-fn-decls-issue-80468.stderr diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index 6d6bf4bf5f705..777107ed86310 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -153,6 +153,7 @@ fn msg_span_from_early_bound_and_free_regions( Some(Node::Item(it)) => item_scope_tag(&it), Some(Node::TraitItem(it)) => trait_item_scope_tag(&it), Some(Node::ImplItem(it)) => impl_item_scope_tag(&it), + Some(Node::ForeignItem(it)) => foreign_item_scope_tag(&it), _ => unreachable!(), }; let (prefix, span) = match *region { @@ -233,6 +234,13 @@ fn impl_item_scope_tag(item: &hir::ImplItem<'_>) -> &'static str { } } +fn foreign_item_scope_tag(item: &hir::ForeignItem<'_>) -> &'static str { + match item.kind { + hir::ForeignItemKind::Fn(..) => "method body", + hir::ForeignItemKind::Static(..) | hir::ForeignItemKind::Type => "associated item", + } +} + fn explain_span(tcx: TyCtxt<'tcx>, heading: &str, span: Span) -> (String, Option) { let lo = tcx.sess.source_map().lookup_char_pos(span.lo()); (format!("the {} at {}:{}", heading, lo.line, lo.col.to_usize() + 1), Some(span)) diff --git a/compiler/rustc_typeck/src/check/wfcheck.rs b/compiler/rustc_typeck/src/check/wfcheck.rs index cd871a4da979f..2ae9ded3fa0cf 100644 --- a/compiler/rustc_typeck/src/check/wfcheck.rs +++ b/compiler/rustc_typeck/src/check/wfcheck.rs @@ -51,7 +51,7 @@ impl<'tcx> CheckWfFcxBuilder<'tcx> { let fcx = FnCtxt::new(&inh, param_env, id); if !inh.tcx.features().trivial_bounds { // As predicates are cached rather than obligations, this - // needsto be called first so that they are checked with an + // needs to be called first so that they are checked with an // empty `param_env`. check_false_global_bounds(&fcx, span, id); } diff --git a/src/test/ui/wf/wf-in-foreign-fn-decls-issue-80468.rs b/src/test/ui/wf/wf-in-foreign-fn-decls-issue-80468.rs new file mode 100644 index 0000000000000..8386959cfb3a8 --- /dev/null +++ b/src/test/ui/wf/wf-in-foreign-fn-decls-issue-80468.rs @@ -0,0 +1,17 @@ +// Regression test for #80468. + +#![crate_type = "lib"] + +pub trait Trait {} + +#[repr(transparent)] +pub struct Wrapper(T); + +#[repr(transparent)] +pub struct Ref<'a>(&'a u8); + +impl Trait for Ref {} //~ ERROR: implicit elided lifetime not allowed here + +extern "C" { + pub fn repro(_: Wrapper); //~ ERROR: mismatched types +} diff --git a/src/test/ui/wf/wf-in-foreign-fn-decls-issue-80468.stderr b/src/test/ui/wf/wf-in-foreign-fn-decls-issue-80468.stderr new file mode 100644 index 0000000000000..bb839d0a5eca6 --- /dev/null +++ b/src/test/ui/wf/wf-in-foreign-fn-decls-issue-80468.stderr @@ -0,0 +1,24 @@ +error[E0726]: implicit elided lifetime not allowed here + --> $DIR/wf-in-foreign-fn-decls-issue-80468.rs:13:16 + | +LL | impl Trait for Ref {} + | ^^^- help: indicate the anonymous lifetime: `<'_>` + +error[E0308]: mismatched types + --> $DIR/wf-in-foreign-fn-decls-issue-80468.rs:16:21 + | +LL | pub fn repro(_: Wrapper); + | ^^^^^^^^^^^^ lifetime mismatch + | + = note: expected trait `Trait` + found trait `Trait` +note: the anonymous lifetime #1 defined on the method body at 16:5... + --> $DIR/wf-in-foreign-fn-decls-issue-80468.rs:16:5 + | +LL | pub fn repro(_: Wrapper); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = note: ...does not necessarily outlive the static lifetime + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0308`. From aac429ffd312f7d1ee7a8c2a78c68a1839743b26 Mon Sep 17 00:00:00 2001 From: Pietro Albini Date: Wed, 30 Dec 2020 12:10:31 +0100 Subject: [PATCH 18/23] bootstrap: never delete the tarball temporary directory Files in the temporary directory are used by ./x.py install. --- src/bootstrap/dist.rs | 4 ++-- src/bootstrap/tarball.rs | 8 +------- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/src/bootstrap/dist.rs b/src/bootstrap/dist.rs index 0a79d09b27fed..01c4ef4f5ae6e 100644 --- a/src/bootstrap/dist.rs +++ b/src/bootstrap/dist.rs @@ -1316,8 +1316,8 @@ impl Step for Extended { tarballs.push(mingw_installer.unwrap()); } - let mut tarball = Tarball::new(builder, "rust", &target.triple); - let work = tarball.persist_work_dir(); + let tarball = Tarball::new(builder, "rust", &target.triple); + let work = tarball.work_dir(); tarball.combine(&tarballs); let tmp = tmpdir(builder).join("combined-tarball"); diff --git a/src/bootstrap/tarball.rs b/src/bootstrap/tarball.rs index 137370fe6cb4f..91302dc3cfdcb 100644 --- a/src/bootstrap/tarball.rs +++ b/src/bootstrap/tarball.rs @@ -97,7 +97,6 @@ pub(crate) struct Tarball<'a> { include_target_in_component_name: bool, is_preview: bool, - delete_temp_dir: bool, } impl<'a> Tarball<'a> { @@ -136,7 +135,6 @@ impl<'a> Tarball<'a> { include_target_in_component_name: false, is_preview: false, - delete_temp_dir: true, } } @@ -198,8 +196,7 @@ impl<'a> Tarball<'a> { self.builder.cp_r(src.as_ref(), &dest); } - pub(crate) fn persist_work_dir(&mut self) -> PathBuf { - self.delete_temp_dir = false; + pub(crate) fn work_dir(&self) -> PathBuf { self.temp_dir.clone() } @@ -299,9 +296,6 @@ impl<'a> Tarball<'a> { cmd.arg("--compression-formats").arg(formats.join(",")); } self.builder.run(&mut cmd); - if self.delete_temp_dir { - t!(std::fs::remove_dir_all(&self.temp_dir)); - } // Use either the first compression format defined, or "gz" as the default. let ext = self From 1fab57491dbdb739ec940be4169013dc0e9982e1 Mon Sep 17 00:00:00 2001 From: Pietro Albini Date: Wed, 30 Dec 2020 12:20:13 +0100 Subject: [PATCH 19/23] bootstrap: change the dist outputs to GeneratedTarball The struct will allow to store more context on the generated tarballs. --- src/bootstrap/dist.rs | 76 ++++++++++++++++++++-------------------- src/bootstrap/tarball.rs | 29 ++++++++++----- src/bootstrap/test.rs | 7 ++-- 3 files changed, 64 insertions(+), 48 deletions(-) diff --git a/src/bootstrap/dist.rs b/src/bootstrap/dist.rs index 01c4ef4f5ae6e..a6957e410cd18 100644 --- a/src/bootstrap/dist.rs +++ b/src/bootstrap/dist.rs @@ -19,7 +19,7 @@ use crate::builder::{Builder, RunConfig, ShouldRun, Step}; use crate::cache::{Interned, INTERNER}; use crate::compile; use crate::config::TargetSelection; -use crate::tarball::{OverlayKind, Tarball}; +use crate::tarball::{GeneratedTarball, OverlayKind, Tarball}; use crate::tool::{self, Tool}; use crate::util::{exe, is_dylib, timeit}; use crate::{Compiler, DependencyType, Mode, LLVM_TOOLS}; @@ -51,7 +51,7 @@ pub struct Docs { } impl Step for Docs { - type Output = Option; + type Output = Option; const DEFAULT: bool = true; fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { @@ -63,7 +63,7 @@ impl Step for Docs { } /// Builds the `rust-docs` installer component. - fn run(self, builder: &Builder<'_>) -> Option { + fn run(self, builder: &Builder<'_>) -> Option { let host = self.host; if !builder.config.docs { return None; @@ -86,7 +86,7 @@ pub struct RustcDocs { } impl Step for RustcDocs { - type Output = Option; + type Output = Option; const DEFAULT: bool = true; fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { @@ -98,7 +98,7 @@ impl Step for RustcDocs { } /// Builds the `rustc-docs` installer component. - fn run(self, builder: &Builder<'_>) -> Option { + fn run(self, builder: &Builder<'_>) -> Option { let host = self.host; if !builder.config.compiler_docs { return None; @@ -267,7 +267,7 @@ pub struct Mingw { } impl Step for Mingw { - type Output = Option; + type Output = Option; const DEFAULT: bool = true; fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { @@ -282,7 +282,7 @@ impl Step for Mingw { /// /// This contains all the bits and pieces to run the MinGW Windows targets /// without any extra installed software (e.g., we bundle gcc, libraries, etc). - fn run(self, builder: &Builder<'_>) -> Option { + fn run(self, builder: &Builder<'_>) -> Option { let host = self.host; if !host.contains("pc-windows-gnu") { return None; @@ -307,7 +307,7 @@ pub struct Rustc { } impl Step for Rustc { - type Output = PathBuf; + type Output = GeneratedTarball; const DEFAULT: bool = true; const ONLY_HOSTS: bool = true; @@ -321,7 +321,7 @@ impl Step for Rustc { } /// Creates the `rustc` installer component. - fn run(self, builder: &Builder<'_>) -> PathBuf { + fn run(self, builder: &Builder<'_>) -> GeneratedTarball { let compiler = self.compiler; let host = self.compiler.host; @@ -555,7 +555,7 @@ pub struct Std { } impl Step for Std { - type Output = Option; + type Output = Option; const DEFAULT: bool = true; fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { @@ -573,7 +573,7 @@ impl Step for Std { }); } - fn run(self, builder: &Builder<'_>) -> Option { + fn run(self, builder: &Builder<'_>) -> Option { let compiler = self.compiler; let target = self.target; @@ -601,7 +601,7 @@ pub struct RustcDev { } impl Step for RustcDev { - type Output = Option; + type Output = Option; const DEFAULT: bool = true; const ONLY_HOSTS: bool = true; @@ -620,7 +620,7 @@ impl Step for RustcDev { }); } - fn run(self, builder: &Builder<'_>) -> Option { + fn run(self, builder: &Builder<'_>) -> Option { let compiler = self.compiler; let target = self.target; if skip_host_target_lib(builder, compiler) { @@ -660,7 +660,7 @@ pub struct Analysis { } impl Step for Analysis { - type Output = Option; + type Output = Option; const DEFAULT: bool = true; fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { @@ -683,7 +683,7 @@ impl Step for Analysis { } /// Creates a tarball of save-analysis metadata, if available. - fn run(self, builder: &Builder<'_>) -> Option { + fn run(self, builder: &Builder<'_>) -> Option { let compiler = self.compiler; let target = self.target; assert!(builder.config.extended); @@ -796,7 +796,7 @@ pub struct Src; impl Step for Src { /// The output path of the src installer tarball - type Output = PathBuf; + type Output = GeneratedTarball; const DEFAULT: bool = true; const ONLY_HOSTS: bool = true; @@ -809,7 +809,7 @@ impl Step for Src { } /// Creates the `rust-src` installer component - fn run(self, builder: &Builder<'_>) -> PathBuf { + fn run(self, builder: &Builder<'_>) -> GeneratedTarball { let tarball = Tarball::new_targetless(builder, "rust-src"); // A lot of tools expect the rust-src component to be entirely in this directory, so if you @@ -848,7 +848,7 @@ pub struct PlainSourceTarball; impl Step for PlainSourceTarball { /// Produces the location of the tarball generated - type Output = PathBuf; + type Output = GeneratedTarball; const DEFAULT: bool = true; const ONLY_HOSTS: bool = true; @@ -862,7 +862,7 @@ impl Step for PlainSourceTarball { } /// Creates the plain source tarball - fn run(self, builder: &Builder<'_>) -> PathBuf { + fn run(self, builder: &Builder<'_>) -> GeneratedTarball { let tarball = Tarball::new(builder, "rustc", "src"); let plain_dst_src = tarball.image_dir(); @@ -941,7 +941,7 @@ pub struct Cargo { } impl Step for Cargo { - type Output = PathBuf; + type Output = GeneratedTarball; const ONLY_HOSTS: bool = true; fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { @@ -959,7 +959,7 @@ impl Step for Cargo { }); } - fn run(self, builder: &Builder<'_>) -> PathBuf { + fn run(self, builder: &Builder<'_>) -> GeneratedTarball { let compiler = self.compiler; let target = self.target; @@ -995,7 +995,7 @@ pub struct Rls { } impl Step for Rls { - type Output = Option; + type Output = Option; const ONLY_HOSTS: bool = true; fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { @@ -1013,7 +1013,7 @@ impl Step for Rls { }); } - fn run(self, builder: &Builder<'_>) -> Option { + fn run(self, builder: &Builder<'_>) -> Option { let compiler = self.compiler; let target = self.target; assert!(builder.config.extended); @@ -1041,7 +1041,7 @@ pub struct RustAnalyzer { } impl Step for RustAnalyzer { - type Output = Option; + type Output = Option; const ONLY_HOSTS: bool = true; fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { @@ -1059,7 +1059,7 @@ impl Step for RustAnalyzer { }); } - fn run(self, builder: &Builder<'_>) -> Option { + fn run(self, builder: &Builder<'_>) -> Option { let compiler = self.compiler; let target = self.target; assert!(builder.config.extended); @@ -1090,7 +1090,7 @@ pub struct Clippy { } impl Step for Clippy { - type Output = PathBuf; + type Output = GeneratedTarball; const ONLY_HOSTS: bool = true; fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { @@ -1108,7 +1108,7 @@ impl Step for Clippy { }); } - fn run(self, builder: &Builder<'_>) -> PathBuf { + fn run(self, builder: &Builder<'_>) -> GeneratedTarball { let compiler = self.compiler; let target = self.target; assert!(builder.config.extended); @@ -1140,7 +1140,7 @@ pub struct Miri { } impl Step for Miri { - type Output = Option; + type Output = Option; const ONLY_HOSTS: bool = true; fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { @@ -1158,7 +1158,7 @@ impl Step for Miri { }); } - fn run(self, builder: &Builder<'_>) -> Option { + fn run(self, builder: &Builder<'_>) -> Option { let compiler = self.compiler; let target = self.target; assert!(builder.config.extended); @@ -1193,7 +1193,7 @@ pub struct Rustfmt { } impl Step for Rustfmt { - type Output = Option; + type Output = Option; const ONLY_HOSTS: bool = true; fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { @@ -1211,7 +1211,7 @@ impl Step for Rustfmt { }); } - fn run(self, builder: &Builder<'_>) -> Option { + fn run(self, builder: &Builder<'_>) -> Option { let compiler = self.compiler; let target = self.target; @@ -1870,7 +1870,7 @@ pub struct LlvmTools { } impl Step for LlvmTools { - type Output = Option; + type Output = Option; const ONLY_HOSTS: bool = true; fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { @@ -1881,7 +1881,7 @@ impl Step for LlvmTools { run.builder.ensure(LlvmTools { target: run.target }); } - fn run(self, builder: &Builder<'_>) -> Option { + fn run(self, builder: &Builder<'_>) -> Option { let target = self.target; assert!(builder.config.extended); @@ -1924,7 +1924,7 @@ pub struct RustDev { } impl Step for RustDev { - type Output = Option; + type Output = Option; const DEFAULT: bool = true; const ONLY_HOSTS: bool = true; @@ -1936,7 +1936,7 @@ impl Step for RustDev { run.builder.ensure(RustDev { target: run.target }); } - fn run(self, builder: &Builder<'_>) -> Option { + fn run(self, builder: &Builder<'_>) -> Option { let target = self.target; /* run only if llvm-config isn't used */ @@ -1989,7 +1989,7 @@ pub struct BuildManifest { } impl Step for BuildManifest { - type Output = PathBuf; + type Output = GeneratedTarball; const DEFAULT: bool = false; const ONLY_HOSTS: bool = true; @@ -2001,7 +2001,7 @@ impl Step for BuildManifest { run.builder.ensure(BuildManifest { target: run.target }); } - fn run(self, builder: &Builder<'_>) -> PathBuf { + fn run(self, builder: &Builder<'_>) -> GeneratedTarball { let build_manifest = builder.tool_exe(Tool::BuildManifest); let tarball = Tarball::new(builder, "build-manifest", &self.target.triple); @@ -2021,7 +2021,7 @@ pub struct ReproducibleArtifacts { } impl Step for ReproducibleArtifacts { - type Output = Option; + type Output = Option; const DEFAULT: bool = true; const ONLY_HOSTS: bool = true; diff --git a/src/bootstrap/tarball.rs b/src/bootstrap/tarball.rs index 91302dc3cfdcb..06a113ed035ef 100644 --- a/src/bootstrap/tarball.rs +++ b/src/bootstrap/tarball.rs @@ -200,7 +200,7 @@ impl<'a> Tarball<'a> { self.temp_dir.clone() } - pub(crate) fn generate(self) -> PathBuf { + pub(crate) fn generate(self) -> GeneratedTarball { let mut component_name = self.component.clone(); if self.is_preview { component_name.push_str("-preview"); @@ -224,20 +224,20 @@ impl<'a> Tarball<'a> { }) } - pub(crate) fn combine(self, tarballs: &[PathBuf]) { - let mut input_tarballs = tarballs[0].as_os_str().to_os_string(); + pub(crate) fn combine(self, tarballs: &[GeneratedTarball]) -> GeneratedTarball { + let mut input_tarballs = tarballs[0].path.as_os_str().to_os_string(); for tarball in &tarballs[1..] { input_tarballs.push(","); - input_tarballs.push(tarball); + input_tarballs.push(&tarball.path); } self.run(|this, cmd| { cmd.arg("combine").arg("--input-tarballs").arg(input_tarballs); this.non_bare_args(cmd); - }); + }) } - pub(crate) fn bare(self) -> PathBuf { + pub(crate) fn bare(self) -> GeneratedTarball { // Bare tarballs should have the top level directory match the package // name, not "image". We rename the image directory just before passing // into rust-installer. @@ -273,7 +273,7 @@ impl<'a> Tarball<'a> { .arg(crate::dist::distdir(self.builder)); } - fn run(self, build_cli: impl FnOnce(&Tarball<'a>, &mut Command)) -> PathBuf { + fn run(self, build_cli: impl FnOnce(&Tarball<'a>, &mut Command)) -> GeneratedTarball { t!(std::fs::create_dir_all(&self.overlay_dir)); self.builder.create(&self.overlay_dir.join("version"), &self.overlay.version(self.builder)); if let Some(sha) = self.builder.rust_sha() { @@ -307,6 +307,19 @@ impl<'a> Tarball<'a> { .map(|s| s.as_str()) .unwrap_or("gz"); - crate::dist::distdir(self.builder).join(format!("{}.tar.{}", package_name, ext)) + GeneratedTarball { + path: crate::dist::distdir(self.builder).join(format!("{}.tar.{}", package_name, ext)), + } + } +} + +#[derive(Debug, Clone)] +pub struct GeneratedTarball { + path: PathBuf, +} + +impl GeneratedTarball { + pub(crate) fn tarball(&self) -> &Path { + &self.path } } diff --git a/src/bootstrap/test.rs b/src/bootstrap/test.rs index 1f209f328a2bd..33e252a63c9a5 100644 --- a/src/bootstrap/test.rs +++ b/src/bootstrap/test.rs @@ -1963,7 +1963,7 @@ impl Step for Distcheck { let mut cmd = Command::new("tar"); cmd.arg("-xf") - .arg(builder.ensure(dist::PlainSourceTarball)) + .arg(builder.ensure(dist::PlainSourceTarball).tarball()) .arg("--strip-components=1") .current_dir(&dir); builder.run(&mut cmd); @@ -1986,7 +1986,10 @@ impl Step for Distcheck { t!(fs::create_dir_all(&dir)); let mut cmd = Command::new("tar"); - cmd.arg("-xf").arg(builder.ensure(dist::Src)).arg("--strip-components=1").current_dir(&dir); + cmd.arg("-xf") + .arg(builder.ensure(dist::Src).tarball()) + .arg("--strip-components=1") + .current_dir(&dir); builder.run(&mut cmd); let toml = dir.join("rust-src/lib/rustlib/src/rust/library/std/Cargo.toml"); From 8e0ab0fb5e35c96ba92c586eb9e0aa684fcd567d Mon Sep 17 00:00:00 2001 From: Pietro Albini Date: Wed, 30 Dec 2020 14:00:24 +0100 Subject: [PATCH 20/23] bootstrap: use the correct paths during ./x.py install --- src/bootstrap/dist.rs | 4 +- src/bootstrap/install.rs | 106 ++++++++++++--------------------------- src/bootstrap/tarball.rs | 16 ++++-- 3 files changed, 45 insertions(+), 81 deletions(-) diff --git a/src/bootstrap/dist.rs b/src/bootstrap/dist.rs index a6957e410cd18..daec1656b27cd 100644 --- a/src/bootstrap/dist.rs +++ b/src/bootstrap/dist.rs @@ -1317,10 +1317,10 @@ impl Step for Extended { } let tarball = Tarball::new(builder, "rust", &target.triple); - let work = tarball.work_dir(); - tarball.combine(&tarballs); + let generated = tarball.combine(&tarballs); let tmp = tmpdir(builder).join("combined-tarball"); + let work = generated.work_dir(); let mut license = String::new(); license += &builder.read(&builder.src.join("COPYRIGHT")); diff --git a/src/bootstrap/install.rs b/src/bootstrap/install.rs index 8f2b128b3688e..96164947943ba 100644 --- a/src/bootstrap/install.rs +++ b/src/bootstrap/install.rs @@ -10,60 +10,19 @@ use std::process::Command; use build_helper::t; -use crate::dist::{self, pkgname, sanitize_sh, tmpdir}; +use crate::dist::{self, sanitize_sh}; +use crate::tarball::GeneratedTarball; use crate::Compiler; use crate::builder::{Builder, RunConfig, ShouldRun, Step}; use crate::config::{Config, TargetSelection}; -pub fn install_docs(builder: &Builder<'_>, stage: u32, host: TargetSelection) { - install_sh(builder, "docs", "rust-docs", stage, Some(host)); -} - -pub fn install_std(builder: &Builder<'_>, stage: u32, target: TargetSelection) { - install_sh(builder, "std", "rust-std", stage, Some(target)); -} - -pub fn install_cargo(builder: &Builder<'_>, stage: u32, host: TargetSelection) { - install_sh(builder, "cargo", "cargo", stage, Some(host)); -} - -pub fn install_rls(builder: &Builder<'_>, stage: u32, host: TargetSelection) { - install_sh(builder, "rls", "rls", stage, Some(host)); -} - -pub fn install_rust_analyzer(builder: &Builder<'_>, stage: u32, host: TargetSelection) { - install_sh(builder, "rust-analyzer", "rust-analyzer", stage, Some(host)); -} - -pub fn install_clippy(builder: &Builder<'_>, stage: u32, host: TargetSelection) { - install_sh(builder, "clippy", "clippy", stage, Some(host)); -} -pub fn install_miri(builder: &Builder<'_>, stage: u32, host: TargetSelection) { - install_sh(builder, "miri", "miri", stage, Some(host)); -} - -pub fn install_rustfmt(builder: &Builder<'_>, stage: u32, host: TargetSelection) { - install_sh(builder, "rustfmt", "rustfmt", stage, Some(host)); -} - -pub fn install_analysis(builder: &Builder<'_>, stage: u32, host: TargetSelection) { - install_sh(builder, "analysis", "rust-analysis", stage, Some(host)); -} - -pub fn install_src(builder: &Builder<'_>, stage: u32) { - install_sh(builder, "src", "rust-src", stage, None); -} -pub fn install_rustc(builder: &Builder<'_>, stage: u32, host: TargetSelection) { - install_sh(builder, "rustc", "rustc", stage, Some(host)); -} - fn install_sh( builder: &Builder<'_>, package: &str, - name: &str, stage: u32, host: Option, + tarball: &GeneratedTarball, ) { builder.info(&format!("Install {} stage{} ({:?})", package, stage, host)); @@ -108,15 +67,10 @@ fn install_sh( let empty_dir = builder.out.join("tmp/empty_dir"); t!(fs::create_dir_all(&empty_dir)); - let package_name = if let Some(host) = host { - format!("{}-{}", pkgname(builder, name), host.triple) - } else { - pkgname(builder, name) - }; let mut cmd = Command::new("sh"); cmd.current_dir(&empty_dir) - .arg(sanitize_sh(&tmpdir(builder).join(&package_name).join("install.sh"))) + .arg(sanitize_sh(&tarball.decompressed_output().join("install.sh"))) .arg(format!("--prefix={}", sanitize_sh(&prefix))) .arg(format!("--sysconfdir={}", sanitize_sh(&sysconfdir))) .arg(format!("--datadir={}", sanitize_sh(&datadir))) @@ -191,25 +145,25 @@ macro_rules! install { install!((self, builder, _config), Docs, "src/doc", _config.docs, only_hosts: false, { - builder.ensure(dist::Docs { host: self.target }); - install_docs(builder, self.compiler.stage, self.target); + let tarball = builder.ensure(dist::Docs { host: self.target }).expect("missing docs"); + install_sh(builder, "docs", self.compiler.stage, Some(self.target), &tarball); }; Std, "library/std", true, only_hosts: false, { for target in &builder.targets { - builder.ensure(dist::Std { + let tarball = builder.ensure(dist::Std { compiler: self.compiler, target: *target - }); - install_std(builder, self.compiler.stage, *target); + }).expect("missing std"); + install_sh(builder, "std", self.compiler.stage, Some(*target), &tarball); } }; Cargo, "cargo", Self::should_build(_config), only_hosts: true, { - builder.ensure(dist::Cargo { compiler: self.compiler, target: self.target }); - install_cargo(builder, self.compiler.stage, self.target); + let tarball = builder.ensure(dist::Cargo { compiler: self.compiler, target: self.target }); + install_sh(builder, "cargo", self.compiler.stage, Some(self.target), &tarball); }; Rls, "rls", Self::should_build(_config), only_hosts: true, { - if builder.ensure(dist::Rls { compiler: self.compiler, target: self.target }).is_some() { - install_rls(builder, self.compiler.stage, self.target); + if let Some(tarball) = builder.ensure(dist::Rls { compiler: self.compiler, target: self.target }) { + install_sh(builder, "rls", self.compiler.stage, Some(self.target), &tarball); } else { builder.info( &format!("skipping Install RLS stage{} ({})", self.compiler.stage, self.target), @@ -217,16 +171,18 @@ install!((self, builder, _config), } }; RustAnalyzer, "rust-analyzer", Self::should_build(_config), only_hosts: true, { - builder.ensure(dist::RustAnalyzer { compiler: self.compiler, target: self.target }); - install_rust_analyzer(builder, self.compiler.stage, self.target); + let tarball = builder + .ensure(dist::RustAnalyzer { compiler: self.compiler, target: self.target }) + .expect("missing rust-analyzer"); + install_sh(builder, "rust-analyzer", self.compiler.stage, Some(self.target), &tarball); }; Clippy, "clippy", Self::should_build(_config), only_hosts: true, { - builder.ensure(dist::Clippy { compiler: self.compiler, target: self.target }); - install_clippy(builder, self.compiler.stage, self.target); + let tarball = builder.ensure(dist::Clippy { compiler: self.compiler, target: self.target }); + install_sh(builder, "clippy", self.compiler.stage, Some(self.target), &tarball); }; Miri, "miri", Self::should_build(_config), only_hosts: true, { - if builder.ensure(dist::Miri { compiler: self.compiler, target: self.target }).is_some() { - install_miri(builder, self.compiler.stage, self.target); + if let Some(tarball) = builder.ensure(dist::Miri { compiler: self.compiler, target: self.target }) { + install_sh(builder, "miri", self.compiler.stage, Some(self.target), &tarball); } else { builder.info( &format!("skipping Install miri stage{} ({})", self.compiler.stage, self.target), @@ -234,11 +190,11 @@ install!((self, builder, _config), } }; Rustfmt, "rustfmt", Self::should_build(_config), only_hosts: true, { - if builder.ensure(dist::Rustfmt { + if let Some(tarball) = builder.ensure(dist::Rustfmt { compiler: self.compiler, target: self.target - }).is_some() { - install_rustfmt(builder, self.compiler.stage, self.target); + }) { + install_sh(builder, "rustfmt", self.compiler.stage, Some(self.target), &tarball); } else { builder.info( &format!("skipping Install Rustfmt stage{} ({})", self.compiler.stage, self.target), @@ -246,20 +202,20 @@ install!((self, builder, _config), } }; Analysis, "analysis", Self::should_build(_config), only_hosts: false, { - builder.ensure(dist::Analysis { + let tarball = builder.ensure(dist::Analysis { // Find the actual compiler (handling the full bootstrap option) which // produced the save-analysis data because that data isn't copied // through the sysroot uplifting. compiler: builder.compiler_for(builder.top_stage, builder.config.build, self.target), target: self.target - }); - install_analysis(builder, self.compiler.stage, self.target); + }).expect("missing analysis"); + install_sh(builder, "analysis", self.compiler.stage, Some(self.target), &tarball); }; Rustc, "src/librustc", true, only_hosts: true, { - builder.ensure(dist::Rustc { + let tarball = builder.ensure(dist::Rustc { compiler: builder.compiler(builder.top_stage, self.target), }); - install_rustc(builder, self.compiler.stage, self.target); + install_sh(builder, "rustc", self.compiler.stage, Some(self.target), &tarball); }; ); @@ -284,7 +240,7 @@ impl Step for Src { } fn run(self, builder: &Builder<'_>) { - builder.ensure(dist::Src); - install_src(builder, self.stage); + let tarball = builder.ensure(dist::Src); + install_sh(builder, "src", self.stage, None, &tarball); } } diff --git a/src/bootstrap/tarball.rs b/src/bootstrap/tarball.rs index 06a113ed035ef..7fb03056f1bd3 100644 --- a/src/bootstrap/tarball.rs +++ b/src/bootstrap/tarball.rs @@ -196,10 +196,6 @@ impl<'a> Tarball<'a> { self.builder.cp_r(src.as_ref(), &dest); } - pub(crate) fn work_dir(&self) -> PathBuf { - self.temp_dir.clone() - } - pub(crate) fn generate(self) -> GeneratedTarball { let mut component_name = self.component.clone(); if self.is_preview { @@ -309,6 +305,8 @@ impl<'a> Tarball<'a> { GeneratedTarball { path: crate::dist::distdir(self.builder).join(format!("{}.tar.{}", package_name, ext)), + decompressed_output: self.temp_dir.join(package_name), + work: self.temp_dir, } } } @@ -316,10 +314,20 @@ impl<'a> Tarball<'a> { #[derive(Debug, Clone)] pub struct GeneratedTarball { path: PathBuf, + decompressed_output: PathBuf, + work: PathBuf, } impl GeneratedTarball { pub(crate) fn tarball(&self) -> &Path { &self.path } + + pub(crate) fn decompressed_output(&self) -> &Path { + &self.decompressed_output + } + + pub(crate) fn work_dir(&self) -> &Path { + &self.work + } } From 2aef46b17b3d1e2ddd4b985a6b59853746c04b86 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Thu, 31 Dec 2020 10:55:20 +0100 Subject: [PATCH 21/23] support pattern as const parents in type_of --- compiler/rustc_hir/src/hir.rs | 10 ++-- compiler/rustc_typeck/src/collect/type_of.rs | 52 +++++++++++++++++--- src/test/ui/const-generics/arg-in-pat-1.rs | 23 +++++++++ src/test/ui/const-generics/arg-in-pat-2.rs | 10 ++++ src/test/ui/const-generics/arg-in-pat-3.rs | 43 ++++++++++++++++ 5 files changed, 127 insertions(+), 11 deletions(-) create mode 100644 src/test/ui/const-generics/arg-in-pat-1.rs create mode 100644 src/test/ui/const-generics/arg-in-pat-2.rs create mode 100644 src/test/ui/const-generics/arg-in-pat-3.rs diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 23999a8dca00f..ad681115812cb 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -760,9 +760,9 @@ pub struct Pat<'hir> { pub default_binding_modes: bool, } -impl Pat<'_> { +impl<'hir> Pat<'hir> { // FIXME(#19596) this is a workaround, but there should be a better way - fn walk_short_(&self, it: &mut impl FnMut(&Pat<'_>) -> bool) -> bool { + fn walk_short_(&self, it: &mut impl FnMut(&Pat<'hir>) -> bool) -> bool { if !it(self) { return false; } @@ -785,12 +785,12 @@ impl Pat<'_> { /// Note that when visiting e.g. `Tuple(ps)`, /// if visiting `ps[0]` returns `false`, /// then `ps[1]` will not be visited. - pub fn walk_short(&self, mut it: impl FnMut(&Pat<'_>) -> bool) -> bool { + pub fn walk_short(&self, mut it: impl FnMut(&Pat<'hir>) -> bool) -> bool { self.walk_short_(&mut it) } // FIXME(#19596) this is a workaround, but there should be a better way - fn walk_(&self, it: &mut impl FnMut(&Pat<'_>) -> bool) { + fn walk_(&self, it: &mut impl FnMut(&Pat<'hir>) -> bool) { if !it(self) { return; } @@ -810,7 +810,7 @@ impl Pat<'_> { /// Walk the pattern in left-to-right order. /// /// If `it(pat)` returns `false`, the children are not visited. - pub fn walk(&self, mut it: impl FnMut(&Pat<'_>) -> bool) { + pub fn walk(&self, mut it: impl FnMut(&Pat<'hir>) -> bool) { self.walk_(&mut it) } diff --git a/compiler/rustc_typeck/src/collect/type_of.rs b/compiler/rustc_typeck/src/collect/type_of.rs index 88ba5788b05d1..3c97b55005c44 100644 --- a/compiler/rustc_typeck/src/collect/type_of.rs +++ b/compiler/rustc_typeck/src/collect/type_of.rs @@ -6,7 +6,7 @@ use rustc_hir::def::{DefKind, Res}; use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::intravisit; use rustc_hir::intravisit::Visitor; -use rustc_hir::Node; +use rustc_hir::{HirId, Node}; use rustc_middle::hir::map::Map; use rustc_middle::ty::subst::{GenericArgKind, InternalSubsts}; use rustc_middle::ty::util::IntTypeExt; @@ -22,7 +22,6 @@ use super::{bad_placeholder_type, is_suggestable_infer_ty}; /// This should be called using the query `tcx.opt_const_param_of`. pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option { use hir::*; - let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); if let Node::AnonConst(_) = tcx.hir().get(hir_id) { @@ -62,9 +61,9 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option< } Node::Ty(&Ty { kind: TyKind::Path(_), .. }) - | Node::Expr(&Expr { kind: ExprKind::Struct(..), .. }) - | Node::Expr(&Expr { kind: ExprKind::Path(_), .. }) - | Node::TraitRef(..) => { + | Node::Expr(&Expr { kind: ExprKind::Path(_) | ExprKind::Struct(..), .. }) + | Node::TraitRef(..) + | Node::Pat(_) => { let path = match parent_node { Node::Ty(&Ty { kind: TyKind::Path(QPath::Resolved(_, path)), .. }) | Node::TraitRef(&TraitRef { path, .. }) => &*path, @@ -79,6 +78,20 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option< let _tables = tcx.typeck(body_owner); &*path } + Node::Pat(pat) => { + if let Some(path) = get_path_containing_arg_in_pat(pat, hir_id) { + path + } else { + tcx.sess.delay_span_bug( + tcx.def_span(def_id), + &format!( + "unable to find const parent for {} in pat {:?}", + hir_id, pat + ), + ); + return None; + } + } _ => { tcx.sess.delay_span_bug( tcx.def_span(def_id), @@ -91,7 +104,6 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option< // We've encountered an `AnonConst` in some path, so we need to // figure out which generic parameter it corresponds to and return // the relevant type. - let (arg_index, segment) = path .segments .iter() @@ -144,6 +156,34 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option< } } +fn get_path_containing_arg_in_pat<'hir>( + pat: &'hir hir::Pat<'hir>, + arg_id: HirId, +) -> Option<&'hir hir::Path<'hir>> { + use hir::*; + + let is_arg_in_path = |p: &hir::Path<'_>| { + p.segments + .iter() + .filter_map(|seg| seg.args) + .flat_map(|args| args.args) + .any(|arg| arg.id() == arg_id) + }; + let mut arg_path = None; + pat.walk(|pat| match pat.kind { + PatKind::Struct(QPath::Resolved(_, path), _, _) + | PatKind::TupleStruct(QPath::Resolved(_, path), _, _) + | PatKind::Path(QPath::Resolved(_, path)) + if is_arg_in_path(path) => + { + arg_path = Some(path); + false + } + _ => true, + }); + arg_path +} + pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { let def_id = def_id.expect_local(); use rustc_hir::*; diff --git a/src/test/ui/const-generics/arg-in-pat-1.rs b/src/test/ui/const-generics/arg-in-pat-1.rs new file mode 100644 index 0000000000000..82555084e418f --- /dev/null +++ b/src/test/ui/const-generics/arg-in-pat-1.rs @@ -0,0 +1,23 @@ +// check-pass +enum ConstGenericEnum { + Foo([i32; N]), + Bar, +} + +fn foo(val: &ConstGenericEnum) { + if let ConstGenericEnum::::Foo(field, ..) = val {} +} + +fn bar(val: &ConstGenericEnum) { + match val { + ConstGenericEnum::::Foo(field, ..) => (), + ConstGenericEnum::::Bar => (), + } +} + +fn main() { + match ConstGenericEnum::Bar { + ConstGenericEnum::<3>::Foo(field, ..) => (), + ConstGenericEnum::<3>::Bar => (), + } +} diff --git a/src/test/ui/const-generics/arg-in-pat-2.rs b/src/test/ui/const-generics/arg-in-pat-2.rs new file mode 100644 index 0000000000000..dc9e722eda84c --- /dev/null +++ b/src/test/ui/const-generics/arg-in-pat-2.rs @@ -0,0 +1,10 @@ +// check-pass +enum Generic { + Variant, +} + +fn main() { + match todo!() { + Generic::<0usize>::Variant => todo!() + } +} diff --git a/src/test/ui/const-generics/arg-in-pat-3.rs b/src/test/ui/const-generics/arg-in-pat-3.rs new file mode 100644 index 0000000000000..24626a3b68ae5 --- /dev/null +++ b/src/test/ui/const-generics/arg-in-pat-3.rs @@ -0,0 +1,43 @@ +// check-pass +struct Foo; + +fn bindingp() { + match Foo { + mut x @ Foo::<3> => { + let ref mut _x @ Foo::<3> = x; + } + } +} + +struct Bar { + field: Foo, +} + +fn structp() { + match todo!() { + Bar::<3> { + field: Foo::<3>, + } => (), + } +} + +struct Baz(Foo); + +fn tuplestructp() { + match Baz(Foo) { + Baz::<3>(Foo::<3>) => (), + } +} + +impl Baz { + const ASSOC: usize = 3; +} + +fn pathp() { + match 3 { + Baz::<3>::ASSOC => (), + _ => (), + } +} + +fn main() {} From dda887a02c7df86f6e713d853b887d79be8cf803 Mon Sep 17 00:00:00 2001 From: Camelid Date: Thu, 31 Dec 2020 12:00:23 -0800 Subject: [PATCH 22/23] Add FIXME for visibility of a module --- src/librustdoc/html/format.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 9fca005c34ee0..9b2fb8582f54f 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -1097,6 +1097,9 @@ impl clean::Visibility { clean::Inherited => Ok(()), clean::Visibility::Restricted(vis_did) => { + // FIXME(camelid): This may not work correctly if `item_did` is a module. + // However, rustdoc currently never displays a module's + // visibility, so it shouldn't matter. let parent_module = find_nearest_parent_module(tcx, item_did); if vis_did.index == CRATE_DEF_INDEX { From 5604a18a60fc99a4a7feb82a280546971adda52d Mon Sep 17 00:00:00 2001 From: Camelid Date: Thu, 31 Dec 2020 12:04:13 -0800 Subject: [PATCH 23/23] Add `@!has` checks to ensure private items don't have `pub` --- src/test/rustdoc/visibility.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/test/rustdoc/visibility.rs b/src/test/rustdoc/visibility.rs index ebb314a7941b6..59427693c5a5d 100644 --- a/src/test/rustdoc/visibility.rs +++ b/src/test/rustdoc/visibility.rs @@ -23,8 +23,10 @@ mod a { // @has 'foo/a/struct.FooAInSuper.html' '//pre' 'pub(crate) struct FooAInSuper' pub(in super) struct FooAInSuper; // @has 'foo/a/struct.FooAInA.html' '//pre' 'struct FooAInA' + // @!has 'foo/a/struct.FooAInA.html' '//pre' 'pub' pub(in a) struct FooAInA; // @has 'foo/a/struct.FooAPriv.html' '//pre' 'struct FooAPriv' + // @!has 'foo/a/struct.FooAPriv.html' '//pre' 'pub' struct FooAPriv; mod b { @@ -33,8 +35,10 @@ mod a { // @has 'foo/a/b/struct.FooBInSuperSuper.html' '//pre' 'pub(crate) struct FooBInSuperSuper' pub(in super::super) struct FooBInSuperSuper; // @has 'foo/a/b/struct.FooBInAB.html' '//pre' 'struct FooBInAB' + // @!has 'foo/a/b/struct.FooBInAB.html' '//pre' 'pub' pub(in a::b) struct FooBInAB; // @has 'foo/a/b/struct.FooBPriv.html' '//pre' 'struct FooBPriv' + // @!has 'foo/a/b/struct.FooBPriv.html' '//pre' 'pub' struct FooBPriv; } }