From 3db6d1cc168d6a2be8048c34012526af1ec24213 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Fri, 20 Mar 2020 11:36:57 -0400 Subject: [PATCH 01/11] For issue 53957: revise unit tests to focus on underlying bug of 23076. Namely, this version focuses on the end-to-end behavior that the attempt to create the UDP binding will fail, regardless of the semantics of how particular DNS servers handle junk inputs. (I spent some time trying to create a second more-focused test that would sidestep the DNS resolution, but this is not possible without more invasive changes to the internal infrastructure of `ToSocketAddrs` and what not. It is not worth it.) --- src/libstd/net/addr.rs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/libstd/net/addr.rs b/src/libstd/net/addr.rs index 57cba6b1f7a1b..de6360cf020f5 100644 --- a/src/libstd/net/addr.rs +++ b/src/libstd/net/addr.rs @@ -989,11 +989,26 @@ mod tests { // s has been moved into the tsa call } - // FIXME: figure out why this fails on openbsd and fix it #[test] - #[cfg(not(any(windows, target_os = "openbsd")))] - fn to_socket_addr_str_bad() { - assert!(tsa("1200::AB00:1234::2552:7777:1313:34300").is_err()); + fn bind_udp_socket_bad() { + // rust-lang/rust#53957: This is a regression test for a parsing problem + // discovered as part of issue rust-lang/rust#23076, where we were + // incorrectly parsing invalid input and then that would result in a + // successful `UdpSocket` binding when we would expect failure. + // + // At one time, this test was written as a call to `tsa` with + // INPUT_23076. However, that structure yields an unreliable test, + // because it ends up passing junk input to the DNS server, and some DNS + // servers will respond with `Ok` to such input, with the ip address of + // the DNS server itself. + // + // This form of the test is more robust: even when the DNS server + // returns its own address, it is still an error to bind a UDP socket to + // a non-local address, and so we still get an error here in that case. + + const INPUT_23076: &'static str = "1200::AB00:1234::2552:7777:1313:34300"; + + assert!(crate::net::UdpSocket::bind(INPUT_23076).is_err()) } #[test] From 13dd9aff64a9730574188b373d63dadc719f73b6 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sat, 21 Mar 2020 02:16:27 +0300 Subject: [PATCH 02/11] ast: Compress `AttrId` from `usize` to `u32` Also stop encoding/decoding it entirely --- src/librustc_ast/ast.rs | 22 ++++++++-------------- src/librustc_ast/attr/mod.rs | 8 ++++---- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/src/librustc_ast/ast.rs b/src/librustc_ast/ast.rs index e3077b9897c18..b7b50617eaaf8 100644 --- a/src/librustc_ast/ast.rs +++ b/src/librustc_ast/ast.rs @@ -31,7 +31,6 @@ use crate::tokenstream::{DelimSpan, TokenStream, TokenTree}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::sync::Lrc; use rustc_data_structures::thin_vec::ThinVec; -use rustc_index::vec::Idx; use rustc_macros::HashStable_Generic; use rustc_serialize::{self, Decoder, Encoder}; use rustc_span::source_map::{respan, Spanned}; @@ -2251,27 +2250,22 @@ pub enum AttrStyle { Inner, } -#[derive(Clone, PartialEq, Eq, Hash, Debug, PartialOrd, Ord, Copy)] -pub struct AttrId(pub usize); - -impl Idx for AttrId { - fn new(idx: usize) -> Self { - AttrId(idx) - } - fn index(self) -> usize { - self.0 +rustc_index::newtype_index! { + pub struct AttrId { + ENCODABLE = custom + DEBUG_FORMAT = "AttrId({})" } } impl rustc_serialize::Encodable for AttrId { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { - s.emit_unit() + fn encode(&self, _: &mut S) -> Result<(), S::Error> { + Ok(()) } } impl rustc_serialize::Decodable for AttrId { - fn decode(d: &mut D) -> Result { - d.read_nil().map(|_| crate::attr::mk_attr_id()) + fn decode(_: &mut D) -> Result { + Ok(crate::attr::mk_attr_id()) } } diff --git a/src/librustc_ast/attr/mod.rs b/src/librustc_ast/attr/mod.rs index 249311851fb1b..d53d776778598 100644 --- a/src/librustc_ast/attr/mod.rs +++ b/src/librustc_ast/attr/mod.rs @@ -366,14 +366,14 @@ pub fn mk_nested_word_item(ident: Ident) -> NestedMetaItem { } crate fn mk_attr_id() -> AttrId { - use std::sync::atomic::AtomicUsize; + use std::sync::atomic::AtomicU32; use std::sync::atomic::Ordering; - static NEXT_ATTR_ID: AtomicUsize = AtomicUsize::new(0); + static NEXT_ATTR_ID: AtomicU32 = AtomicU32::new(0); let id = NEXT_ATTR_ID.fetch_add(1, Ordering::SeqCst); - assert!(id != ::std::usize::MAX); - AttrId(id) + assert!(id != u32::MAX); + AttrId::from_u32(id) } pub fn mk_attr(style: AttrStyle, path: Path, args: MacArgs, span: Span) -> Attribute { From 1d004bddee33fcae51611b0a83569052692ce15b Mon Sep 17 00:00:00 2001 From: Marti Raudsepp Date: Sat, 21 Mar 2020 12:33:52 +0200 Subject: [PATCH 03/11] Fix deprecated Error.description() usage in docs --- src/libstd/error.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/error.rs b/src/libstd/error.rs index 2a370f192964f..b394f2efc2e35 100644 --- a/src/libstd/error.rs +++ b/src/libstd/error.rs @@ -88,7 +88,7 @@ pub trait Error: Debug + Display { /// fn main() { /// match get_super_error() { /// Err(e) => { - /// println!("Error: {}", e.description()); + /// println!("Error: {}", e); /// println!("Caused by: {}", e.source().unwrap()); /// } /// _ => println!("No error"), From 2fa550b85d6d3faed9ab7fbf442ac3b11b380184 Mon Sep 17 00:00:00 2001 From: Wesley Wiser Date: Wed, 18 Mar 2020 22:15:23 -0400 Subject: [PATCH 04/11] Fix ICE caused by truncating a negative ZST enum discriminant --- src/librustc_codegen_ssa/mir/rvalue.rs | 7 ++- src/librustc_mir/interpret/cast.rs | 4 +- .../ui/consts/cast-discriminant-zst-enum.rs | 47 +++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 src/test/ui/consts/cast-discriminant-zst-enum.rs diff --git a/src/librustc_codegen_ssa/mir/rvalue.rs b/src/librustc_codegen_ssa/mir/rvalue.rs index 6d0046063989b..245df0846b583 100644 --- a/src/librustc_codegen_ssa/mir/rvalue.rs +++ b/src/librustc_codegen_ssa/mir/rvalue.rs @@ -293,7 +293,12 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { if let Some(discr) = operand.layout.ty.discriminant_for_variant(bx.tcx(), index) { - let discr_val = bx.cx().const_uint_big(ll_t_out, discr.val); + let discr_layout = bx.cx().layout_of(discr.ty); + let discr_t = bx.cx().immediate_backend_type(discr_layout); + let discr_val = bx.cx().const_uint_big(discr_t, discr.val); + let discr_val = + bx.intcast(discr_val, ll_t_out, discr.ty.is_signed()); + return ( bx, OperandRef { diff --git a/src/librustc_mir/interpret/cast.rs b/src/librustc_mir/interpret/cast.rs index 3cbb0667ff392..df63127f368b8 100644 --- a/src/librustc_mir/interpret/cast.rs +++ b/src/librustc_mir/interpret/cast.rs @@ -3,6 +3,7 @@ use rustc::ty::layout::{self, Size, TyLayout}; use rustc::ty::{self, Ty, TypeAndMut, TypeFoldable}; use rustc_ast::ast::FloatTy; use rustc_span::symbol::sym; +use rustc_target::abi::LayoutOf; use rustc::mir::interpret::{InterpResult, PointerArithmetic, Scalar}; use rustc::mir::CastKind; @@ -134,7 +135,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { layout::Variants::Single { index } => { if let Some(discr) = src.layout.ty.discriminant_for_variant(*self.tcx, index) { assert!(src.layout.is_zst()); - return Ok(Scalar::from_uint(discr.val, dest_layout.size).into()); + let discr_layout = self.layout_of(discr.ty)?; + return Ok(self.cast_from_int(discr.val, discr_layout, dest_layout)?.into()); } } layout::Variants::Multiple { .. } => {} diff --git a/src/test/ui/consts/cast-discriminant-zst-enum.rs b/src/test/ui/consts/cast-discriminant-zst-enum.rs new file mode 100644 index 0000000000000..a77258120111e --- /dev/null +++ b/src/test/ui/consts/cast-discriminant-zst-enum.rs @@ -0,0 +1,47 @@ +// run-pass +// Test a ZST enum whose dicriminant is ~0i128. This caused an ICE when casting to a i32. + +#[derive(Copy, Clone)] +enum Nums { + NegOne = -1, +} + +const NEG_ONE_I8: i8 = Nums::NegOne as i8; +const NEG_ONE_I16: i16 = Nums::NegOne as i16; +const NEG_ONE_I32: i32 = Nums::NegOne as i32; +const NEG_ONE_I64: i64 = Nums::NegOne as i64; +const NEG_ONE_I128: i128 = Nums::NegOne as i128; + +#[inline(never)] +fn identity(t: T) -> T { t } + +fn test_as_arg(n: Nums) { + assert_eq!(-1i8, n as i8); + assert_eq!(-1i16, n as i16); + assert_eq!(-1i32, n as i32); + assert_eq!(-1i64, n as i64); + assert_eq!(-1i128, n as i128); +} + +fn main() { + let kind = Nums::NegOne; + assert_eq!(-1i8, kind as i8); + assert_eq!(-1i16, kind as i16); + assert_eq!(-1i32, kind as i32); + assert_eq!(-1i64, kind as i64); + assert_eq!(-1i128, kind as i128); + + assert_eq!(-1i8, identity(kind) as i8); + assert_eq!(-1i16, identity(kind) as i16); + assert_eq!(-1i32, identity(kind) as i32); + assert_eq!(-1i64, identity(kind) as i64); + assert_eq!(-1i128, identity(kind) as i128); + + test_as_arg(Nums::NegOne); + + assert_eq!(-1i8, NEG_ONE_I8); + assert_eq!(-1i16, NEG_ONE_I16); + assert_eq!(-1i32, NEG_ONE_I32); + assert_eq!(-1i64, NEG_ONE_I64); + assert_eq!(-1i128, NEG_ONE_I128); +} From 96bbd1ccb6c210318d5ba90e7b68430b643e470a Mon Sep 17 00:00:00 2001 From: Wesley Wiser Date: Fri, 20 Mar 2020 19:57:29 -0400 Subject: [PATCH 05/11] Rename `cast_from_int` to `cast_from_int_like` --- src/librustc_mir/interpret/cast.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/librustc_mir/interpret/cast.rs b/src/librustc_mir/interpret/cast.rs index df63127f368b8..5c70b28a56786 100644 --- a/src/librustc_mir/interpret/cast.rs +++ b/src/librustc_mir/interpret/cast.rs @@ -136,7 +136,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { if let Some(discr) = src.layout.ty.discriminant_for_variant(*self.tcx, index) { assert!(src.layout.is_zst()); let discr_layout = self.layout_of(discr.ty)?; - return Ok(self.cast_from_int(discr.val, discr_layout, dest_layout)?.into()); + return Ok(self + .cast_from_int_like(discr.val, discr_layout, dest_layout)? + .into()); } } layout::Variants::Multiple { .. } => {} @@ -173,10 +175,10 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { // (b) cast from an integer-like (including bool, char, enums). // In both cases we want the bits. let bits = self.force_bits(src.to_scalar()?, src.layout.size)?; - Ok(self.cast_from_int(bits, src.layout, dest_layout)?.into()) + Ok(self.cast_from_int_like(bits, src.layout, dest_layout)?.into()) } - fn cast_from_int( + fn cast_from_int_like( &self, v: u128, // raw bits src_layout: TyLayout<'tcx>, From 14e0aad9909d35195c5725962a4c97697235759d Mon Sep 17 00:00:00 2001 From: Eduard-Mihai Burtescu Date: Sat, 14 Mar 2020 18:48:10 +0200 Subject: [PATCH 06/11] symbol_names: treat ReifyShim like VtableShim. --- src/librustc/ty/instance.rs | 4 ---- src/librustc_symbol_mangling/legacy.rs | 9 +++++++-- src/librustc_symbol_mangling/v0.rs | 13 +++++++++++-- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/librustc/ty/instance.rs b/src/librustc/ty/instance.rs index 13d58ea73ac6b..46372701a9254 100644 --- a/src/librustc/ty/instance.rs +++ b/src/librustc/ty/instance.rs @@ -406,10 +406,6 @@ impl<'tcx> Instance<'tcx> { | InstanceDef::VtableShim(..) => Some(self.substs), } } - - pub fn is_vtable_shim(&self) -> bool { - if let InstanceDef::VtableShim(..) = self.def { true } else { false } - } } fn needs_fn_once_adapter_shim( diff --git a/src/librustc_symbol_mangling/legacy.rs b/src/librustc_symbol_mangling/legacy.rs index 0dedda9bb6b73..7b082309f34b5 100644 --- a/src/librustc_symbol_mangling/legacy.rs +++ b/src/librustc_symbol_mangling/legacy.rs @@ -59,10 +59,14 @@ pub(super) fn mangle( .print_def_path(def_id, &[]) .unwrap(); - if instance.is_vtable_shim() { + if let ty::InstanceDef::VtableShim(..) = instance.def { let _ = printer.write_str("{{vtable-shim}}"); } + if let ty::InstanceDef::ReifyShim(..) = instance.def { + let _ = printer.write_str("{{reify-shim}}"); + } + printer.path.finish(hash) } @@ -123,7 +127,8 @@ fn get_symbol_hash<'tcx>( } // We want to avoid accidental collision between different types of instances. - // Especially, VtableShim may overlap with its original instance without this. + // Especially, `VtableShim`s and `ReifyShim`s may overlap with their original + // instances without this. discriminant(&instance.def).hash_stable(&mut hcx, &mut hasher); }); diff --git a/src/librustc_symbol_mangling/v0.rs b/src/librustc_symbol_mangling/v0.rs index ce6d0d9dc5ba8..e22a49061bb19 100644 --- a/src/librustc_symbol_mangling/v0.rs +++ b/src/librustc_symbol_mangling/v0.rs @@ -34,8 +34,17 @@ pub(super) fn mangle( binders: vec![], out: String::from(prefix), }; - cx = if instance.is_vtable_shim() { - cx.path_append_ns(|cx| cx.print_def_path(def_id, substs), 'S', 0, "").unwrap() + + // Append `::{shim:...#0}` to shims that can coexist with a non-shim instance. + let shim_kind = match instance.def { + ty::InstanceDef::VtableShim(_) => Some("vtable"), + ty::InstanceDef::ReifyShim(_) => Some("reify"), + + _ => None, + }; + + cx = if let Some(shim_kind) = shim_kind { + cx.path_append_ns(|cx| cx.print_def_path(def_id, substs), 'S', 0, shim_kind).unwrap() } else { cx.print_def_path(def_id, substs).unwrap() }; From a39e96407a579abbf4c273dc41692b69717b2aa8 Mon Sep 17 00:00:00 2001 From: lzutao Date: Sat, 21 Mar 2020 21:54:01 +0700 Subject: [PATCH 07/11] Remove CARGO_BUILD_TARGET from bootstrap.py --- src/bootstrap/bootstrap.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/bootstrap/bootstrap.py b/src/bootstrap/bootstrap.py index 50e1726240fff..d5efed61b541e 100644 --- a/src/bootstrap/bootstrap.py +++ b/src/bootstrap/bootstrap.py @@ -664,6 +664,10 @@ def build_bootstrap(self): if self.clean and os.path.exists(build_dir): shutil.rmtree(build_dir) env = os.environ.copy() + # `CARGO_BUILD_TARGET` breaks bootstrap build. + # See also: . + if "CARGO_BUILD_TARGET" in env: + del env["CARGO_BUILD_TARGET"] env["RUSTC_BOOTSTRAP"] = '1' env["CARGO_TARGET_DIR"] = build_dir env["RUSTC"] = self.rustc() From 97aa2704ec109002b513b99e925c891bc8461217 Mon Sep 17 00:00:00 2001 From: Nixon Enraght-Moony Date: Sat, 21 Mar 2020 15:43:12 +0000 Subject: [PATCH 08/11] Add explanation message for E0224 --- src/librustc_error_codes/error_codes.rs | 2 +- src/librustc_error_codes/error_codes/E0224.md | 15 +++++++++++++++ src/test/ui/parser/issue-68890-2.stderr | 1 + .../macro/trait-object-macro-matcher.stderr | 1 + .../trait-alias-only-maybe-bound.stderr | 1 + .../ui/traits/trait-object-macro-matcher.stderr | 3 ++- .../ui/traits/trait-object-vs-lifetime-2.stderr | 1 + .../ui/traits/trait-object-vs-lifetime.stderr | 2 +- .../wf-trait-object-only-maybe-bound.stderr | 1 + 9 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 src/librustc_error_codes/error_codes/E0224.md diff --git a/src/librustc_error_codes/error_codes.rs b/src/librustc_error_codes/error_codes.rs index 85339ab9aa15b..33bfaddc39c9d 100644 --- a/src/librustc_error_codes/error_codes.rs +++ b/src/librustc_error_codes/error_codes.rs @@ -118,6 +118,7 @@ E0220: include_str!("./error_codes/E0220.md"), E0221: include_str!("./error_codes/E0221.md"), E0222: include_str!("./error_codes/E0222.md"), E0223: include_str!("./error_codes/E0223.md"), +E0224: include_str!("./error_codes/E0224.md"), E0225: include_str!("./error_codes/E0225.md"), E0229: include_str!("./error_codes/E0229.md"), E0230: include_str!("./error_codes/E0230.md"), @@ -469,7 +470,6 @@ E0748: include_str!("./error_codes/E0748.md"), // E0217, // ambiguous associated type, defined in multiple supertraits // E0218, // no associated type defined // E0219, // associated type defined in higher-ranked supertrait - E0224, // at least one non-builtin train is required for an object type E0226, // only a single explicit lifetime bound is permitted E0227, // ambiguous lifetime bound, explicit lifetime bound required E0228, // explicit lifetime bound required diff --git a/src/librustc_error_codes/error_codes/E0224.md b/src/librustc_error_codes/error_codes/E0224.md new file mode 100644 index 0000000000000..fd89c1d52560f --- /dev/null +++ b/src/librustc_error_codes/error_codes/E0224.md @@ -0,0 +1,15 @@ +A trait object was declaired with no traits. + +Erroneous code example: + +```compile_fail,E0224 +type Foo = dyn 'static +; +``` + +Rust does not currently support this. + +To solve ensure the the trait object has at least one trait: + +``` +type Foo = dyn 'static + Copy; +``` diff --git a/src/test/ui/parser/issue-68890-2.stderr b/src/test/ui/parser/issue-68890-2.stderr index d475c79cb27b4..967eee06e282f 100644 --- a/src/test/ui/parser/issue-68890-2.stderr +++ b/src/test/ui/parser/issue-68890-2.stderr @@ -20,3 +20,4 @@ LL | type X<'a> = (?'a) +; error: aborting due to 2 previous errors +For more information about this error, try `rustc --explain E0224`. diff --git a/src/test/ui/parser/macro/trait-object-macro-matcher.stderr b/src/test/ui/parser/macro/trait-object-macro-matcher.stderr index 230733371ddd8..a6fa9f8dddc27 100644 --- a/src/test/ui/parser/macro/trait-object-macro-matcher.stderr +++ b/src/test/ui/parser/macro/trait-object-macro-matcher.stderr @@ -20,3 +20,4 @@ LL | m!('static); error: aborting due to 2 previous errors +For more information about this error, try `rustc --explain E0224`. diff --git a/src/test/ui/traits/trait-alias/trait-alias-only-maybe-bound.stderr b/src/test/ui/traits/trait-alias/trait-alias-only-maybe-bound.stderr index 6de79fa917b16..594115d980c8b 100644 --- a/src/test/ui/traits/trait-alias/trait-alias-only-maybe-bound.stderr +++ b/src/test/ui/traits/trait-alias/trait-alias-only-maybe-bound.stderr @@ -12,3 +12,4 @@ LL | type _T1 = dyn _2; error: aborting due to 2 previous errors +For more information about this error, try `rustc --explain E0224`. diff --git a/src/test/ui/traits/trait-object-macro-matcher.stderr b/src/test/ui/traits/trait-object-macro-matcher.stderr index a8511f63c16a5..cb48bd1258ea2 100644 --- a/src/test/ui/traits/trait-object-macro-matcher.stderr +++ b/src/test/ui/traits/trait-object-macro-matcher.stderr @@ -14,4 +14,5 @@ LL | m!(dyn Copy + Send + 'static); error: aborting due to 2 previous errors -For more information about this error, try `rustc --explain E0038`. +Some errors have detailed explanations: E0038, E0224. +For more information about an error, try `rustc --explain E0038`. diff --git a/src/test/ui/traits/trait-object-vs-lifetime-2.stderr b/src/test/ui/traits/trait-object-vs-lifetime-2.stderr index 014d380b63ca5..28b8e11f1330c 100644 --- a/src/test/ui/traits/trait-object-vs-lifetime-2.stderr +++ b/src/test/ui/traits/trait-object-vs-lifetime-2.stderr @@ -6,3 +6,4 @@ LL | dyn 'static +: 'static + Copy, error: aborting due to previous error +For more information about this error, try `rustc --explain E0224`. diff --git a/src/test/ui/traits/trait-object-vs-lifetime.stderr b/src/test/ui/traits/trait-object-vs-lifetime.stderr index 04529fb8cfab3..ff3fc2a197c66 100644 --- a/src/test/ui/traits/trait-object-vs-lifetime.stderr +++ b/src/test/ui/traits/trait-object-vs-lifetime.stderr @@ -32,5 +32,5 @@ LL | let _: S; error: aborting due to 5 previous errors -Some errors have detailed explanations: E0107, E0747. +Some errors have detailed explanations: E0107, E0224, E0747. For more information about an error, try `rustc --explain E0107`. diff --git a/src/test/ui/traits/wf-trait-object-only-maybe-bound.stderr b/src/test/ui/traits/wf-trait-object-only-maybe-bound.stderr index 8cc97addc7dd4..482410886329e 100644 --- a/src/test/ui/traits/wf-trait-object-only-maybe-bound.stderr +++ b/src/test/ui/traits/wf-trait-object-only-maybe-bound.stderr @@ -12,3 +12,4 @@ LL | type _0 = dyn ?Sized; error: aborting due to 2 previous errors +For more information about this error, try `rustc --explain E0224`. From 0b307f7a9f00878325f8c2c2573ea49e7a9c4353 Mon Sep 17 00:00:00 2001 From: adrian5 Date: Sat, 21 Mar 2020 17:06:10 +0100 Subject: [PATCH 09/11] Tweak wording for std::io::Read::read function --- src/libstd/io/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index dc831432c176f..83c492fecf974 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -502,7 +502,7 @@ pub trait Read { /// how many bytes were read. /// /// This function does not provide any guarantees about whether it blocks - /// waiting for data, but if an object needs to block for a read but cannot + /// waiting for data, but if an object needs to block for a read and cannot, /// it will typically signal this via an [`Err`] return value. /// /// If the return value of this method is [`Ok(n)`], then it must be From e391d776e3869f56ca1467a4c7e3469080c5378d Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sat, 21 Mar 2020 21:40:58 +0300 Subject: [PATCH 10/11] Add a test for out-of-line module passed through a proc macro --- src/test/ui/proc-macro/out-of-line-mod.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/test/ui/proc-macro/out-of-line-mod.rs diff --git a/src/test/ui/proc-macro/out-of-line-mod.rs b/src/test/ui/proc-macro/out-of-line-mod.rs new file mode 100644 index 0000000000000..658ed6c18e05a --- /dev/null +++ b/src/test/ui/proc-macro/out-of-line-mod.rs @@ -0,0 +1,13 @@ +// Out-of-line module is found on the filesystem if passed through a proc macro (issue #58818). + +// check-pass +// aux-build:test-macros.rs + +#[macro_use] +extern crate test_macros; + +mod outer { + identity! { mod inner; } +} + +fn main() {} From 0aa15d048573d4fd0453e1da301857d7ca1faf7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20K=C3=A5re=20Alsaker?= Date: Mon, 16 Mar 2020 19:17:40 +0100 Subject: [PATCH 11/11] Allow `hir().find` to return `None` --- src/librustc/hir/map/mod.rs | 32 ++++++++++++++++----------- src/librustc/hir/mod.rs | 7 +++--- src/librustc/query/mod.rs | 4 ++-- src/test/ui/issues/issue-70041.rs | 13 +++++++++++ src/test/ui/issues/issue-70041.stderr | 19 ++++++++++++++++ 5 files changed, 56 insertions(+), 19 deletions(-) create mode 100644 src/test/ui/issues/issue-70041.rs create mode 100644 src/test/ui/issues/issue-70041.stderr diff --git a/src/librustc/hir/map/mod.rs b/src/librustc/hir/map/mod.rs index 13257e7bf4770..49b7ce3445ba8 100644 --- a/src/librustc/hir/map/mod.rs +++ b/src/librustc/hir/map/mod.rs @@ -337,23 +337,28 @@ impl<'hir> Map<'hir> { } fn find_entry(&self, id: HirId) -> Option> { - Some(self.get_entry(id)) - } - - fn get_entry(&self, id: HirId) -> Entry<'hir> { if id.local_id == ItemLocalId::from_u32(0) { let owner = self.tcx.hir_owner(id.owner); - Entry { parent: owner.parent, node: owner.node } + owner.map(|owner| Entry { parent: owner.parent, node: owner.node }) } else { let owner = self.tcx.hir_owner_nodes(id.owner); - let node = owner.nodes[id.local_id].as_ref().unwrap(); - // FIXME(eddyb) use a single generic type insted of having both - // `Entry` and `ParentedNode`, which are effectively the same. - // Alternatively, rewrite code using `Entry` to use `ParentedNode`. - Entry { parent: HirId { owner: id.owner, local_id: node.parent }, node: node.node } + owner.and_then(|owner| { + let node = owner.nodes[id.local_id].as_ref(); + // FIXME(eddyb) use a single generic type insted of having both + // `Entry` and `ParentedNode`, which are effectively the same. + // Alternatively, rewrite code using `Entry` to use `ParentedNode`. + node.map(|node| Entry { + parent: HirId { owner: id.owner, local_id: node.parent }, + node: node.node, + }) + }) } } + fn get_entry(&self, id: HirId) -> Entry<'hir> { + self.find_entry(id).unwrap() + } + pub fn item(&self, id: HirId) -> &'hir Item<'hir> { match self.find(id).unwrap() { Node::Item(item) => item, @@ -376,7 +381,7 @@ impl<'hir> Map<'hir> { } pub fn body(&self, id: BodyId) -> &'hir Body<'hir> { - self.tcx.hir_owner_nodes(id.hir_id.owner).bodies.get(&id.hir_id.local_id).unwrap() + self.tcx.hir_owner_nodes(id.hir_id.owner).unwrap().bodies.get(&id.hir_id.local_id).unwrap() } pub fn fn_decl_by_hir_id(&self, hir_id: HirId) -> Option<&'hir FnDecl<'hir>> { @@ -536,8 +541,9 @@ impl<'hir> Map<'hir> { /// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found. pub fn find(&self, hir_id: HirId) -> Option> { - let node = self.get_entry(hir_id).node; - if let Node::Crate(..) = node { None } else { Some(node) } + self.find_entry(hir_id).and_then(|entry| { + if let Node::Crate(..) = entry.node { None } else { Some(entry.node) } + }) } /// Similar to `get_parent`; returns the parent HIR Id, or just `hir_id` if there diff --git a/src/librustc/hir/mod.rs b/src/librustc/hir/mod.rs index d9dfd2961ff17..ce8e1f48daa77 100644 --- a/src/librustc/hir/mod.rs +++ b/src/librustc/hir/mod.rs @@ -78,9 +78,8 @@ pub fn provide(providers: &mut Providers<'_>) { let module = hir.as_local_hir_id(id.to_def_id()).unwrap(); &tcx.untracked_crate.modules[&module] }; - providers.hir_owner = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].signature.unwrap(); - providers.hir_owner_nodes = |tcx, id| { - tcx.index_hir(LOCAL_CRATE).map[id].with_bodies.as_ref().map(|nodes| &**nodes).unwrap() - }; + providers.hir_owner = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].signature; + providers.hir_owner_nodes = + |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].with_bodies.as_ref().map(|nodes| &**nodes); map::provide(providers); } diff --git a/src/librustc/query/mod.rs b/src/librustc/query/mod.rs index 00e40faa95c30..54f5103f736ec 100644 --- a/src/librustc/query/mod.rs +++ b/src/librustc/query/mod.rs @@ -76,7 +76,7 @@ rustc_queries! { // // This can be conveniently accessed by methods on `tcx.hir()`. // Avoid calling this query directly. - query hir_owner(key: LocalDefId) -> &'tcx crate::hir::Owner<'tcx> { + query hir_owner(key: LocalDefId) -> Option<&'tcx crate::hir::Owner<'tcx>> { eval_always desc { |tcx| "HIR owner of `{}`", tcx.def_path_str(key.to_def_id()) } } @@ -85,7 +85,7 @@ rustc_queries! { // // This can be conveniently accessed by methods on `tcx.hir()`. // Avoid calling this query directly. - query hir_owner_nodes(key: LocalDefId) -> &'tcx crate::hir::OwnerNodes<'tcx> { + query hir_owner_nodes(key: LocalDefId) -> Option<&'tcx crate::hir::OwnerNodes<'tcx>> { eval_always desc { |tcx| "HIR owner items in `{}`", tcx.def_path_str(key.to_def_id()) } } diff --git a/src/test/ui/issues/issue-70041.rs b/src/test/ui/issues/issue-70041.rs new file mode 100644 index 0000000000000..22e42295eedf3 --- /dev/null +++ b/src/test/ui/issues/issue-70041.rs @@ -0,0 +1,13 @@ +// compile-flags: --edition=2018 +// run-pass + +macro_rules! regex { + //~^ WARN unused macro definition + () => {}; +} + +#[allow(dead_code)] +use regex; +//~^ WARN unused import + +fn main() {} diff --git a/src/test/ui/issues/issue-70041.stderr b/src/test/ui/issues/issue-70041.stderr new file mode 100644 index 0000000000000..b180175c5ab76 --- /dev/null +++ b/src/test/ui/issues/issue-70041.stderr @@ -0,0 +1,19 @@ +warning: unused macro definition + --> $DIR/issue-70041.rs:4:1 + | +LL | / macro_rules! regex { +LL | | +LL | | () => {}; +LL | | } + | |_^ + | + = note: `#[warn(unused_macros)]` on by default + +warning: unused import: `regex` + --> $DIR/issue-70041.rs:10:5 + | +LL | use regex; + | ^^^^^ + | + = note: `#[warn(unused_imports)]` on by default +