From 261a91519d57590283b250ea9dc1d924b01d4dd6 Mon Sep 17 00:00:00 2001 From: Yuki OKUSHI Date: Fri, 29 Mar 2019 06:44:31 +0900 Subject: [PATCH 01/30] Use platform dependent mcount function --- src/librustc_codegen_llvm/attributes.rs | 30 ++++++++++++++++++++++++- src/test/codegen/instrument-mcount.rs | 7 ++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/test/codegen/instrument-mcount.rs diff --git a/src/librustc_codegen_llvm/attributes.rs b/src/librustc_codegen_llvm/attributes.rs index 71e7535313f7..e765b986d623 100644 --- a/src/librustc_codegen_llvm/attributes.rs +++ b/src/librustc_codegen_llvm/attributes.rs @@ -77,9 +77,37 @@ pub fn set_instrument_function(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) { if cx.sess().instrument_mcount() { // Similar to `clang -pg` behavior. Handled by the // `post-inline-ee-instrument` LLVM pass. + + // The function name varies on platforms. + // See test/CodeGen/mcount.c in clang. + let mcount_name = if cfg!(target_os = "netbsd") { + const_cstr!("__mcount") + } else if cfg!(any( + target_arch = "mips", target_arch = "mips64", + target_arch = "powerpc", target_arch = "powerpc64")) { + const_cstr!("_mcount") + } else if cfg!(target_os = "darwin") { + const_cstr!("\01mcount") + } else if cfg!(target_arch = "aarch64") + && (cfg!(target_os = "linux") + || (cfg!(target_os = "unknown") && cfg!(target_env = "gnu"))) + { + const_cstr!("\01_mcount") + } else if cfg!(target_arch = "arm") + && cfg!(any(target_os = "linux", target_os = "unknown")) + { + if cfg!(target_env = "gnu") { + const_cstr!("\01__gnu_mcount_nc") + } else { + const_cstr!("\01mcount") + } + } else { + const_cstr!("mcount") + }; + llvm::AddFunctionAttrStringValue( llfn, llvm::AttributePlace::Function, - const_cstr!("instrument-function-entry-inlined"), const_cstr!("mcount")); + const_cstr!("instrument-function-entry-inlined"), mcount_name); } } diff --git a/src/test/codegen/instrument-mcount.rs b/src/test/codegen/instrument-mcount.rs new file mode 100644 index 000000000000..bd01556d986a --- /dev/null +++ b/src/test/codegen/instrument-mcount.rs @@ -0,0 +1,7 @@ +// ignore-tidy-linelength +// compile-flags: -Z instrument-mcount + +#![crate_type = "lib"] + +// CHECK: attributes #{{.*}} "instrument-function-entry-inlined"="{{_*}}mcount" "no-frame-pointer-elim"="true" +pub fn foo() {} From 5b7f4e9e2106f5e83fc650e8185d59a57e27ad09 Mon Sep 17 00:00:00 2001 From: Eduard-Mihai Burtescu Date: Fri, 29 Mar 2019 07:44:54 +0200 Subject: [PATCH 02/30] rustc_target: factor out common fields of non-Single Variants. --- src/librustc/ty/layout.rs | 57 +++++++----- .../debuginfo/metadata.rs | 68 ++++++++++----- src/librustc_codegen_llvm/type_of.rs | 8 +- src/librustc_codegen_ssa/mir/place.rs | 36 ++++---- src/librustc_codegen_ssa/mir/rvalue.rs | 3 +- src/librustc_lint/types.rs | 87 ++++++++++--------- src/librustc_mir/interpret/cast.rs | 3 +- src/librustc_mir/interpret/operand.rs | 16 ++-- src/librustc_mir/interpret/place.rs | 18 ++-- src/librustc_mir/interpret/visitor.rs | 3 +- src/librustc_target/abi/call/x86_64.rs | 3 +- src/librustc_target/abi/mod.rs | 25 +++--- 12 files changed, 193 insertions(+), 134 deletions(-) diff --git a/src/librustc/ty/layout.rs b/src/librustc/ty/layout.rs index 7d2b21b9aecd..e01b50113b9f 100644 --- a/src/librustc/ty/layout.rs +++ b/src/librustc/ty/layout.rs @@ -913,11 +913,13 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> { } return Ok(tcx.intern_layout(LayoutDetails { - variants: Variants::NicheFilling { - dataful_variant: i, - niche_variants, - niche: niche_scalar, - niche_start, + variants: Variants::Multiple { + discr: niche_scalar, + discr_kind: DiscriminantKind::Niche { + dataful_variant: i, + niche_variants, + niche_start, + }, variants: st, }, fields: FieldPlacement::Arbitrary { @@ -1137,8 +1139,9 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> { } tcx.intern_layout(LayoutDetails { - variants: Variants::Tagged { - tag, + variants: Variants::Multiple { + discr: tag, + discr_kind: DiscriminantKind::Tag, variants: layout_variants, }, fields: FieldPlacement::Arbitrary { @@ -1293,8 +1296,7 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> { } } - Variants::NicheFilling { .. } | - Variants::Tagged { .. } => { + Variants::Multiple { ref discr, ref discr_kind, .. } => { debug!("print-type-size `{:#?}` adt general variants def {}", layout.ty, adt_def.variants.len()); let variant_infos: Vec<_> = @@ -1306,8 +1308,8 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> { layout.for_variant(self, i)) }) .collect(); - record(adt_kind.into(), adt_packed, match layout.variants { - Variants::Tagged { ref tag, .. } => Some(tag.value.size(self)), + record(adt_kind.into(), adt_packed, match discr_kind { + DiscriminantKind::Tag => Some(discr.value.size(self)), _ => None }, variant_infos); } @@ -1627,8 +1629,7 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx> }) } - Variants::NicheFilling { ref variants, .. } | - Variants::Tagged { ref variants, .. } => { + Variants::Multiple { ref variants, .. } => { &variants[variant_index] } }; @@ -1735,8 +1736,7 @@ impl<'a, 'tcx, C> TyLayoutMethods<'tcx, C> for Ty<'tcx> } // Discriminant field for enums (where applicable). - Variants::Tagged { tag: ref discr, .. } | - Variants::NicheFilling { niche: ref discr, .. } => { + Variants::Multiple { ref discr, .. } => { assert_eq!(i, 0); let layout = LayoutDetails::scalar(cx, discr.clone()); return MaybeResult::from_ok(TyLayout { @@ -1881,26 +1881,37 @@ impl<'a> HashStable> for Variants { Single { index } => { index.hash_stable(hcx, hasher); } - Tagged { - ref tag, + Multiple { + ref discr, + ref discr_kind, ref variants, } => { - tag.hash_stable(hcx, hasher); + discr.hash_stable(hcx, hasher); + discr_kind.hash_stable(hcx, hasher); variants.hash_stable(hcx, hasher); } - NicheFilling { + } + } +} + +impl<'a> HashStable> for DiscriminantKind { + fn hash_stable(&self, + hcx: &mut StableHashingContext<'a>, + hasher: &mut StableHasher) { + use crate::ty::layout::DiscriminantKind::*; + mem::discriminant(self).hash_stable(hcx, hasher); + + match *self { + Tag => {} + Niche { dataful_variant, ref niche_variants, - ref niche, niche_start, - ref variants, } => { dataful_variant.hash_stable(hcx, hasher); niche_variants.start().hash_stable(hcx, hasher); niche_variants.end().hash_stable(hcx, hasher); - niche.hash_stable(hcx, hasher); niche_start.hash_stable(hcx, hasher); - variants.hash_stable(hcx, hasher); } } } diff --git a/src/librustc_codegen_llvm/debuginfo/metadata.rs b/src/librustc_codegen_llvm/debuginfo/metadata.rs index 042a8c60cfaa..d2bbc00868a4 100644 --- a/src/librustc_codegen_llvm/debuginfo/metadata.rs +++ b/src/librustc_codegen_llvm/debuginfo/metadata.rs @@ -1235,7 +1235,11 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> { } ] } - layout::Variants::Tagged { ref variants, .. } => { + layout::Variants::Multiple { + discr_kind: layout::DiscriminantKind::Tag, + ref variants, + .. + } => { let discriminant_info = if fallback { RegularDiscriminant(self.discriminant_type_metadata .expect("")) @@ -1277,12 +1281,14 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> { } }).collect() } - layout::Variants::NicheFilling { - ref niche_variants, - niche_start, + layout::Variants::Multiple { + discr_kind: layout::DiscriminantKind::Niche { + ref niche_variants, + niche_start, + dataful_variant, + }, + ref discr, ref variants, - dataful_variant, - ref niche, } => { if fallback { let variant = self.layout.for_variant(cx, dataful_variant); @@ -1369,7 +1375,11 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> { let value = (i.as_u32() as u128) .wrapping_sub(niche_variants.start().as_u32() as u128) .wrapping_add(niche_start); - let value = truncate(value, niche.value.size(cx)); + let value = truncate(value, discr.value.size(cx)); + // NOTE(eddyb) do *NOT* remove this assert, until + // we pass the full 128-bit value to LLVM, otherwise + // truncation will be silent and remain undetected. + assert_eq!(value as u64 as u128, value); Some(value as u64) }; @@ -1586,8 +1596,11 @@ fn prepare_enum_metadata( let layout = cx.layout_of(enum_type); match (&layout.abi, &layout.variants) { - (&layout::Abi::Scalar(_), &layout::Variants::Tagged {ref tag, .. }) => - return FinalMetadata(discriminant_type_metadata(tag.value)), + (&layout::Abi::Scalar(_), &layout::Variants::Multiple { + discr_kind: layout::DiscriminantKind::Tag, + ref discr, + .. + }) => return FinalMetadata(discriminant_type_metadata(discr.value)), _ => {} } @@ -1599,9 +1612,16 @@ fn prepare_enum_metadata( if use_enum_fallback(cx) { let discriminant_type_metadata = match layout.variants { layout::Variants::Single { .. } | - layout::Variants::NicheFilling { .. } => None, - layout::Variants::Tagged { ref tag, .. } => { - Some(discriminant_type_metadata(tag.value)) + layout::Variants::Multiple { + discr_kind: layout::DiscriminantKind::Niche { .. }, + .. + } => None, + layout::Variants::Multiple { + discr_kind: layout::DiscriminantKind::Tag, + ref discr, + .. + } => { + Some(discriminant_type_metadata(discr.value)) } }; @@ -1636,16 +1656,20 @@ fn prepare_enum_metadata( ); } - let discriminator_metadata = match &layout.variants { + let discriminator_metadata = match layout.variants { // A single-variant enum has no discriminant. - &layout::Variants::Single { .. } => None, + layout::Variants::Single { .. } => None, - &layout::Variants::NicheFilling { ref niche, .. } => { + layout::Variants::Multiple { + discr_kind: layout::DiscriminantKind::Niche { .. }, + ref discr, + .. + } => { // Find the integer type of the correct size. - let size = niche.value.size(cx); - let align = niche.value.align(cx); + let size = discr.value.size(cx); + let align = discr.value.align(cx); - let discr_type = match niche.value { + let discr_type = match discr.value { layout::Int(t, _) => t, layout::Float(layout::FloatTy::F32) => Integer::I32, layout::Float(layout::FloatTy::F64) => Integer::I64, @@ -1668,8 +1692,12 @@ fn prepare_enum_metadata( } }, - &layout::Variants::Tagged { ref tag, .. } => { - let discr_type = tag.value.to_ty(cx.tcx); + layout::Variants::Multiple { + discr_kind: layout::DiscriminantKind::Tag, + ref discr, + .. + } => { + let discr_type = discr.value.to_ty(cx.tcx); let (size, align) = cx.size_and_align_of(discr_type); let discr_metadata = basic_type_metadata(cx, discr_type); diff --git a/src/librustc_codegen_llvm/type_of.rs b/src/librustc_codegen_llvm/type_of.rs index fb5624d56078..020447608eeb 100644 --- a/src/librustc_codegen_llvm/type_of.rs +++ b/src/librustc_codegen_llvm/type_of.rs @@ -452,7 +452,13 @@ impl<'tcx> LayoutLlvmExt<'tcx> for TyLayout<'tcx> { _ => { let mut data_variant = match self.variants { - layout::Variants::NicheFilling { dataful_variant, .. } => { + layout::Variants::Multiple { + discr_kind: layout::DiscriminantKind::Niche { + dataful_variant, + .. + }, + .. + } => { // Only the niche itself is always initialized, // so only check for a pointer at its offset. // diff --git a/src/librustc_codegen_ssa/mir/place.rs b/src/librustc_codegen_ssa/mir/place.rs index 7cafa0088a01..39d7638ae088 100644 --- a/src/librustc_codegen_ssa/mir/place.rs +++ b/src/librustc_codegen_ssa/mir/place.rs @@ -216,37 +216,36 @@ impl<'a, 'tcx: 'a, V: CodegenObject> PlaceRef<'tcx, V> { if self.layout.abi.is_uninhabited() { return bx.cx().const_undef(cast_to); } - match self.layout.variants { + let (discr_scalar, discr_kind) = match self.layout.variants { layout::Variants::Single { index } => { let discr_val = self.layout.ty.ty_adt_def().map_or( index.as_u32() as u128, |def| def.discriminant_for_variant(bx.cx().tcx(), index).val); return bx.cx().const_uint_big(cast_to, discr_val); } - layout::Variants::Tagged { .. } | - layout::Variants::NicheFilling { .. } => {}, - } + layout::Variants::Multiple { ref discr, ref discr_kind, .. } => { + (discr, discr_kind) + } + }; let discr = self.project_field(bx, 0); let lldiscr = bx.load_operand(discr).immediate(); - match self.layout.variants { - layout::Variants::Single { .. } => bug!(), - layout::Variants::Tagged { ref tag, .. } => { - let signed = match tag.value { + match *discr_kind { + layout::DiscriminantKind::Tag => { + let signed = match discr_scalar.value { // We use `i1` for bytes that are always `0` or `1`, // e.g., `#[repr(i8)] enum E { A, B }`, but we can't // let LLVM interpret the `i1` as signed, because // then `i1 1` (i.e., E::B) is effectively `i8 -1`. - layout::Int(_, signed) => !tag.is_bool() && signed, + layout::Int(_, signed) => !discr_scalar.is_bool() && signed, _ => false }; bx.intcast(lldiscr, cast_to, signed) } - layout::Variants::NicheFilling { + layout::DiscriminantKind::Niche { dataful_variant, ref niche_variants, niche_start, - .. } => { let niche_llty = bx.cx().immediate_backend_type(discr.layout); if niche_variants.start() == niche_variants.end() { @@ -291,7 +290,10 @@ impl<'a, 'tcx: 'a, V: CodegenObject> PlaceRef<'tcx, V> { layout::Variants::Single { index } => { assert_eq!(index, variant_index); } - layout::Variants::Tagged { .. } => { + layout::Variants::Multiple { + discr_kind: layout::DiscriminantKind::Tag, + .. + } => { let ptr = self.project_field(bx, 0); let to = self.layout.ty.ty_adt_def().unwrap() .discriminant_for_variant(bx.tcx(), variant_index) @@ -301,10 +303,12 @@ impl<'a, 'tcx: 'a, V: CodegenObject> PlaceRef<'tcx, V> { ptr.llval, ptr.align); } - layout::Variants::NicheFilling { - dataful_variant, - ref niche_variants, - niche_start, + layout::Variants::Multiple { + discr_kind: layout::DiscriminantKind::Niche { + dataful_variant, + ref niche_variants, + niche_start, + }, .. } => { if variant_index != dataful_variant { diff --git a/src/librustc_codegen_ssa/mir/rvalue.rs b/src/librustc_codegen_ssa/mir/rvalue.rs index b8131671320e..6815d08321ef 100644 --- a/src/librustc_codegen_ssa/mir/rvalue.rs +++ b/src/librustc_codegen_ssa/mir/rvalue.rs @@ -303,8 +303,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { }); } } - layout::Variants::Tagged { .. } | - layout::Variants::NicheFilling { .. } => {}, + layout::Variants::Multiple { .. } => {}, } let llval = operand.immediate(); diff --git a/src/librustc_lint/types.rs b/src/librustc_lint/types.rs index 4ad1a00afe97..494a9bb73ed4 100644 --- a/src/librustc_lint/types.rs +++ b/src/librustc_lint/types.rs @@ -842,51 +842,56 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for VariantSizeDifferences { let item_def_id = cx.tcx.hir().local_def_id_from_hir_id(it.hir_id); let t = cx.tcx.type_of(item_def_id); let ty = cx.tcx.erase_regions(&t); - match cx.layout_of(ty) { - Ok(layout) => { - let variants = &layout.variants; - if let layout::Variants::Tagged { ref variants, ref tag, .. } = variants { - let discr_size = tag.value.size(&cx.tcx).bytes(); - - debug!("enum `{}` is {} bytes large with layout:\n{:#?}", - t, layout.size.bytes(), layout); - - let (largest, slargest, largest_index) = enum_definition.variants - .iter() - .zip(variants) - .map(|(variant, variant_layout)| { - // Subtract the size of the enum discriminant. - let bytes = variant_layout.size.bytes().saturating_sub(discr_size); - - debug!("- variant `{}` is {} bytes large", - variant.node.ident, - bytes); - bytes - }) - .enumerate() - .fold((0, 0, 0), |(l, s, li), (idx, size)| if size > l { - (size, l, idx) - } else if size > s { - (l, size, li) - } else { - (l, s, li) - }); - - // We only warn if the largest variant is at least thrice as large as - // the second-largest. - if largest > slargest * 3 && slargest > 0 { - cx.span_lint(VARIANT_SIZE_DIFFERENCES, - enum_definition.variants[largest_index].span, - &format!("enum variant is more than three times \ - larger ({} bytes) than the next largest", - largest)); - } - } - } + let layout = match cx.layout_of(ty) { + Ok(layout) => layout, Err(ty::layout::LayoutError::Unknown(_)) => return, Err(err @ ty::layout::LayoutError::SizeOverflow(_)) => { bug!("failed to get layout for `{}`: {}", t, err); } + }; + let (variants, tag) = match layout.variants { + layout::Variants::Multiple { + discr_kind: layout::DiscriminantKind::Tag, + ref discr, + ref variants, + } => (variants, discr), + _ => return, + }; + + let discr_size = tag.value.size(&cx.tcx).bytes(); + + debug!("enum `{}` is {} bytes large with layout:\n{:#?}", + t, layout.size.bytes(), layout); + + let (largest, slargest, largest_index) = enum_definition.variants + .iter() + .zip(variants) + .map(|(variant, variant_layout)| { + // Subtract the size of the enum discriminant. + let bytes = variant_layout.size.bytes().saturating_sub(discr_size); + + debug!("- variant `{}` is {} bytes large", + variant.node.ident, + bytes); + bytes + }) + .enumerate() + .fold((0, 0, 0), |(l, s, li), (idx, size)| if size > l { + (size, l, idx) + } else if size > s { + (l, size, li) + } else { + (l, s, li) + }); + + // We only warn if the largest variant is at least thrice as large as + // the second-largest. + if largest > slargest * 3 && slargest > 0 { + cx.span_lint(VARIANT_SIZE_DIFFERENCES, + enum_definition.variants[largest_index].span, + &format!("enum variant is more than three times \ + larger ({} bytes) than the next largest", + largest)); } } } diff --git a/src/librustc_mir/interpret/cast.rs b/src/librustc_mir/interpret/cast.rs index 003c2182d0b4..ba61b03ea673 100644 --- a/src/librustc_mir/interpret/cast.rs +++ b/src/librustc_mir/interpret/cast.rs @@ -64,8 +64,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> InterpretCx<'a, 'mir, 'tcx, M> dest); } } - layout::Variants::Tagged { .. } | - layout::Variants::NicheFilling { .. } => {}, + layout::Variants::Multiple { .. } => {}, } let dest_val = self.cast_scalar(src.to_scalar()?, src.layout, dest.layout)?; diff --git a/src/librustc_mir/interpret/operand.rs b/src/librustc_mir/interpret/operand.rs index 15b6d5c914d2..38a9371b9272 100644 --- a/src/librustc_mir/interpret/operand.rs +++ b/src/librustc_mir/interpret/operand.rs @@ -610,25 +610,24 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> InterpretCx<'a, 'mir, 'tcx, M> ) -> EvalResult<'tcx, (u128, VariantIdx)> { trace!("read_discriminant_value {:#?}", rval.layout); - match rval.layout.variants { + let discr_kind = match rval.layout.variants { layout::Variants::Single { index } => { let discr_val = rval.layout.ty.ty_adt_def().map_or( index.as_u32() as u128, |def| def.discriminant_for_variant(*self.tcx, index).val); return Ok((discr_val, index)); } - layout::Variants::Tagged { .. } | - layout::Variants::NicheFilling { .. } => {}, - } + layout::Variants::Multiple { ref discr_kind, .. } => discr_kind, + }; + // read raw discriminant value let discr_op = self.operand_field(rval, 0)?; let discr_val = self.read_immediate(discr_op)?; let raw_discr = discr_val.to_scalar_or_undef(); trace!("discr value: {:?}", raw_discr); // post-process - Ok(match rval.layout.variants { - layout::Variants::Single { .. } => bug!(), - layout::Variants::Tagged { .. } => { + Ok(match *discr_kind { + layout::DiscriminantKind::Tag => { let bits_discr = match raw_discr.to_bits(discr_val.layout.size) { Ok(raw_discr) => raw_discr, Err(_) => return err!(InvalidDiscriminant(raw_discr.erase_tag())), @@ -657,11 +656,10 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> InterpretCx<'a, 'mir, 'tcx, M> .ok_or_else(|| EvalErrorKind::InvalidDiscriminant(raw_discr.erase_tag()))?; (real_discr, index.0) }, - layout::Variants::NicheFilling { + layout::DiscriminantKind::Niche { dataful_variant, ref niche_variants, niche_start, - .. } => { let variants_start = niche_variants.start().as_u32() as u128; let variants_end = niche_variants.end().as_u32() as u128; diff --git a/src/librustc_mir/interpret/place.rs b/src/librustc_mir/interpret/place.rs index 82b92acdb764..4d51772d5ea1 100644 --- a/src/librustc_mir/interpret/place.rs +++ b/src/librustc_mir/interpret/place.rs @@ -958,7 +958,11 @@ where layout::Variants::Single { index } => { assert_eq!(index, variant_index); } - layout::Variants::Tagged { ref tag, .. } => { + layout::Variants::Multiple { + discr_kind: layout::DiscriminantKind::Tag, + ref discr, + .. + } => { let adt_def = dest.layout.ty.ty_adt_def().unwrap(); assert!(variant_index.as_usize() < adt_def.variants.len()); let discr_val = adt_def @@ -968,16 +972,18 @@ where // raw discriminants for enums are isize or bigger during // their computation, but the in-memory tag is the smallest possible // representation - let size = tag.value.size(self); + let size = discr.value.size(self); let discr_val = truncate(discr_val, size); let discr_dest = self.place_field(dest, 0)?; self.write_scalar(Scalar::from_uint(discr_val, size), discr_dest)?; } - layout::Variants::NicheFilling { - dataful_variant, - ref niche_variants, - niche_start, + layout::Variants::Multiple { + discr_kind: layout::DiscriminantKind::Niche { + dataful_variant, + ref niche_variants, + niche_start, + }, .. } => { assert!( diff --git a/src/librustc_mir/interpret/visitor.rs b/src/librustc_mir/interpret/visitor.rs index 90d4fff42183..05343ac66d96 100644 --- a/src/librustc_mir/interpret/visitor.rs +++ b/src/librustc_mir/interpret/visitor.rs @@ -241,8 +241,7 @@ macro_rules! make_value_visitor { // If this is a multi-variant layout, we have find the right one and proceed with // that. match v.layout().variants { - layout::Variants::NicheFilling { .. } | - layout::Variants::Tagged { .. } => { + layout::Variants::Multiple { .. } => { let op = v.to_op(self.ecx())?; let idx = self.ecx().read_discriminant(op)?.1; let inner = v.project_downcast(self.ecx(), idx)?; diff --git a/src/librustc_target/abi/call/x86_64.rs b/src/librustc_target/abi/call/x86_64.rs index 680e529b108e..b68c70224c95 100644 --- a/src/librustc_target/abi/call/x86_64.rs +++ b/src/librustc_target/abi/call/x86_64.rs @@ -61,8 +61,7 @@ fn classify_arg<'a, Ty, C>(cx: &C, arg: &ArgType<'a, Ty>) } return Ok(()); } - abi::Variants::Tagged { .. } | - abi::Variants::NicheFilling { .. } => return Err(Memory), + abi::Variants::Multiple { .. } => return Err(Memory), } } diff --git a/src/librustc_target/abi/mod.rs b/src/librustc_target/abi/mod.rs index 8b96a8c1658b..235b530a7ef2 100644 --- a/src/librustc_target/abi/mod.rs +++ b/src/librustc_target/abi/mod.rs @@ -828,15 +828,22 @@ pub enum Variants { index: VariantIdx, }, - /// General-case enums: for each case there is a struct, and they all have - /// all space reserved for the tag, and their first field starts - /// at a non-0 offset, after where the tag would go. - Tagged { - tag: Scalar, + /// Enums with more than one inhabited variant: for each case there is + /// a struct, and they all have space reserved for the discriminant, + /// which is the sole field of the enum layout. + Multiple { + discr: Scalar, + discr_kind: DiscriminantKind, variants: IndexVec, }, +} + +#[derive(PartialEq, Eq, Hash, Debug)] +pub enum DiscriminantKind { + /// Integer tag holding the discriminant value itself. + Tag, - /// Multiple cases distinguished by a niche (values invalid for a type): + /// Niche (values invalid for a type) encoding the discriminant: /// the variant `dataful_variant` contains a niche at an arbitrary /// offset (field 0 of the enum), which for a variant with discriminant /// `d` is set to `(d - niche_variants.start).wrapping_add(niche_start)`. @@ -844,13 +851,11 @@ pub enum Variants { /// For example, `Option<(usize, &T)>` is represented such that /// `None` has a null pointer for the second tuple field, and /// `Some` is the identity function (with a non-null reference). - NicheFilling { + Niche { dataful_variant: VariantIdx, niche_variants: RangeInclusive, - niche: Scalar, niche_start: u128, - variants: IndexVec, - } + }, } #[derive(PartialEq, Eq, Hash, Debug)] From 8381cbab1a80b2c71df79fb8c33f480cfbca52bb Mon Sep 17 00:00:00 2001 From: Yuki OKUSHI Date: Sat, 30 Mar 2019 18:50:19 +0900 Subject: [PATCH 03/30] Add target_mcount option --- src/librustc_target/spec/aarch64_unknown_linux_gnu.rs | 1 + src/librustc_target/spec/aarch64_unknown_linux_musl.rs | 1 + src/librustc_target/spec/aarch64_unknown_netbsd.rs | 7 +++++-- src/librustc_target/spec/arm_unknown_linux_gnueabi.rs | 1 + src/librustc_target/spec/arm_unknown_linux_gnueabihf.rs | 1 + src/librustc_target/spec/arm_unknown_linux_musleabi.rs | 1 + src/librustc_target/spec/arm_unknown_linux_musleabihf.rs | 1 + src/librustc_target/spec/armv4t_unknown_linux_gnueabi.rs | 1 + src/librustc_target/spec/armv5te_unknown_linux_gnueabi.rs | 1 + .../spec/armv5te_unknown_linux_musleabi.rs | 1 + src/librustc_target/spec/armv6_unknown_freebsd.rs | 1 + src/librustc_target/spec/armv6_unknown_netbsd_eabihf.rs | 1 + src/librustc_target/spec/armv7_unknown_cloudabi_eabihf.rs | 7 +++++-- src/librustc_target/spec/armv7_unknown_freebsd.rs | 1 + src/librustc_target/spec/armv7_unknown_linux_gnueabihf.rs | 1 + .../spec/armv7_unknown_linux_musleabihf.rs | 1 + src/librustc_target/spec/armv7_unknown_netbsd_eabihf.rs | 1 + src/librustc_target/spec/i686_apple_darwin.rs | 7 +++++-- src/librustc_target/spec/i686_unknown_netbsd.rs | 7 +++++-- src/librustc_target/spec/mips64_unknown_linux_gnuabi64.rs | 1 + .../spec/mips64el_unknown_linux_gnuabi64.rs | 1 + src/librustc_target/spec/mips_unknown_linux_gnu.rs | 1 + src/librustc_target/spec/mips_unknown_linux_musl.rs | 7 +++++-- src/librustc_target/spec/mips_unknown_linux_uclibc.rs | 1 + src/librustc_target/spec/mipsel_unknown_linux_gnu.rs | 1 + src/librustc_target/spec/mipsel_unknown_linux_musl.rs | 7 +++++-- src/librustc_target/spec/mipsel_unknown_linux_uclibc.rs | 1 + src/librustc_target/spec/mipsisa32r6_unknown_linux_gnu.rs | 1 + .../spec/mipsisa32r6el_unknown_linux_gnu.rs | 1 + .../spec/mipsisa64r6_unknown_linux_gnuabi64.rs | 1 + .../spec/mipsisa64r6el_unknown_linux_gnuabi64.rs | 1 + src/librustc_target/spec/mod.rs | 8 +++++++- src/librustc_target/spec/powerpc64_unknown_freebsd.rs | 7 +++++-- src/librustc_target/spec/powerpc64_unknown_linux_gnu.rs | 7 +++++-- src/librustc_target/spec/powerpc64_unknown_linux_musl.rs | 7 +++++-- src/librustc_target/spec/powerpc64le_unknown_linux_gnu.rs | 7 +++++-- .../spec/powerpc64le_unknown_linux_musl.rs | 7 +++++-- src/librustc_target/spec/powerpc_unknown_linux_gnu.rs | 7 +++++-- src/librustc_target/spec/powerpc_unknown_linux_gnuspe.rs | 7 +++++-- src/librustc_target/spec/powerpc_unknown_linux_musl.rs | 7 +++++-- src/librustc_target/spec/powerpc_unknown_netbsd.rs | 7 +++++-- src/librustc_target/spec/sparc64_unknown_netbsd.rs | 7 +++++-- src/librustc_target/spec/x86_64_apple_darwin.rs | 7 +++++-- src/librustc_target/spec/x86_64_rumprun_netbsd.rs | 7 +++++-- src/librustc_target/spec/x86_64_unknown_netbsd.rs | 7 +++++-- 45 files changed, 127 insertions(+), 39 deletions(-) diff --git a/src/librustc_target/spec/aarch64_unknown_linux_gnu.rs b/src/librustc_target/spec/aarch64_unknown_linux_gnu.rs index e772d8b532cb..59a285736412 100644 --- a/src/librustc_target/spec/aarch64_unknown_linux_gnu.rs +++ b/src/librustc_target/spec/aarch64_unknown_linux_gnu.rs @@ -17,6 +17,7 @@ pub fn target() -> TargetResult { linker_flavor: LinkerFlavor::Gcc, options: TargetOptions { abi_blacklist: super::arm_base::abi_blacklist(), + target_mcount: "\01_mcount".to_string(), .. base }, }) diff --git a/src/librustc_target/spec/aarch64_unknown_linux_musl.rs b/src/librustc_target/spec/aarch64_unknown_linux_musl.rs index 8123ee82ed52..781b1322f5ac 100644 --- a/src/librustc_target/spec/aarch64_unknown_linux_musl.rs +++ b/src/librustc_target/spec/aarch64_unknown_linux_musl.rs @@ -17,6 +17,7 @@ pub fn target() -> TargetResult { linker_flavor: LinkerFlavor::Gcc, options: TargetOptions { abi_blacklist: super::arm_base::abi_blacklist(), + target_mcount: "\01_mcount".to_string(), .. base }, }) diff --git a/src/librustc_target/spec/aarch64_unknown_netbsd.rs b/src/librustc_target/spec/aarch64_unknown_netbsd.rs index 47ae08ade9a6..455cbebb91e2 100644 --- a/src/librustc_target/spec/aarch64_unknown_netbsd.rs +++ b/src/librustc_target/spec/aarch64_unknown_netbsd.rs @@ -1,4 +1,4 @@ -use crate::spec::{LinkerFlavor, Target, TargetResult}; +use crate::spec::{LinkerFlavor, Target, TargetOptions, TargetResult}; pub fn target() -> TargetResult { let mut base = super::netbsd_base::opts(); @@ -16,6 +16,9 @@ pub fn target() -> TargetResult { target_env: String::new(), target_vendor: "unknown".to_string(), linker_flavor: LinkerFlavor::Gcc, - options: base, + options: TargetOptions { + target_mcount: "__mcount".to_string(), + .. base + }, }) } diff --git a/src/librustc_target/spec/arm_unknown_linux_gnueabi.rs b/src/librustc_target/spec/arm_unknown_linux_gnueabi.rs index f291818ba80f..1173d18634e1 100644 --- a/src/librustc_target/spec/arm_unknown_linux_gnueabi.rs +++ b/src/librustc_target/spec/arm_unknown_linux_gnueabi.rs @@ -18,6 +18,7 @@ pub fn target() -> TargetResult { options: TargetOptions { features: "+strict-align,+v6".to_string(), abi_blacklist: super::arm_base::abi_blacklist(), + target_mcount: "\01__gnu_mcount_nc".to_string(), .. base }, }) diff --git a/src/librustc_target/spec/arm_unknown_linux_gnueabihf.rs b/src/librustc_target/spec/arm_unknown_linux_gnueabihf.rs index 32b509d9721e..6fc7c46be3b1 100644 --- a/src/librustc_target/spec/arm_unknown_linux_gnueabihf.rs +++ b/src/librustc_target/spec/arm_unknown_linux_gnueabihf.rs @@ -18,6 +18,7 @@ pub fn target() -> TargetResult { options: TargetOptions { features: "+strict-align,+v6,+vfp2".to_string(), abi_blacklist: super::arm_base::abi_blacklist(), + target_mcount: "\01__gnu_mcount_nc".to_string(), .. base } }) diff --git a/src/librustc_target/spec/arm_unknown_linux_musleabi.rs b/src/librustc_target/spec/arm_unknown_linux_musleabi.rs index 7637577e7e84..7a2a4e85e4a1 100644 --- a/src/librustc_target/spec/arm_unknown_linux_musleabi.rs +++ b/src/librustc_target/spec/arm_unknown_linux_musleabi.rs @@ -23,6 +23,7 @@ pub fn target() -> TargetResult { linker_flavor: LinkerFlavor::Gcc, options: TargetOptions { abi_blacklist: super::arm_base::abi_blacklist(), + target_mcount: "\01mcount".to_string(), .. base }, }) diff --git a/src/librustc_target/spec/arm_unknown_linux_musleabihf.rs b/src/librustc_target/spec/arm_unknown_linux_musleabihf.rs index 9def151b3ef2..b7aa1952cb0b 100644 --- a/src/librustc_target/spec/arm_unknown_linux_musleabihf.rs +++ b/src/librustc_target/spec/arm_unknown_linux_musleabihf.rs @@ -23,6 +23,7 @@ pub fn target() -> TargetResult { linker_flavor: LinkerFlavor::Gcc, options: TargetOptions { abi_blacklist: super::arm_base::abi_blacklist(), + target_mcount: "\01mcount".to_string(), .. base }, }) diff --git a/src/librustc_target/spec/armv4t_unknown_linux_gnueabi.rs b/src/librustc_target/spec/armv4t_unknown_linux_gnueabi.rs index 7cd4b14cdebc..59c15f1610de 100644 --- a/src/librustc_target/spec/armv4t_unknown_linux_gnueabi.rs +++ b/src/librustc_target/spec/armv4t_unknown_linux_gnueabi.rs @@ -19,6 +19,7 @@ pub fn target() -> TargetResult { // Atomic operations provided by compiler-builtins max_atomic_width: Some(32), abi_blacklist: super::arm_base::abi_blacklist(), + target_mcount: "\01__gnu_mcount_nc".to_string(), .. base } }) diff --git a/src/librustc_target/spec/armv5te_unknown_linux_gnueabi.rs b/src/librustc_target/spec/armv5te_unknown_linux_gnueabi.rs index 15f614827718..a1a61a69d052 100644 --- a/src/librustc_target/spec/armv5te_unknown_linux_gnueabi.rs +++ b/src/librustc_target/spec/armv5te_unknown_linux_gnueabi.rs @@ -19,6 +19,7 @@ pub fn target() -> TargetResult { // Atomic operations provided by compiler-builtins max_atomic_width: Some(32), abi_blacklist: super::arm_base::abi_blacklist(), + target_mcount: "\01__gnu_mcount_nc".to_string(), .. base } }) diff --git a/src/librustc_target/spec/armv5te_unknown_linux_musleabi.rs b/src/librustc_target/spec/armv5te_unknown_linux_musleabi.rs index 74915b942ea4..9b44a54715e7 100644 --- a/src/librustc_target/spec/armv5te_unknown_linux_musleabi.rs +++ b/src/librustc_target/spec/armv5te_unknown_linux_musleabi.rs @@ -22,6 +22,7 @@ pub fn target() -> TargetResult { // Atomic operations provided by compiler-builtins max_atomic_width: Some(32), abi_blacklist: super::arm_base::abi_blacklist(), + target_mcount: "\01mcount".to_string(), .. base } }) diff --git a/src/librustc_target/spec/armv6_unknown_freebsd.rs b/src/librustc_target/spec/armv6_unknown_freebsd.rs index 39886a16a740..e706c8281d23 100644 --- a/src/librustc_target/spec/armv6_unknown_freebsd.rs +++ b/src/librustc_target/spec/armv6_unknown_freebsd.rs @@ -18,6 +18,7 @@ pub fn target() -> TargetResult { features: "+v6,+vfp2".to_string(), max_atomic_width: Some(64), abi_blacklist: super::arm_base::abi_blacklist(), + target_mcount: "\01__gnu_mcount_nc".to_string(), .. base } }) diff --git a/src/librustc_target/spec/armv6_unknown_netbsd_eabihf.rs b/src/librustc_target/spec/armv6_unknown_netbsd_eabihf.rs index e460b6c574a2..b056776bdfb8 100644 --- a/src/librustc_target/spec/armv6_unknown_netbsd_eabihf.rs +++ b/src/librustc_target/spec/armv6_unknown_netbsd_eabihf.rs @@ -18,6 +18,7 @@ pub fn target() -> TargetResult { options: TargetOptions { features: "+v6,+vfp2".to_string(), abi_blacklist: super::arm_base::abi_blacklist(), + target_mcount: "__mcount".to_string(), .. base } }) diff --git a/src/librustc_target/spec/armv7_unknown_cloudabi_eabihf.rs b/src/librustc_target/spec/armv7_unknown_cloudabi_eabihf.rs index a6c7fb537c78..70d4985ab860 100644 --- a/src/librustc_target/spec/armv7_unknown_cloudabi_eabihf.rs +++ b/src/librustc_target/spec/armv7_unknown_cloudabi_eabihf.rs @@ -1,4 +1,4 @@ -use crate::spec::{LinkerFlavor, Target, TargetResult}; +use crate::spec::{LinkerFlavor, Target, TargetOptions, TargetResult}; pub fn target() -> TargetResult { let mut base = super::cloudabi_base::opts(); @@ -19,6 +19,9 @@ pub fn target() -> TargetResult { target_env: String::new(), target_vendor: "unknown".to_string(), linker_flavor: LinkerFlavor::Gcc, - options: base, + options: TargetOptions { + target_mcount: "\01mcount".to_string(), + .. base + }, }) } diff --git a/src/librustc_target/spec/armv7_unknown_freebsd.rs b/src/librustc_target/spec/armv7_unknown_freebsd.rs index ba63fd2bf053..0f9ee5d44a95 100644 --- a/src/librustc_target/spec/armv7_unknown_freebsd.rs +++ b/src/librustc_target/spec/armv7_unknown_freebsd.rs @@ -18,6 +18,7 @@ pub fn target() -> TargetResult { features: "+v7,+vfp3,+d16,+thumb2,-neon".to_string(), max_atomic_width: Some(64), abi_blacklist: super::arm_base::abi_blacklist(), + target_mcount: "\01__gnu_mcount_nc".to_string(), .. base } }) diff --git a/src/librustc_target/spec/armv7_unknown_linux_gnueabihf.rs b/src/librustc_target/spec/armv7_unknown_linux_gnueabihf.rs index f16215433c76..afba6f608ee0 100644 --- a/src/librustc_target/spec/armv7_unknown_linux_gnueabihf.rs +++ b/src/librustc_target/spec/armv7_unknown_linux_gnueabihf.rs @@ -23,6 +23,7 @@ pub fn target() -> TargetResult { cpu: "generic".to_string(), max_atomic_width: Some(64), abi_blacklist: super::arm_base::abi_blacklist(), + target_mcount: "\01__gnu_mcount_nc".to_string(), .. base } }) diff --git a/src/librustc_target/spec/armv7_unknown_linux_musleabihf.rs b/src/librustc_target/spec/armv7_unknown_linux_musleabihf.rs index 45a26966b716..516d0401adf5 100644 --- a/src/librustc_target/spec/armv7_unknown_linux_musleabihf.rs +++ b/src/librustc_target/spec/armv7_unknown_linux_musleabihf.rs @@ -26,6 +26,7 @@ pub fn target() -> TargetResult { cpu: "generic".to_string(), max_atomic_width: Some(64), abi_blacklist: super::arm_base::abi_blacklist(), + target_mcount: "\01mcount".to_string(), .. base } }) diff --git a/src/librustc_target/spec/armv7_unknown_netbsd_eabihf.rs b/src/librustc_target/spec/armv7_unknown_netbsd_eabihf.rs index 44e2636e9188..e2d55e9317b7 100644 --- a/src/librustc_target/spec/armv7_unknown_netbsd_eabihf.rs +++ b/src/librustc_target/spec/armv7_unknown_netbsd_eabihf.rs @@ -19,6 +19,7 @@ pub fn target() -> TargetResult { cpu: "generic".to_string(), max_atomic_width: Some(64), abi_blacklist: super::arm_base::abi_blacklist(), + target_mcount: "__mcount".to_string(), .. base } }) diff --git a/src/librustc_target/spec/i686_apple_darwin.rs b/src/librustc_target/spec/i686_apple_darwin.rs index c8a61296d33d..a82ff33ce1db 100644 --- a/src/librustc_target/spec/i686_apple_darwin.rs +++ b/src/librustc_target/spec/i686_apple_darwin.rs @@ -1,4 +1,4 @@ -use crate::spec::{LinkerFlavor, Target, TargetResult}; +use crate::spec::{LinkerFlavor, Target, TargetOptions, TargetResult}; pub fn target() -> TargetResult { let mut base = super::apple_base::opts(); @@ -19,6 +19,9 @@ pub fn target() -> TargetResult { target_env: String::new(), target_vendor: "apple".to_string(), linker_flavor: LinkerFlavor::Gcc, - options: base, + options: TargetOptions { + target_mcount: "\01mcount".to_string(), + .. base + }, }) } diff --git a/src/librustc_target/spec/i686_unknown_netbsd.rs b/src/librustc_target/spec/i686_unknown_netbsd.rs index e8a9f29ea5f4..99130e93dad6 100644 --- a/src/librustc_target/spec/i686_unknown_netbsd.rs +++ b/src/librustc_target/spec/i686_unknown_netbsd.rs @@ -1,4 +1,4 @@ -use crate::spec::{LinkerFlavor, Target, TargetResult}; +use crate::spec::{LinkerFlavor, Target, TargetOptions, TargetResult}; pub fn target() -> TargetResult { let mut base = super::netbsd_base::opts(); @@ -18,6 +18,9 @@ pub fn target() -> TargetResult { target_env: String::new(), target_vendor: "unknown".to_string(), linker_flavor: LinkerFlavor::Gcc, - options: base, + options: TargetOptions { + target_mcount: "__mcount".to_string(), + .. base + }, }) } diff --git a/src/librustc_target/spec/mips64_unknown_linux_gnuabi64.rs b/src/librustc_target/spec/mips64_unknown_linux_gnuabi64.rs index 3b38e64050f3..b2ea8a6f3881 100644 --- a/src/librustc_target/spec/mips64_unknown_linux_gnuabi64.rs +++ b/src/librustc_target/spec/mips64_unknown_linux_gnuabi64.rs @@ -17,6 +17,7 @@ pub fn target() -> TargetResult { cpu: "mips64r2".to_string(), features: "+mips64r2".to_string(), max_atomic_width: Some(64), + target_mcount: "_mcount".to_string(), ..super::linux_base::opts() }, diff --git a/src/librustc_target/spec/mips64el_unknown_linux_gnuabi64.rs b/src/librustc_target/spec/mips64el_unknown_linux_gnuabi64.rs index 0f6cd86d616d..48aea4a39b0a 100644 --- a/src/librustc_target/spec/mips64el_unknown_linux_gnuabi64.rs +++ b/src/librustc_target/spec/mips64el_unknown_linux_gnuabi64.rs @@ -17,6 +17,7 @@ pub fn target() -> TargetResult { cpu: "mips64r2".to_string(), features: "+mips64r2".to_string(), max_atomic_width: Some(64), + target_mcount: "_mcount".to_string(), ..super::linux_base::opts() }, diff --git a/src/librustc_target/spec/mips_unknown_linux_gnu.rs b/src/librustc_target/spec/mips_unknown_linux_gnu.rs index b4d29c5fbeaf..e360abdb38d3 100644 --- a/src/librustc_target/spec/mips_unknown_linux_gnu.rs +++ b/src/librustc_target/spec/mips_unknown_linux_gnu.rs @@ -16,6 +16,7 @@ pub fn target() -> TargetResult { cpu: "mips32r2".to_string(), features: "+mips32r2,+fpxx,+nooddspreg".to_string(), max_atomic_width: Some(32), + target_mcount: "_mcount".to_string(), ..super::linux_base::opts() }, diff --git a/src/librustc_target/spec/mips_unknown_linux_musl.rs b/src/librustc_target/spec/mips_unknown_linux_musl.rs index c56c6e382295..3f5428de9507 100644 --- a/src/librustc_target/spec/mips_unknown_linux_musl.rs +++ b/src/librustc_target/spec/mips_unknown_linux_musl.rs @@ -1,4 +1,4 @@ -use crate::spec::{LinkerFlavor, Target, TargetResult}; +use crate::spec::{LinkerFlavor, Target, TargetOptions, TargetResult}; pub fn target() -> TargetResult { let mut base = super::linux_musl_base::opts(); @@ -17,6 +17,9 @@ pub fn target() -> TargetResult { target_env: "musl".to_string(), target_vendor: "unknown".to_string(), linker_flavor: LinkerFlavor::Gcc, - options: base, + options: TargetOptions { + target_mcount: "_mcount".to_string(), + .. base + }, }) } diff --git a/src/librustc_target/spec/mips_unknown_linux_uclibc.rs b/src/librustc_target/spec/mips_unknown_linux_uclibc.rs index cb02769c7dfe..8116b8c9cc84 100644 --- a/src/librustc_target/spec/mips_unknown_linux_uclibc.rs +++ b/src/librustc_target/spec/mips_unknown_linux_uclibc.rs @@ -16,6 +16,7 @@ pub fn target() -> TargetResult { cpu: "mips32r2".to_string(), features: "+mips32r2,+soft-float".to_string(), max_atomic_width: Some(32), + target_mcount: "_mcount".to_string(), ..super::linux_base::opts() }, diff --git a/src/librustc_target/spec/mipsel_unknown_linux_gnu.rs b/src/librustc_target/spec/mipsel_unknown_linux_gnu.rs index ed49ddd49937..7e9d8cd942a2 100644 --- a/src/librustc_target/spec/mipsel_unknown_linux_gnu.rs +++ b/src/librustc_target/spec/mipsel_unknown_linux_gnu.rs @@ -17,6 +17,7 @@ pub fn target() -> TargetResult { cpu: "mips32r2".to_string(), features: "+mips32r2,+fpxx,+nooddspreg".to_string(), max_atomic_width: Some(32), + target_mcount: "_mcount".to_string(), ..super::linux_base::opts() }, diff --git a/src/librustc_target/spec/mipsel_unknown_linux_musl.rs b/src/librustc_target/spec/mipsel_unknown_linux_musl.rs index bcc49cf5ffe4..56ad2940feba 100644 --- a/src/librustc_target/spec/mipsel_unknown_linux_musl.rs +++ b/src/librustc_target/spec/mipsel_unknown_linux_musl.rs @@ -1,4 +1,4 @@ -use crate::spec::{LinkerFlavor, Target, TargetResult}; +use crate::spec::{LinkerFlavor, Target, TargetOptions, TargetResult}; pub fn target() -> TargetResult { let mut base = super::linux_musl_base::opts(); @@ -17,6 +17,9 @@ pub fn target() -> TargetResult { target_env: "musl".to_string(), target_vendor: "unknown".to_string(), linker_flavor: LinkerFlavor::Gcc, - options: base, + options: TargetOptions { + target_mcount: "_mcount".to_string(), + .. base + }, }) } diff --git a/src/librustc_target/spec/mipsel_unknown_linux_uclibc.rs b/src/librustc_target/spec/mipsel_unknown_linux_uclibc.rs index 205f328a24ce..a8152011efa7 100644 --- a/src/librustc_target/spec/mipsel_unknown_linux_uclibc.rs +++ b/src/librustc_target/spec/mipsel_unknown_linux_uclibc.rs @@ -17,6 +17,7 @@ pub fn target() -> TargetResult { cpu: "mips32r2".to_string(), features: "+mips32r2,+soft-float".to_string(), max_atomic_width: Some(32), + target_mcount: "_mcount".to_string(), ..super::linux_base::opts() }, diff --git a/src/librustc_target/spec/mipsisa32r6_unknown_linux_gnu.rs b/src/librustc_target/spec/mipsisa32r6_unknown_linux_gnu.rs index f47291458492..36b83c63fca2 100644 --- a/src/librustc_target/spec/mipsisa32r6_unknown_linux_gnu.rs +++ b/src/librustc_target/spec/mipsisa32r6_unknown_linux_gnu.rs @@ -16,6 +16,7 @@ pub fn target() -> TargetResult { cpu: "mips32r6".to_string(), features: "+mips32r6".to_string(), max_atomic_width: Some(32), + target_mcount: "_mcount".to_string(), ..super::linux_base::opts() }, diff --git a/src/librustc_target/spec/mipsisa32r6el_unknown_linux_gnu.rs b/src/librustc_target/spec/mipsisa32r6el_unknown_linux_gnu.rs index f4f98d33571f..717ae3f1d20e 100644 --- a/src/librustc_target/spec/mipsisa32r6el_unknown_linux_gnu.rs +++ b/src/librustc_target/spec/mipsisa32r6el_unknown_linux_gnu.rs @@ -17,6 +17,7 @@ pub fn target() -> TargetResult { cpu: "mips32r6".to_string(), features: "+mips32r6".to_string(), max_atomic_width: Some(32), + target_mcount: "_mcount".to_string(), ..super::linux_base::opts() }, diff --git a/src/librustc_target/spec/mipsisa64r6_unknown_linux_gnuabi64.rs b/src/librustc_target/spec/mipsisa64r6_unknown_linux_gnuabi64.rs index 7faed3adc79c..3f7d233e55fb 100644 --- a/src/librustc_target/spec/mipsisa64r6_unknown_linux_gnuabi64.rs +++ b/src/librustc_target/spec/mipsisa64r6_unknown_linux_gnuabi64.rs @@ -17,6 +17,7 @@ pub fn target() -> TargetResult { cpu: "mips64r6".to_string(), features: "+mips64r6".to_string(), max_atomic_width: Some(64), + target_mcount: "_mcount".to_string(), ..super::linux_base::opts() }, diff --git a/src/librustc_target/spec/mipsisa64r6el_unknown_linux_gnuabi64.rs b/src/librustc_target/spec/mipsisa64r6el_unknown_linux_gnuabi64.rs index 58a814a759eb..4f41b8323a99 100644 --- a/src/librustc_target/spec/mipsisa64r6el_unknown_linux_gnuabi64.rs +++ b/src/librustc_target/spec/mipsisa64r6el_unknown_linux_gnuabi64.rs @@ -17,6 +17,7 @@ pub fn target() -> TargetResult { cpu: "mips64r6".to_string(), features: "+mips64r6".to_string(), max_atomic_width: Some(64), + target_mcount: "_mcount".to_string(), ..super::linux_base::opts() }, diff --git a/src/librustc_target/spec/mod.rs b/src/librustc_target/spec/mod.rs index fdb1db645c3c..7654d3932e33 100644 --- a/src/librustc_target/spec/mod.rs +++ b/src/librustc_target/spec/mod.rs @@ -759,7 +759,10 @@ pub struct TargetOptions { /// to opt out. The default is "aliases". /// /// Workaround for: https://github.com/rust-lang/rust/issues/57356 - pub merge_functions: MergeFunctions + pub merge_functions: MergeFunctions, + + /// Use platform dependent mcount function + pub target_mcount: String } impl Default for TargetOptions { @@ -843,6 +846,7 @@ impl Default for TargetOptions { simd_types_indirect: true, override_export_symbols: None, merge_functions: MergeFunctions::Aliases, + target_mcount: "mcount".to_string(), } } } @@ -1148,6 +1152,7 @@ impl Target { key!(simd_types_indirect, bool); key!(override_export_symbols, opt_list); key!(merge_functions, MergeFunctions)?; + key!(target_mcount); if let Some(array) = obj.find("abi-blacklist").and_then(Json::as_array) { for name in array.iter().filter_map(|abi| abi.as_string()) { @@ -1362,6 +1367,7 @@ impl ToJson for Target { target_option_val!(simd_types_indirect); target_option_val!(override_export_symbols); target_option_val!(merge_functions); + target_option_val!(target_mcount); if default.abi_blacklist != self.options.abi_blacklist { d.insert("abi-blacklist".to_string(), self.options.abi_blacklist.iter() diff --git a/src/librustc_target/spec/powerpc64_unknown_freebsd.rs b/src/librustc_target/spec/powerpc64_unknown_freebsd.rs index 360876b9ff55..fc881db6b090 100644 --- a/src/librustc_target/spec/powerpc64_unknown_freebsd.rs +++ b/src/librustc_target/spec/powerpc64_unknown_freebsd.rs @@ -1,4 +1,4 @@ -use crate::spec::{LinkerFlavor, Target, TargetResult}; +use crate::spec::{LinkerFlavor, Target, TargetOptions, TargetResult}; pub fn target() -> TargetResult { let mut base = super::freebsd_base::opts(); @@ -17,6 +17,9 @@ pub fn target() -> TargetResult { target_env: String::new(), target_vendor: "unknown".to_string(), linker_flavor: LinkerFlavor::Gcc, - options: base, + options: TargetOptions { + target_mcount: "_mcount".to_string(), + .. base + }, }) } diff --git a/src/librustc_target/spec/powerpc64_unknown_linux_gnu.rs b/src/librustc_target/spec/powerpc64_unknown_linux_gnu.rs index c16db7583f32..89e68ab30620 100644 --- a/src/librustc_target/spec/powerpc64_unknown_linux_gnu.rs +++ b/src/librustc_target/spec/powerpc64_unknown_linux_gnu.rs @@ -1,4 +1,4 @@ -use crate::spec::{LinkerFlavor, Target, TargetResult, RelroLevel}; +use crate::spec::{LinkerFlavor, Target, TargetOptions, TargetResult, RelroLevel}; pub fn target() -> TargetResult { let mut base = super::linux_base::opts(); @@ -21,6 +21,9 @@ pub fn target() -> TargetResult { target_env: "gnu".to_string(), target_vendor: "unknown".to_string(), linker_flavor: LinkerFlavor::Gcc, - options: base, + options: TargetOptions { + target_mcount: "_mcount".to_string(), + .. base + }, }) } diff --git a/src/librustc_target/spec/powerpc64_unknown_linux_musl.rs b/src/librustc_target/spec/powerpc64_unknown_linux_musl.rs index ac0b7431f91a..be91dc44b935 100644 --- a/src/librustc_target/spec/powerpc64_unknown_linux_musl.rs +++ b/src/librustc_target/spec/powerpc64_unknown_linux_musl.rs @@ -1,4 +1,4 @@ -use crate::spec::{LinkerFlavor, Target, TargetResult}; +use crate::spec::{LinkerFlavor, Target, TargetOptions, TargetResult}; pub fn target() -> TargetResult { let mut base = super::linux_musl_base::opts(); @@ -17,6 +17,9 @@ pub fn target() -> TargetResult { target_env: "musl".to_string(), target_vendor: "unknown".to_string(), linker_flavor: LinkerFlavor::Gcc, - options: base, + options: TargetOptions { + target_mcount: "_mcount".to_string(), + .. base + }, }) } diff --git a/src/librustc_target/spec/powerpc64le_unknown_linux_gnu.rs b/src/librustc_target/spec/powerpc64le_unknown_linux_gnu.rs index 038b925a2869..784e3b090943 100644 --- a/src/librustc_target/spec/powerpc64le_unknown_linux_gnu.rs +++ b/src/librustc_target/spec/powerpc64le_unknown_linux_gnu.rs @@ -1,4 +1,4 @@ -use crate::spec::{LinkerFlavor, Target, TargetResult}; +use crate::spec::{LinkerFlavor, Target, TargetOptions, TargetResult}; pub fn target() -> TargetResult { let mut base = super::linux_base::opts(); @@ -17,6 +17,9 @@ pub fn target() -> TargetResult { target_env: "gnu".to_string(), target_vendor: "unknown".to_string(), linker_flavor: LinkerFlavor::Gcc, - options: base, + options: TargetOptions { + target_mcount: "_mcount".to_string(), + .. base + }, }) } diff --git a/src/librustc_target/spec/powerpc64le_unknown_linux_musl.rs b/src/librustc_target/spec/powerpc64le_unknown_linux_musl.rs index 57103345f0a0..a3cf47fc5e08 100644 --- a/src/librustc_target/spec/powerpc64le_unknown_linux_musl.rs +++ b/src/librustc_target/spec/powerpc64le_unknown_linux_musl.rs @@ -1,4 +1,4 @@ -use crate::spec::{LinkerFlavor, Target, TargetResult}; +use crate::spec::{LinkerFlavor, Target, TargetOptions, TargetResult}; pub fn target() -> TargetResult { let mut base = super::linux_musl_base::opts(); @@ -17,6 +17,9 @@ pub fn target() -> TargetResult { target_env: "musl".to_string(), target_vendor: "unknown".to_string(), linker_flavor: LinkerFlavor::Gcc, - options: base, + options: TargetOptions { + target_mcount: "_mcount".to_string(), + .. base + }, }) } diff --git a/src/librustc_target/spec/powerpc_unknown_linux_gnu.rs b/src/librustc_target/spec/powerpc_unknown_linux_gnu.rs index 38a801d5ab50..ff52fbc179b1 100644 --- a/src/librustc_target/spec/powerpc_unknown_linux_gnu.rs +++ b/src/librustc_target/spec/powerpc_unknown_linux_gnu.rs @@ -1,4 +1,4 @@ -use crate::spec::{LinkerFlavor, Target, TargetResult}; +use crate::spec::{LinkerFlavor, Target, TargetOptions, TargetResult}; pub fn target() -> TargetResult { let mut base = super::linux_base::opts(); @@ -16,6 +16,9 @@ pub fn target() -> TargetResult { target_env: "gnu".to_string(), target_vendor: "unknown".to_string(), linker_flavor: LinkerFlavor::Gcc, - options: base, + options: TargetOptions { + target_mcount: "_mcount".to_string(), + .. base + }, }) } diff --git a/src/librustc_target/spec/powerpc_unknown_linux_gnuspe.rs b/src/librustc_target/spec/powerpc_unknown_linux_gnuspe.rs index 675b2c749d64..1868c42be39d 100644 --- a/src/librustc_target/spec/powerpc_unknown_linux_gnuspe.rs +++ b/src/librustc_target/spec/powerpc_unknown_linux_gnuspe.rs @@ -1,4 +1,4 @@ -use crate::spec::{LinkerFlavor, Target, TargetResult}; +use crate::spec::{LinkerFlavor, Target, TargetOptions, TargetResult}; pub fn target() -> TargetResult { let mut base = super::linux_base::opts(); @@ -16,6 +16,9 @@ pub fn target() -> TargetResult { target_env: "gnu".to_string(), target_vendor: "unknown".to_string(), linker_flavor: LinkerFlavor::Gcc, - options: base, + options: TargetOptions { + target_mcount: "_mcount".to_string(), + .. base + }, }) } diff --git a/src/librustc_target/spec/powerpc_unknown_linux_musl.rs b/src/librustc_target/spec/powerpc_unknown_linux_musl.rs index 240443aa98db..1ad2201092c3 100644 --- a/src/librustc_target/spec/powerpc_unknown_linux_musl.rs +++ b/src/librustc_target/spec/powerpc_unknown_linux_musl.rs @@ -1,4 +1,4 @@ -use crate::spec::{LinkerFlavor, Target, TargetResult}; +use crate::spec::{LinkerFlavor, Target, TargetOptions, TargetResult}; pub fn target() -> TargetResult { let mut base = super::linux_musl_base::opts(); @@ -16,6 +16,9 @@ pub fn target() -> TargetResult { target_env: "musl".to_string(), target_vendor: "unknown".to_string(), linker_flavor: LinkerFlavor::Gcc, - options: base, + options: TargetOptions { + target_mcount: "_mcount".to_string(), + .. base + }, }) } diff --git a/src/librustc_target/spec/powerpc_unknown_netbsd.rs b/src/librustc_target/spec/powerpc_unknown_netbsd.rs index 10e7089cf1c4..6cc3a6c2ef3f 100644 --- a/src/librustc_target/spec/powerpc_unknown_netbsd.rs +++ b/src/librustc_target/spec/powerpc_unknown_netbsd.rs @@ -1,4 +1,4 @@ -use crate::spec::{LinkerFlavor, Target, TargetResult}; +use crate::spec::{LinkerFlavor, Target, TargetOptions, TargetResult}; pub fn target() -> TargetResult { let mut base = super::netbsd_base::opts(); @@ -16,6 +16,9 @@ pub fn target() -> TargetResult { target_env: String::new(), target_vendor: "unknown".to_string(), linker_flavor: LinkerFlavor::Gcc, - options: base, + options: TargetOptions { + target_mcount: "__mcount".to_string(), + .. base + }, }) } diff --git a/src/librustc_target/spec/sparc64_unknown_netbsd.rs b/src/librustc_target/spec/sparc64_unknown_netbsd.rs index 78d53e69e8b5..09d1debef41e 100644 --- a/src/librustc_target/spec/sparc64_unknown_netbsd.rs +++ b/src/librustc_target/spec/sparc64_unknown_netbsd.rs @@ -1,4 +1,4 @@ -use crate::spec::{LinkerFlavor, Target, TargetResult}; +use crate::spec::{LinkerFlavor, Target, TargetOptions, TargetResult}; pub fn target() -> TargetResult { let mut base = super::netbsd_base::opts(); @@ -17,6 +17,9 @@ pub fn target() -> TargetResult { target_env: String::new(), target_vendor: "unknown".to_string(), linker_flavor: LinkerFlavor::Gcc, - options: base, + options: TargetOptions { + target_mcount: "__mcount".to_string(), + .. base + }, }) } diff --git a/src/librustc_target/spec/x86_64_apple_darwin.rs b/src/librustc_target/spec/x86_64_apple_darwin.rs index 0911ce06c13d..fecc20c01c5e 100644 --- a/src/librustc_target/spec/x86_64_apple_darwin.rs +++ b/src/librustc_target/spec/x86_64_apple_darwin.rs @@ -1,4 +1,4 @@ -use crate::spec::{LinkerFlavor, Target, TargetResult}; +use crate::spec::{LinkerFlavor, Target, TargetOptions, TargetResult}; pub fn target() -> TargetResult { let mut base = super::apple_base::opts(); @@ -19,6 +19,9 @@ pub fn target() -> TargetResult { target_env: String::new(), target_vendor: "apple".to_string(), linker_flavor: LinkerFlavor::Gcc, - options: base, + options: TargetOptions { + target_mcount: "\01mcount".to_string(), + .. base + }, }) } diff --git a/src/librustc_target/spec/x86_64_rumprun_netbsd.rs b/src/librustc_target/spec/x86_64_rumprun_netbsd.rs index a2c706c4c723..f6861f2a6872 100644 --- a/src/librustc_target/spec/x86_64_rumprun_netbsd.rs +++ b/src/librustc_target/spec/x86_64_rumprun_netbsd.rs @@ -1,4 +1,4 @@ -use crate::spec::{LinkerFlavor, Target, TargetResult}; +use crate::spec::{LinkerFlavor, Target, TargetOptions, TargetResult}; pub fn target() -> TargetResult { let mut base = super::netbsd_base::opts(); @@ -24,6 +24,9 @@ pub fn target() -> TargetResult { target_env: String::new(), target_vendor: "rumprun".to_string(), linker_flavor: LinkerFlavor::Gcc, - options: base, + options: TargetOptions { + target_mcount: "__mcount".to_string(), + .. base + }, }) } diff --git a/src/librustc_target/spec/x86_64_unknown_netbsd.rs b/src/librustc_target/spec/x86_64_unknown_netbsd.rs index ffc4f1d5c49b..6f4ab4995b5d 100644 --- a/src/librustc_target/spec/x86_64_unknown_netbsd.rs +++ b/src/librustc_target/spec/x86_64_unknown_netbsd.rs @@ -1,4 +1,4 @@ -use crate::spec::{LinkerFlavor, Target, TargetResult}; +use crate::spec::{LinkerFlavor, Target, TargetOptions, TargetResult}; pub fn target() -> TargetResult { let mut base = super::netbsd_base::opts(); @@ -18,6 +18,9 @@ pub fn target() -> TargetResult { target_env: String::new(), target_vendor: "unknown".to_string(), linker_flavor: LinkerFlavor::Gcc, - options: base, + options: TargetOptions { + target_mcount: "__mcount".to_string(), + .. base + }, }) } From 3281248b88262687fcf34b2404cece968bcf837c Mon Sep 17 00:00:00 2001 From: Yuki OKUSHI Date: Sat, 30 Mar 2019 18:50:34 +0900 Subject: [PATCH 04/30] Use target_mcount --- src/librustc_codegen_llvm/attributes.rs | 28 ++++--------------------- 1 file changed, 4 insertions(+), 24 deletions(-) diff --git a/src/librustc_codegen_llvm/attributes.rs b/src/librustc_codegen_llvm/attributes.rs index e765b986d623..9aaad60cd6d3 100644 --- a/src/librustc_codegen_llvm/attributes.rs +++ b/src/librustc_codegen_llvm/attributes.rs @@ -80,30 +80,10 @@ pub fn set_instrument_function(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) { // The function name varies on platforms. // See test/CodeGen/mcount.c in clang. - let mcount_name = if cfg!(target_os = "netbsd") { - const_cstr!("__mcount") - } else if cfg!(any( - target_arch = "mips", target_arch = "mips64", - target_arch = "powerpc", target_arch = "powerpc64")) { - const_cstr!("_mcount") - } else if cfg!(target_os = "darwin") { - const_cstr!("\01mcount") - } else if cfg!(target_arch = "aarch64") - && (cfg!(target_os = "linux") - || (cfg!(target_os = "unknown") && cfg!(target_env = "gnu"))) - { - const_cstr!("\01_mcount") - } else if cfg!(target_arch = "arm") - && cfg!(any(target_os = "linux", target_os = "unknown")) - { - if cfg!(target_env = "gnu") { - const_cstr!("\01__gnu_mcount_nc") - } else { - const_cstr!("\01mcount") - } - } else { - const_cstr!("mcount") - }; + use std::ffi::CStr; + let target_mcount = format!("{}{}", + &cx.sess().target.target.options.target_mcount, "\0"); + let mcount_name = CStr::from_bytes_with_nul(target_mcount.as_bytes()).unwrap(); llvm::AddFunctionAttrStringValue( llfn, llvm::AttributePlace::Function, From 77774e4e960e6b65ddb95b087772c6d2ad6c3009 Mon Sep 17 00:00:00 2001 From: Yuki OKUSHI Date: Sat, 30 Mar 2019 21:37:02 +0900 Subject: [PATCH 05/30] Use CString --- src/librustc_codegen_llvm/attributes.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/librustc_codegen_llvm/attributes.rs b/src/librustc_codegen_llvm/attributes.rs index 9aaad60cd6d3..77fa34e74dd7 100644 --- a/src/librustc_codegen_llvm/attributes.rs +++ b/src/librustc_codegen_llvm/attributes.rs @@ -80,14 +80,12 @@ pub fn set_instrument_function(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) { // The function name varies on platforms. // See test/CodeGen/mcount.c in clang. - use std::ffi::CStr; - let target_mcount = format!("{}{}", - &cx.sess().target.target.options.target_mcount, "\0"); - let mcount_name = CStr::from_bytes_with_nul(target_mcount.as_bytes()).unwrap(); + let mcount_name = CString::new( + cx.sess().target.target.options.target_mcount.as_str().as_bytes()).unwrap(); llvm::AddFunctionAttrStringValue( llfn, llvm::AttributePlace::Function, - const_cstr!("instrument-function-entry-inlined"), mcount_name); + const_cstr!("instrument-function-entry-inlined"), &mcount_name); } } From 29d68edc6e1d4f5e6d1599202e3af38080840977 Mon Sep 17 00:00:00 2001 From: Fabian Drinck Date: Fri, 22 Feb 2019 15:07:18 +0100 Subject: [PATCH 06/30] Add lint for redundant imports Co-authored-by: Stephan Schauerte --- src/librustc/lint/builtin.rs | 6 ++ src/librustc_resolve/resolve_imports.rs | 93 ++++++++++++++++++++++++- src/test/ui/lint/use-redundant.rs | 17 +++++ src/test/ui/lint/use-redundant.stderr | 14 ++++ 4 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/lint/use-redundant.rs create mode 100644 src/test/ui/lint/use-redundant.stderr diff --git a/src/librustc/lint/builtin.rs b/src/librustc/lint/builtin.rs index 10a5c1479fa6..615bc0af37a1 100644 --- a/src/librustc/lint/builtin.rs +++ b/src/librustc/lint/builtin.rs @@ -392,6 +392,12 @@ declare_lint! { "nested occurrence of `impl Trait` type" } +declare_lint! { + pub REDUNDANT_IMPORT, + Warn, + "redundant import" +} + /// Does nothing as a lint pass, but registers some `Lint`s /// that are used by other parts of the compiler. #[derive(Copy, Clone)] diff --git a/src/librustc_resolve/resolve_imports.rs b/src/librustc_resolve/resolve_imports.rs index c2d2bd753c82..b40b68b08534 100644 --- a/src/librustc_resolve/resolve_imports.rs +++ b/src/librustc_resolve/resolve_imports.rs @@ -7,6 +7,7 @@ use crate::{NameBinding, NameBindingKind, ToNameBinding, PathResult, PrivacyErro use crate::{Resolver, Segment}; use crate::{names_to_string, module_to_string}; use crate::{resolve_error, ResolutionError, Suggestion}; +use crate::ModuleKind; use crate::macros::ParentScope; use errors::Applicability; @@ -14,7 +15,11 @@ use errors::Applicability; use rustc_data_structures::ptr_key::PtrKey; use rustc::ty; use rustc::lint::builtin::BuiltinLintDiagnostics; -use rustc::lint::builtin::{DUPLICATE_MACRO_EXPORTS, PUB_USE_OF_PRIVATE_EXTERN_CRATE}; +use rustc::lint::builtin::{ + DUPLICATE_MACRO_EXPORTS, + PUB_USE_OF_PRIVATE_EXTERN_CRATE, + REDUNDANT_IMPORT, +}; use rustc::hir::def_id::{CrateNum, DefId}; use rustc::hir::def::*; use rustc::session::DiagnosticMessageId; @@ -1227,10 +1232,96 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> { import[ns] = Some(PathResolution::new(def)); }); + self.check_for_redundant_imports( + ident, + directive, + source_bindings, + target_bindings, + target, + ); + debug!("(resolving single import) successfully resolved import"); None } + fn check_for_redundant_imports( + &mut self, + ident: Ident, + directive: &'b ImportDirective<'b>, + source_bindings: &PerNS, Determinacy>>>, + target_bindings: &PerNS>>>, + target: Ident, + ) { + // Check if we are at the root of a macro expansion and skip if we are. + if directive.parent_scope.expansion != Mark::root() { + return; + } + + if let ModuleKind::Def(_, _) = directive.parent_scope.module.kind { + return; + } + + let mut is_redundant = PerNS { + value_ns: None, + type_ns: None, + macro_ns: None, + }; + + let mut redundant_span = PerNS { + value_ns: None, + type_ns: None, + macro_ns: None, + }; + + self.per_ns(|this, ns| if let Some(binding) = source_bindings[ns].get().ok() { + if binding.def() == Def::Err { + return; + } + + let orig_blacklisted_binding = mem::replace( + &mut this.blacklisted_binding, + target_bindings[ns].get() + ); + + match this.early_resolve_ident_in_lexical_scope( + target, + ScopeSet::Import(ns), + &directive.parent_scope, + false, + false, + directive.span, + ) { + Ok(other_binding) => { + is_redundant[ns] = Some(binding.def() == other_binding.def()); + redundant_span[ns] = Some(other_binding.span); + } + Err(_) => is_redundant[ns] = Some(false) + } + + this.blacklisted_binding = orig_blacklisted_binding; + }); + + if !is_redundant.is_empty() && + is_redundant.present_items().all(|is_redundant| is_redundant) + { + self.session.buffer_lint( + REDUNDANT_IMPORT, + directive.id, + directive.span, + &format!("the item `{}` is imported redundantly", ident), + ); + + for span in redundant_span.present_items() { + self.session.buffer_lint( + REDUNDANT_IMPORT, + directive.id, + span, + "another import" + ); + } + } + } + fn resolve_glob_import(&mut self, directive: &'b ImportDirective<'b>) { let module = match directive.imported_module.get().unwrap() { ModuleOrUniformRoot::Module(module) => module, diff --git a/src/test/ui/lint/use-redundant.rs b/src/test/ui/lint/use-redundant.rs new file mode 100644 index 000000000000..50d4d30625a6 --- /dev/null +++ b/src/test/ui/lint/use-redundant.rs @@ -0,0 +1,17 @@ +// compile-pass + +use crate::foo::Bar; //~ WARNING first import + +mod foo { + pub type Bar = i32; +} + +fn baz() -> Bar { + 3 +} + +fn main() { + use crate::foo::Bar; //~ WARNING redundant import + let _a: Bar = 3; + baz(); +} diff --git a/src/test/ui/lint/use-redundant.stderr b/src/test/ui/lint/use-redundant.stderr new file mode 100644 index 000000000000..6a6becc5e615 --- /dev/null +++ b/src/test/ui/lint/use-redundant.stderr @@ -0,0 +1,14 @@ +warning: the item `Bar` is imported redundantly + --> $DIR/use-redundant.rs:14:9 + | +LL | use crate::foo::Bar; //~ WARNING redundant import + | ^^^^^^^^^^^^^^^ + | + = note: #[warn(redundant_import)] on by default + +warning: another import + --> $DIR/use-redundant.rs:3:5 + | +LL | use crate::foo::Bar; //~ WARNING first import + | ^^^^^^^^^^^^^^^ + From 541c4999a9e878f26ada62bc9a82f088a8f2fad2 Mon Sep 17 00:00:00 2001 From: Fabian Drinck Date: Sun, 10 Mar 2019 12:52:30 +0100 Subject: [PATCH 07/30] Remove redundant imports --- src/librustc/ty/query/on_disk_cache.rs | 1 - src/librustc_codegen_llvm/context.rs | 1 - src/librustc_codegen_ssa/mir/rvalue.rs | 1 - src/librustc_errors/lib.rs | 2 +- src/librustc_interface/profile/mod.rs | 1 - src/librustc_mir/hair/pattern/mod.rs | 1 - src/librustc_mir/interpret/operator.rs | 2 -- src/librustc_resolve/lib.rs | 1 - src/librustc_traits/lowering/environment.rs | 2 +- src/librustc_typeck/check/wfcheck.rs | 5 ----- 10 files changed, 2 insertions(+), 15 deletions(-) diff --git a/src/librustc/ty/query/on_disk_cache.rs b/src/librustc/ty/query/on_disk_cache.rs index 1b4c36b8b06c..05d80f9203f1 100644 --- a/src/librustc/ty/query/on_disk_cache.rs +++ b/src/librustc/ty/query/on_disk_cache.rs @@ -777,7 +777,6 @@ impl<'enc, 'a, 'tcx, E> CacheEncoder<'enc, 'a, 'tcx, E> value: &V) -> Result<(), E::Error> { - use crate::ty::codec::TyEncoder; let start_pos = self.position(); tag.encode(self)?; diff --git a/src/librustc_codegen_llvm/context.rs b/src/librustc_codegen_llvm/context.rs index fc79e868fb4b..f6956bd5736e 100644 --- a/src/librustc_codegen_llvm/context.rs +++ b/src/librustc_codegen_llvm/context.rs @@ -372,7 +372,6 @@ impl MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> { // Returns a Value of the "eh_unwind_resume" lang item if one is defined, // otherwise declares it as an external function. fn eh_unwind_resume(&self) -> &'ll Value { - use crate::attributes; let unwresume = &self.eh_unwind_resume; if let Some(llfn) = unwresume.get() { return llfn; diff --git a/src/librustc_codegen_ssa/mir/rvalue.rs b/src/librustc_codegen_ssa/mir/rvalue.rs index 7a31c5b3950e..fc4f6b832478 100644 --- a/src/librustc_codegen_ssa/mir/rvalue.rs +++ b/src/librustc_codegen_ssa/mir/rvalue.rs @@ -732,7 +732,6 @@ fn cast_int_to_float<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>( // All inputs greater or equal to (f32::MAX + 0.5 ULP) are rounded to infinity, // and for everything else LLVM's uitofp works just fine. use rustc_apfloat::ieee::Single; - use rustc_apfloat::Float; const MAX_F32_PLUS_HALF_ULP: u128 = ((1 << (Single::PRECISION + 1)) - 1) << (Single::MAX_EXP - Single::PRECISION as i16); let max = bx.cx().const_uint_big(int_ty, MAX_F32_PLUS_HALF_ULP); diff --git a/src/librustc_errors/lib.rs b/src/librustc_errors/lib.rs index 6b4b437930d2..ebbc5a3d3a34 100644 --- a/src/librustc_errors/lib.rs +++ b/src/librustc_errors/lib.rs @@ -155,7 +155,7 @@ impl CodeSuggestion { /// Returns the assembled code suggestions and whether they should be shown with an underline. pub fn splice_lines(&self, cm: &SourceMapperDyn) -> Vec<(String, Vec)> { - use syntax_pos::{CharPos, Loc, Pos}; + use syntax_pos::{CharPos, Pos}; fn push_trailing(buf: &mut String, line_opt: Option<&Cow<'_, str>>, diff --git a/src/librustc_interface/profile/mod.rs b/src/librustc_interface/profile/mod.rs index d0c8dff20708..2e71d46f4154 100644 --- a/src/librustc_interface/profile/mod.rs +++ b/src/librustc_interface/profile/mod.rs @@ -62,7 +62,6 @@ fn total_duration(traces: &[trace::Rec]) -> Duration { fn profile_queries_thread(r: Receiver) { use self::trace::*; use std::fs::File; - use std::time::{Instant}; let mut profq_msgs: Vec = vec![]; let mut frame: StackFrame = StackFrame { parse_st: ParseState::Clear, traces: vec![] }; diff --git a/src/librustc_mir/hair/pattern/mod.rs b/src/librustc_mir/hair/pattern/mod.rs index 6c532fce57f0..622cf00ed86e 100644 --- a/src/librustc_mir/hair/pattern/mod.rs +++ b/src/librustc_mir/hair/pattern/mod.rs @@ -427,7 +427,6 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> { let mut kind = match (lo, hi) { (PatternKind::Constant { value: lo }, PatternKind::Constant { value: hi }) => { - use std::cmp::Ordering; let cmp = compare_const_vals( self.tcx, lo, diff --git a/src/librustc_mir/interpret/operator.rs b/src/librustc_mir/interpret/operator.rs index ca93007788e0..488f81d8f740 100644 --- a/src/librustc_mir/interpret/operator.rs +++ b/src/librustc_mir/interpret/operator.rs @@ -331,8 +331,6 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> InterpretCx<'a, 'mir, 'tcx, M> val: ImmTy<'tcx, M::PointerTag>, ) -> EvalResult<'tcx, Scalar> { use rustc::mir::UnOp::*; - use rustc_apfloat::ieee::{Single, Double}; - use rustc_apfloat::Float; let layout = val.layout; let val = val.to_scalar()?; diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index ab3d87fafdac..3703ea08b625 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -1738,7 +1738,6 @@ impl<'a> Resolver<'a> { /// just that an error occurred. pub fn resolve_str_path_error(&mut self, span: Span, path_str: &str, is_value: bool) -> Result { - use std::iter; let mut errored = false; let path = if path_str.starts_with("::") { diff --git a/src/librustc_traits/lowering/environment.rs b/src/librustc_traits/lowering/environment.rs index 3570cb102460..eb47892ea410 100644 --- a/src/librustc_traits/lowering/environment.rs +++ b/src/librustc_traits/lowering/environment.rs @@ -164,7 +164,7 @@ crate fn environment<'a, 'tcx>( tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId ) -> Environment<'tcx> { - use super::{Lower, IntoFromEnvGoal}; + use super::IntoFromEnvGoal; use rustc::hir::{Node, TraitItemKind, ImplItemKind, ItemKind, ForeignItemKind}; debug!("environment(def_id = {:?})", def_id); diff --git a/src/librustc_typeck/check/wfcheck.rs b/src/librustc_typeck/check/wfcheck.rs index 920270b5473c..3579810b8d75 100644 --- a/src/librustc_typeck/check/wfcheck.rs +++ b/src/librustc_typeck/check/wfcheck.rs @@ -420,9 +420,6 @@ fn check_where_clauses<'a, 'gcx, 'fcx, 'tcx>( def_id: DefId, return_ty: Option>, ) { - use ty::subst::Subst; - use rustc::ty::TypeFoldable; - let predicates = fcx.tcx.predicates_of(def_id); let generics = tcx.generics_of(def_id); @@ -1010,8 +1007,6 @@ fn check_false_global_bounds<'a, 'gcx, 'tcx>( span: Span, id: hir::HirId) { - use rustc::ty::TypeFoldable; - let empty_env = ty::ParamEnv::empty(); let def_id = fcx.tcx.hir().local_def_id_from_hir_id(id); From 2cebbe25995f308fe6ed72a246f9d2ea633addf9 Mon Sep 17 00:00:00 2001 From: Fabian Drinck Date: Sat, 16 Mar 2019 16:49:13 +0100 Subject: [PATCH 08/30] Edit comments --- src/librustc_resolve/resolve_imports.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/librustc_resolve/resolve_imports.rs b/src/librustc_resolve/resolve_imports.rs index b40b68b08534..534e5e0ff45a 100644 --- a/src/librustc_resolve/resolve_imports.rs +++ b/src/librustc_resolve/resolve_imports.rs @@ -1252,11 +1252,13 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> { target_bindings: &PerNS>>>, target: Ident, ) { - // Check if we are at the root of a macro expansion and skip if we are. + // Skip if the import was produced by a macro. if directive.parent_scope.expansion != Mark::root() { return; } + // Skip if we are inside a named module (in contrast to an anonymous + // module defined by a block). if let ModuleKind::Def(_, _) = directive.parent_scope.module.kind { return; } From 2245d10fac9b37c55a286f48d8560fc9537cbc3e Mon Sep 17 00:00:00 2001 From: Fabian Drinck Date: Sat, 16 Mar 2019 17:38:12 +0100 Subject: [PATCH 09/30] Improve warning --- src/librustc/lint/builtin.rs | 9 +++++++++ src/librustc_resolve/resolve_imports.rs | 15 +++++---------- src/test/ui/lint/use-redundant.stderr | 11 ++++------- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/librustc/lint/builtin.rs b/src/librustc/lint/builtin.rs index 615bc0af37a1..002ee5994005 100644 --- a/src/librustc/lint/builtin.rs +++ b/src/librustc/lint/builtin.rs @@ -489,6 +489,7 @@ pub enum BuiltinLintDiagnostics { UnknownCrateTypes(Span, String, String), UnusedImports(String, Vec<(Span, String)>), NestedImplTrait { outer_impl_trait_span: Span, inner_impl_trait_span: Span }, + RedundantImport(Vec, ast::Ident), } impl BuiltinLintDiagnostics { @@ -585,6 +586,14 @@ impl BuiltinLintDiagnostics { db.span_label(outer_impl_trait_span, "outer `impl Trait`"); db.span_label(inner_impl_trait_span, "nested `impl Trait` here"); } + BuiltinLintDiagnostics::RedundantImport(spans, ident) => { + for span in spans { + db.span_label( + span, + format!("the item `{}` was already imported here", ident) + ); + } + } } } } diff --git a/src/librustc_resolve/resolve_imports.rs b/src/librustc_resolve/resolve_imports.rs index 534e5e0ff45a..cd018d8eb9e2 100644 --- a/src/librustc_resolve/resolve_imports.rs +++ b/src/librustc_resolve/resolve_imports.rs @@ -1306,21 +1306,16 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> { if !is_redundant.is_empty() && is_redundant.present_items().all(|is_redundant| is_redundant) { - self.session.buffer_lint( + self.session.buffer_lint_with_diagnostic( REDUNDANT_IMPORT, directive.id, directive.span, &format!("the item `{}` is imported redundantly", ident), + BuiltinLintDiagnostics::RedundantImport( + redundant_span.present_items().collect(), + ident, + ), ); - - for span in redundant_span.present_items() { - self.session.buffer_lint( - REDUNDANT_IMPORT, - directive.id, - span, - "another import" - ); - } } } diff --git a/src/test/ui/lint/use-redundant.stderr b/src/test/ui/lint/use-redundant.stderr index 6a6becc5e615..c843ed160df0 100644 --- a/src/test/ui/lint/use-redundant.stderr +++ b/src/test/ui/lint/use-redundant.stderr @@ -1,14 +1,11 @@ warning: the item `Bar` is imported redundantly --> $DIR/use-redundant.rs:14:9 | -LL | use crate::foo::Bar; //~ WARNING redundant import +LL | use crate::foo::Bar; + | --------------- the item `Bar` was already imported here +... +LL | use crate::foo::Bar; | ^^^^^^^^^^^^^^^ | = note: #[warn(redundant_import)] on by default -warning: another import - --> $DIR/use-redundant.rs:3:5 - | -LL | use crate::foo::Bar; //~ WARNING first import - | ^^^^^^^^^^^^^^^ - From d04e83fe2c51acd2c8e89c03d15deb7e1896bb8b Mon Sep 17 00:00:00 2001 From: Fabian Drinck Date: Sat, 16 Mar 2019 18:08:51 +0100 Subject: [PATCH 10/30] Bless tests --- src/test/ui/lint/lint-unused-imports.stderr | 23 ++++++++++ .../rust-2018/future-proofing-locals.stderr | 46 +++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/src/test/ui/lint/lint-unused-imports.stderr b/src/test/ui/lint/lint-unused-imports.stderr index f9a54f477f99..bf194696f618 100644 --- a/src/test/ui/lint/lint-unused-imports.stderr +++ b/src/test/ui/lint/lint-unused-imports.stderr @@ -34,12 +34,35 @@ error: unused import: `foo::Square` LL | use foo::Square; | ^^^^^^^^^^^ +warning: the item `g` is imported redundantly + --> $DIR/lint-unused-imports.rs:68:9 + | +LL | / fn g() { +LL | | use self::g; + | | ^^^^^^^ +LL | | fn f() { +LL | | self::g(); +LL | | } +LL | | } + | |_- the item `g` was already imported here + | + = note: #[warn(redundant_import)] on by default + error: unused import: `self::g` --> $DIR/lint-unused-imports.rs:68:9 | LL | use self::g; | ^^^^^^^ +warning: the item `foo` is imported redundantly + --> $DIR/lint-unused-imports.rs:77:9 + | +LL | use test2::{foo, bar}; + | --- the item `foo` was already imported here +... +LL | use test2::foo; + | ^^^^^^^^^^ + error: unused import: `test2::foo` --> $DIR/lint-unused-imports.rs:77:9 | diff --git a/src/test/ui/rust-2018/future-proofing-locals.stderr b/src/test/ui/rust-2018/future-proofing-locals.stderr index 4d666d22afed..fa8333b5d211 100644 --- a/src/test/ui/rust-2018/future-proofing-locals.stderr +++ b/src/test/ui/rust-2018/future-proofing-locals.stderr @@ -52,5 +52,51 @@ error: imports cannot refer to local variables LL | use {T as _, x}; | ^ +warning: the item `T` is imported redundantly + --> $DIR/future-proofing-locals.rs:19:9 + | +LL | / mod T { +LL | | pub struct U; +LL | | } + | |_- the item `T` was already imported here +... +LL | use T; + | ^ + | + = note: #[warn(redundant_import)] on by default + +warning: the item `x` is imported redundantly + --> $DIR/future-proofing-locals.rs:31:9 + | +LL | / mod x { +LL | | pub struct y; +LL | | } + | |_- the item `x` was already imported here +... +LL | use x; + | ^ + +warning: the item `x` is imported redundantly + --> $DIR/future-proofing-locals.rs:37:17 + | +LL | / mod x { +LL | | pub struct y; +LL | | } + | |_- the item `x` was already imported here +... +LL | use x; + | ^ + +warning: the item `x` is imported redundantly + --> $DIR/future-proofing-locals.rs:45:18 + | +LL | / mod x { +LL | | pub struct y; +LL | | } + | |_- the item `x` was already imported here +... +LL | use {T as _, x}; + | ^ + error: aborting due to 9 previous errors From 8919894c513a8f8eadacd9555afee7a6d095665c Mon Sep 17 00:00:00 2001 From: Fabian Drinck Date: Sat, 16 Mar 2019 19:06:22 +0100 Subject: [PATCH 11/30] Distinguish between imported and defined items --- src/librustc/lint/builtin.rs | 7 ++++--- src/librustc_resolve/resolve_imports.rs | 3 ++- src/test/ui/lint/use-redundant.stderr | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/librustc/lint/builtin.rs b/src/librustc/lint/builtin.rs index 002ee5994005..462f0947338b 100644 --- a/src/librustc/lint/builtin.rs +++ b/src/librustc/lint/builtin.rs @@ -489,7 +489,7 @@ pub enum BuiltinLintDiagnostics { UnknownCrateTypes(Span, String, String), UnusedImports(String, Vec<(Span, String)>), NestedImplTrait { outer_impl_trait_span: Span, inner_impl_trait_span: Span }, - RedundantImport(Vec, ast::Ident), + RedundantImport(Vec<(Span, bool)>, ast::Ident), } impl BuiltinLintDiagnostics { @@ -587,10 +587,11 @@ impl BuiltinLintDiagnostics { db.span_label(inner_impl_trait_span, "nested `impl Trait` here"); } BuiltinLintDiagnostics::RedundantImport(spans, ident) => { - for span in spans { + for (span, is_imported) in spans { + let introduced = if is_imported { "imported" } else { "defined" }; db.span_label( span, - format!("the item `{}` was already imported here", ident) + format!("the item `{}` was {} here", ident, introduced) ); } } diff --git a/src/librustc_resolve/resolve_imports.rs b/src/librustc_resolve/resolve_imports.rs index cd018d8eb9e2..eacbf81347aa 100644 --- a/src/librustc_resolve/resolve_imports.rs +++ b/src/librustc_resolve/resolve_imports.rs @@ -1295,7 +1295,8 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> { ) { Ok(other_binding) => { is_redundant[ns] = Some(binding.def() == other_binding.def()); - redundant_span[ns] = Some(other_binding.span); + redundant_span[ns] = + Some((other_binding.span, other_binding.is_import())); } Err(_) => is_redundant[ns] = Some(false) } diff --git a/src/test/ui/lint/use-redundant.stderr b/src/test/ui/lint/use-redundant.stderr index c843ed160df0..b5000b22a1da 100644 --- a/src/test/ui/lint/use-redundant.stderr +++ b/src/test/ui/lint/use-redundant.stderr @@ -2,7 +2,7 @@ warning: the item `Bar` is imported redundantly --> $DIR/use-redundant.rs:14:9 | LL | use crate::foo::Bar; - | --------------- the item `Bar` was already imported here + | --------------- the item `Bar` was imported here ... LL | use crate::foo::Bar; | ^^^^^^^^^^^^^^^ From fef3f5c88d660277c85909af36edd3e121a0d576 Mon Sep 17 00:00:00 2001 From: Fabian Drinck Date: Sat, 16 Mar 2019 19:19:40 +0100 Subject: [PATCH 12/30] Remove redundant import --- src/librustdoc/html/render.rs | 2 -- src/librustdoc/test.rs | 3 +-- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 9220d2feed2d..f7e8cdeaeca8 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -1069,8 +1069,6 @@ themePicker.onblur = handleThemeButtonsBlur; } if cx.shared.include_sources { - use std::path::Component; - let mut hierarchy = Hierarchy::new(OsString::new()); for source in cx.shared.local_sources.iter() .filter_map(|p| p.0.strip_prefix(&cx.shared.src_root) diff --git a/src/librustdoc/test.rs b/src/librustdoc/test.rs index f1d4d8470b2a..0bbc7c5c4b22 100644 --- a/src/librustdoc/test.rs +++ b/src/librustdoc/test.rs @@ -371,8 +371,7 @@ pub fn make_test(s: &str, // Uses libsyntax to parse the doctest and find if there's a main fn and the extern // crate already is included. let (already_has_main, already_has_extern_crate, found_macro) = crate::syntax::with_globals(|| { - use crate::syntax::{ast, parse::{self, ParseSess}, source_map::FilePathMapping}; - use crate::syntax_pos::FileName; + use crate::syntax::{parse::{self, ParseSess}, source_map::FilePathMapping}; use errors::emitter::EmitterWriter; use errors::Handler; From f9272364bf443ec29cf9cc3548f37a7579b35bfd Mon Sep 17 00:00:00 2001 From: Fabian Drinck Date: Sat, 16 Mar 2019 19:21:03 +0100 Subject: [PATCH 13/30] Edit ui tests --- src/test/ui/lint/lint-unused-imports.rs | 1 + src/test/ui/rust-2018/future-proofing-locals.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/src/test/ui/lint/lint-unused-imports.rs b/src/test/ui/lint/lint-unused-imports.rs index 9c5b206203c1..c7ffdc270c94 100644 --- a/src/test/ui/lint/lint-unused-imports.rs +++ b/src/test/ui/lint/lint-unused-imports.rs @@ -1,5 +1,6 @@ #![deny(unused_imports)] #![allow(dead_code)] +#![allow(redundant_import)] use bar::c::cc as cal; diff --git a/src/test/ui/rust-2018/future-proofing-locals.rs b/src/test/ui/rust-2018/future-proofing-locals.rs index 1e53c2d1daca..df3badb94e5f 100644 --- a/src/test/ui/rust-2018/future-proofing-locals.rs +++ b/src/test/ui/rust-2018/future-proofing-locals.rs @@ -1,6 +1,7 @@ // edition:2018 #![allow(non_camel_case_types)] +#![allow(redundant_import)] mod T { pub struct U; From 6e7b45e12b76cad70262281d5fae7ad98032fafc Mon Sep 17 00:00:00 2001 From: Fabian Drinck Date: Sun, 17 Mar 2019 09:49:31 +0100 Subject: [PATCH 14/30] Replace REDUNDANT_IMPORT with UNUSED_IMPORTS --- src/librustc/lint/builtin.rs | 8 +-- src/librustc_resolve/resolve_imports.rs | 4 +- src/test/ui/lint/lint-unused-imports.rs | 1 - src/test/ui/lint/lint-unused-imports.stderr | 10 ++- .../ui/rust-2018/future-proofing-locals.rs | 2 +- .../rust-2018/future-proofing-locals.stderr | 64 +++---------------- 6 files changed, 17 insertions(+), 72 deletions(-) diff --git a/src/librustc/lint/builtin.rs b/src/librustc/lint/builtin.rs index 462f0947338b..6908674bc13a 100644 --- a/src/librustc/lint/builtin.rs +++ b/src/librustc/lint/builtin.rs @@ -392,12 +392,6 @@ declare_lint! { "nested occurrence of `impl Trait` type" } -declare_lint! { - pub REDUNDANT_IMPORT, - Warn, - "redundant import" -} - /// Does nothing as a lint pass, but registers some `Lint`s /// that are used by other parts of the compiler. #[derive(Copy, Clone)] @@ -591,7 +585,7 @@ impl BuiltinLintDiagnostics { let introduced = if is_imported { "imported" } else { "defined" }; db.span_label( span, - format!("the item `{}` was {} here", ident, introduced) + format!("the item `{}` was already {} here", ident, introduced) ); } } diff --git a/src/librustc_resolve/resolve_imports.rs b/src/librustc_resolve/resolve_imports.rs index eacbf81347aa..99e20c398d92 100644 --- a/src/librustc_resolve/resolve_imports.rs +++ b/src/librustc_resolve/resolve_imports.rs @@ -18,7 +18,7 @@ use rustc::lint::builtin::BuiltinLintDiagnostics; use rustc::lint::builtin::{ DUPLICATE_MACRO_EXPORTS, PUB_USE_OF_PRIVATE_EXTERN_CRATE, - REDUNDANT_IMPORT, + UNUSED_IMPORTS, }; use rustc::hir::def_id::{CrateNum, DefId}; use rustc::hir::def::*; @@ -1308,7 +1308,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> { is_redundant.present_items().all(|is_redundant| is_redundant) { self.session.buffer_lint_with_diagnostic( - REDUNDANT_IMPORT, + UNUSED_IMPORTS, directive.id, directive.span, &format!("the item `{}` is imported redundantly", ident), diff --git a/src/test/ui/lint/lint-unused-imports.rs b/src/test/ui/lint/lint-unused-imports.rs index c7ffdc270c94..9c5b206203c1 100644 --- a/src/test/ui/lint/lint-unused-imports.rs +++ b/src/test/ui/lint/lint-unused-imports.rs @@ -1,6 +1,5 @@ #![deny(unused_imports)] #![allow(dead_code)] -#![allow(redundant_import)] use bar::c::cc as cal; diff --git a/src/test/ui/lint/lint-unused-imports.stderr b/src/test/ui/lint/lint-unused-imports.stderr index bf194696f618..62490972420a 100644 --- a/src/test/ui/lint/lint-unused-imports.stderr +++ b/src/test/ui/lint/lint-unused-imports.stderr @@ -34,7 +34,7 @@ error: unused import: `foo::Square` LL | use foo::Square; | ^^^^^^^^^^^ -warning: the item `g` is imported redundantly +error: the item `g` is imported redundantly --> $DIR/lint-unused-imports.rs:68:9 | LL | / fn g() { @@ -44,9 +44,7 @@ LL | | fn f() { LL | | self::g(); LL | | } LL | | } - | |_- the item `g` was already imported here - | - = note: #[warn(redundant_import)] on by default + | |_- the item `g` was already defined here error: unused import: `self::g` --> $DIR/lint-unused-imports.rs:68:9 @@ -54,7 +52,7 @@ error: unused import: `self::g` LL | use self::g; | ^^^^^^^ -warning: the item `foo` is imported redundantly +error: the item `foo` is imported redundantly --> $DIR/lint-unused-imports.rs:77:9 | LL | use test2::{foo, bar}; @@ -75,5 +73,5 @@ error: unused import: `test::B2` LL | use test::B2; | ^^^^^^^^ -error: aborting due to 8 previous errors +error: aborting due to 10 previous errors diff --git a/src/test/ui/rust-2018/future-proofing-locals.rs b/src/test/ui/rust-2018/future-proofing-locals.rs index df3badb94e5f..2c388cf3713b 100644 --- a/src/test/ui/rust-2018/future-proofing-locals.rs +++ b/src/test/ui/rust-2018/future-proofing-locals.rs @@ -1,7 +1,7 @@ // edition:2018 #![allow(non_camel_case_types)] -#![allow(redundant_import)] +#![allow(unused_imports)] mod T { pub struct U; diff --git a/src/test/ui/rust-2018/future-proofing-locals.stderr b/src/test/ui/rust-2018/future-proofing-locals.stderr index fa8333b5d211..7021489a6ddc 100644 --- a/src/test/ui/rust-2018/future-proofing-locals.stderr +++ b/src/test/ui/rust-2018/future-proofing-locals.stderr @@ -1,102 +1,56 @@ error: imports cannot refer to type parameters - --> $DIR/future-proofing-locals.rs:13:9 + --> $DIR/future-proofing-locals.rs:14:9 | LL | use T as _; | ^ error: imports cannot refer to type parameters - --> $DIR/future-proofing-locals.rs:14:9 + --> $DIR/future-proofing-locals.rs:15:9 | LL | use T::U; | ^ error: imports cannot refer to type parameters - --> $DIR/future-proofing-locals.rs:15:9 + --> $DIR/future-proofing-locals.rs:16:9 | LL | use T::*; | ^ error: imports cannot refer to type parameters - --> $DIR/future-proofing-locals.rs:19:9 + --> $DIR/future-proofing-locals.rs:20:9 | LL | use T; | ^ error: imports cannot refer to local variables - --> $DIR/future-proofing-locals.rs:25:9 + --> $DIR/future-proofing-locals.rs:26:9 | LL | use x as _; | ^ error: imports cannot refer to local variables - --> $DIR/future-proofing-locals.rs:31:9 + --> $DIR/future-proofing-locals.rs:32:9 | LL | use x; | ^ error: imports cannot refer to local variables - --> $DIR/future-proofing-locals.rs:37:17 + --> $DIR/future-proofing-locals.rs:38:17 | LL | use x; | ^ error: imports cannot refer to type parameters - --> $DIR/future-proofing-locals.rs:45:10 + --> $DIR/future-proofing-locals.rs:46:10 | LL | use {T as _, x}; | ^ error: imports cannot refer to local variables - --> $DIR/future-proofing-locals.rs:45:18 + --> $DIR/future-proofing-locals.rs:46:18 | LL | use {T as _, x}; | ^ -warning: the item `T` is imported redundantly - --> $DIR/future-proofing-locals.rs:19:9 - | -LL | / mod T { -LL | | pub struct U; -LL | | } - | |_- the item `T` was already imported here -... -LL | use T; - | ^ - | - = note: #[warn(redundant_import)] on by default - -warning: the item `x` is imported redundantly - --> $DIR/future-proofing-locals.rs:31:9 - | -LL | / mod x { -LL | | pub struct y; -LL | | } - | |_- the item `x` was already imported here -... -LL | use x; - | ^ - -warning: the item `x` is imported redundantly - --> $DIR/future-proofing-locals.rs:37:17 - | -LL | / mod x { -LL | | pub struct y; -LL | | } - | |_- the item `x` was already imported here -... -LL | use x; - | ^ - -warning: the item `x` is imported redundantly - --> $DIR/future-proofing-locals.rs:45:18 - | -LL | / mod x { -LL | | pub struct y; -LL | | } - | |_- the item `x` was already imported here -... -LL | use {T as _, x}; - | ^ - error: aborting due to 9 previous errors From 0f3b1c035837fc5e101d5a2d8c49b1d5128927b5 Mon Sep 17 00:00:00 2001 From: Fabian Drinck Date: Sun, 17 Mar 2019 11:38:38 +0100 Subject: [PATCH 15/30] Fix tests --- src/test/ui/lint/lint-unused-imports.rs | 2 ++ src/test/ui/lint/lint-unused-imports.stderr | 5 +++-- src/test/ui/lint/use-redundant.rs | 1 + src/test/ui/lint/use-redundant.stderr | 10 +++++++--- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/test/ui/lint/lint-unused-imports.rs b/src/test/ui/lint/lint-unused-imports.rs index 9c5b206203c1..4754d8880763 100644 --- a/src/test/ui/lint/lint-unused-imports.rs +++ b/src/test/ui/lint/lint-unused-imports.rs @@ -66,6 +66,7 @@ mod bar { fn g() { use self::g; //~ ERROR unused import: `self::g` + //~^ ERROR the item `g` is imported redundantly fn f() { self::g(); } @@ -75,6 +76,7 @@ fn g() { #[allow(unused_variables)] fn h() { use test2::foo; //~ ERROR unused import: `test2::foo` + //~^ ERROR the item `foo` is imported redundantly let foo = 0; } diff --git a/src/test/ui/lint/lint-unused-imports.stderr b/src/test/ui/lint/lint-unused-imports.stderr index 62490972420a..b37f25ec0174 100644 --- a/src/test/ui/lint/lint-unused-imports.stderr +++ b/src/test/ui/lint/lint-unused-imports.stderr @@ -40,6 +40,7 @@ error: the item `g` is imported redundantly LL | / fn g() { LL | | use self::g; | | ^^^^^^^ +LL | | LL | | fn f() { LL | | self::g(); LL | | } @@ -53,7 +54,7 @@ LL | use self::g; | ^^^^^^^ error: the item `foo` is imported redundantly - --> $DIR/lint-unused-imports.rs:77:9 + --> $DIR/lint-unused-imports.rs:78:9 | LL | use test2::{foo, bar}; | --- the item `foo` was already imported here @@ -62,7 +63,7 @@ LL | use test2::foo; | ^^^^^^^^^^ error: unused import: `test2::foo` - --> $DIR/lint-unused-imports.rs:77:9 + --> $DIR/lint-unused-imports.rs:78:9 | LL | use test2::foo; | ^^^^^^^^^^ diff --git a/src/test/ui/lint/use-redundant.rs b/src/test/ui/lint/use-redundant.rs index 50d4d30625a6..7abf5e498b53 100644 --- a/src/test/ui/lint/use-redundant.rs +++ b/src/test/ui/lint/use-redundant.rs @@ -1,4 +1,5 @@ // compile-pass +#![warn(unused_imports)] use crate::foo::Bar; //~ WARNING first import diff --git a/src/test/ui/lint/use-redundant.stderr b/src/test/ui/lint/use-redundant.stderr index b5000b22a1da..b25ffc6073f4 100644 --- a/src/test/ui/lint/use-redundant.stderr +++ b/src/test/ui/lint/use-redundant.stderr @@ -1,11 +1,15 @@ warning: the item `Bar` is imported redundantly - --> $DIR/use-redundant.rs:14:9 + --> $DIR/use-redundant.rs:15:9 | LL | use crate::foo::Bar; - | --------------- the item `Bar` was imported here + | --------------- the item `Bar` was already imported here ... LL | use crate::foo::Bar; | ^^^^^^^^^^^^^^^ | - = note: #[warn(redundant_import)] on by default +note: lint level defined here + --> $DIR/use-redundant.rs:2:9 + | +LL | #![warn(unused_imports)] + | ^^^^^^^^^^^^^^ From 4a619aa1261c995abedc446226a7701c2c9e1c79 Mon Sep 17 00:00:00 2001 From: Fabian Drinck Date: Sun, 17 Mar 2019 11:55:32 +0100 Subject: [PATCH 16/30] Add glob import to redundancy test --- src/test/ui/lint/use-redundant.rs | 9 ++++++++ src/test/ui/lint/use-redundant.stderr | 33 ++++++++++++++++++++++----- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/test/ui/lint/use-redundant.rs b/src/test/ui/lint/use-redundant.rs index 7abf5e498b53..328f8232bafa 100644 --- a/src/test/ui/lint/use-redundant.rs +++ b/src/test/ui/lint/use-redundant.rs @@ -11,8 +11,17 @@ fn baz() -> Bar { 3 } +mod m1 { pub struct S {} } +mod m2 { pub struct S {} } + +use m1::*; +use m2::*; + fn main() { use crate::foo::Bar; //~ WARNING redundant import let _a: Bar = 3; baz(); + + use m1::S; //~ WARNING redundant import + let _s = S {}; } diff --git a/src/test/ui/lint/use-redundant.stderr b/src/test/ui/lint/use-redundant.stderr index b25ffc6073f4..82d2312779e6 100644 --- a/src/test/ui/lint/use-redundant.stderr +++ b/src/test/ui/lint/use-redundant.stderr @@ -1,15 +1,36 @@ +warning: unused import: `m1::*` + --> $DIR/use-redundant.rs:17:5 + | +LL | use m1::*; + | ^^^^^ + | +note: lint level defined here + --> $DIR/use-redundant.rs:2:9 + | +LL | #![warn(unused_imports)] + | ^^^^^^^^^^^^^^ + +warning: unused import: `m2::*` + --> $DIR/use-redundant.rs:18:5 + | +LL | use m2::*; + | ^^^^^ + warning: the item `Bar` is imported redundantly - --> $DIR/use-redundant.rs:15:9 + --> $DIR/use-redundant.rs:21:9 | LL | use crate::foo::Bar; | --------------- the item `Bar` was already imported here ... LL | use crate::foo::Bar; | ^^^^^^^^^^^^^^^ + +warning: the item `S` is imported redundantly + --> $DIR/use-redundant.rs:25:9 | -note: lint level defined here - --> $DIR/use-redundant.rs:2:9 - | -LL | #![warn(unused_imports)] - | ^^^^^^^^^^^^^^ +LL | use m1::*; + | ----- the item `S` was already imported here +... +LL | use m1::S; + | ^^^^^ From a97aeb41d4d342136f52b3cdddc8d59beae606cc Mon Sep 17 00:00:00 2001 From: Fabian Drinck Date: Sun, 17 Mar 2019 13:30:42 +0100 Subject: [PATCH 17/30] Fix more tests --- src/test/run-pass/binding/match-arm-statics.rs | 2 -- src/test/run-pass/ifmt.rs | 2 -- src/test/run-pass/invalid_const_promotion.rs | 1 - src/test/run-pass/issues/issue-38556.rs | 1 - src/test/run-pass/issues/issue-39367.rs | 1 - src/test/run-pass/out-of-stack.rs | 1 - src/test/run-pass/rfcs/rfc-2126-extern-absolute-paths/basic.rs | 1 - .../traits/traits-multidispatch-infer-convert-target.rs | 1 - 8 files changed, 10 deletions(-) diff --git a/src/test/run-pass/binding/match-arm-statics.rs b/src/test/run-pass/binding/match-arm-statics.rs index 359c39211588..5f7e357eeb2a 100644 --- a/src/test/run-pass/binding/match-arm-statics.rs +++ b/src/test/run-pass/binding/match-arm-statics.rs @@ -45,8 +45,6 @@ pub mod glfw { } fn issue_6533() { - use glfw; - fn action_to_str(state: glfw::InputState) -> &'static str { use glfw::{RELEASE, PRESS, REPEAT}; match state { diff --git a/src/test/run-pass/ifmt.rs b/src/test/run-pass/ifmt.rs index 56222fa46f7c..8c17b01e2bd8 100644 --- a/src/test/run-pass/ifmt.rs +++ b/src/test/run-pass/ifmt.rs @@ -238,7 +238,6 @@ pub fn main() { // Basic test to make sure that we can invoke the `write!` macro with an // fmt::Write instance. fn test_write() { - use std::fmt::Write; let mut buf = String::new(); write!(&mut buf, "{}", 3); { @@ -267,7 +266,6 @@ fn test_print() { // Just make sure that the macros are defined, there's not really a lot that we // can do with them just yet (to test the output) fn test_format_args() { - use std::fmt::Write; let mut buf = String::new(); { let w = &mut buf; diff --git a/src/test/run-pass/invalid_const_promotion.rs b/src/test/run-pass/invalid_const_promotion.rs index 1524373895d9..2775aac01561 100644 --- a/src/test/run-pass/invalid_const_promotion.rs +++ b/src/test/run-pass/invalid_const_promotion.rs @@ -25,7 +25,6 @@ fn foo() { #[cfg(unix)] fn check_status(status: std::process::ExitStatus) { - use libc; use std::os::unix::process::ExitStatusExt; assert!(status.signal() == Some(libc::SIGILL) diff --git a/src/test/run-pass/issues/issue-38556.rs b/src/test/run-pass/issues/issue-38556.rs index 0cc247f5b9ca..63fd9db08ff2 100644 --- a/src/test/run-pass/issues/issue-38556.rs +++ b/src/test/run-pass/issues/issue-38556.rs @@ -9,6 +9,5 @@ macro_rules! reexport { reexport!(); fn main() { - use Bar; fn f(_: Bar) {} } diff --git a/src/test/run-pass/issues/issue-39367.rs b/src/test/run-pass/issues/issue-39367.rs index bd92224bce16..484cd782a09d 100644 --- a/src/test/run-pass/issues/issue-39367.rs +++ b/src/test/run-pass/issues/issue-39367.rs @@ -15,7 +15,6 @@ fn arena() -> &'static ArenaSet> { fn require_sync(_: &T) { } unsafe fn __stability() -> &'static ArenaSet> { use std::mem::transmute; - use std::boxed::Box; static mut DATA: *const ArenaSet> = 0 as *const ArenaSet>; static mut ONCE: Once = ONCE_INIT; diff --git a/src/test/run-pass/out-of-stack.rs b/src/test/run-pass/out-of-stack.rs index 72d6d6806229..9f868d6e5c3e 100644 --- a/src/test/run-pass/out-of-stack.rs +++ b/src/test/run-pass/out-of-stack.rs @@ -36,7 +36,6 @@ fn loud_recurse() { #[cfg(unix)] fn check_status(status: std::process::ExitStatus) { - use libc; use std::os::unix::process::ExitStatusExt; assert!(!status.success()); diff --git a/src/test/run-pass/rfcs/rfc-2126-extern-absolute-paths/basic.rs b/src/test/run-pass/rfcs/rfc-2126-extern-absolute-paths/basic.rs index 15449a6b83e2..f25d81f1c9a5 100644 --- a/src/test/run-pass/rfcs/rfc-2126-extern-absolute-paths/basic.rs +++ b/src/test/run-pass/rfcs/rfc-2126-extern-absolute-paths/basic.rs @@ -7,7 +7,6 @@ use xcrate::Z; fn f() { - use xcrate; use xcrate as ycrate; let s = xcrate::S; assert_eq!(format!("{:?}", s), "S"); diff --git a/src/test/run-pass/traits/traits-multidispatch-infer-convert-target.rs b/src/test/run-pass/traits/traits-multidispatch-infer-convert-target.rs index ca47d9736f63..626e1ae71bc2 100644 --- a/src/test/run-pass/traits/traits-multidispatch-infer-convert-target.rs +++ b/src/test/run-pass/traits/traits-multidispatch-infer-convert-target.rs @@ -28,7 +28,6 @@ where T : Convert } fn main() { - use std::default::Default; // T = i16, U = u32 test(22_i16, Default::default(), 2, 4); From 8fb05491514c5cfb3c3aa03e0266389fa0b29ed0 Mon Sep 17 00:00:00 2001 From: Fabian Drinck Date: Sun, 17 Mar 2019 17:40:25 +0100 Subject: [PATCH 18/30] Fix doc tests --- src/liballoc/borrow.rs | 2 +- src/libcore/cell.rs | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/liballoc/borrow.rs b/src/liballoc/borrow.rs index 74c80a08b12a..ee1799fad8e1 100644 --- a/src/liballoc/borrow.rs +++ b/src/liballoc/borrow.rs @@ -135,7 +135,7 @@ impl ToOwned for T /// Another example showing how to keep `Cow` in a struct: /// /// ``` -/// use std::borrow::{Cow, ToOwned}; +/// use std::borrow::Cow; /// /// struct Items<'a, X: 'a> where [X]: ToOwned> { /// values: Cow<'a, [X]>, diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 753f10e6a0ad..3b888244341f 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -1421,7 +1421,6 @@ impl fmt::Display for RefMut<'_, T> { /// /// ``` /// use std::cell::UnsafeCell; -/// use std::marker::Sync; /// /// # #[allow(dead_code)] /// struct NotThreadSafe { From df80eae985e80e0907c1ebde4caf1d7cc8acbc21 Mon Sep 17 00:00:00 2001 From: Fabian Drinck Date: Sun, 17 Mar 2019 21:07:57 +0100 Subject: [PATCH 19/30] Change message to present tense --- src/librustc/lint/builtin.rs | 2 +- src/test/ui/lint/lint-unused-imports.stderr | 4 ++-- src/test/ui/lint/use-redundant.stderr | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/librustc/lint/builtin.rs b/src/librustc/lint/builtin.rs index 6908674bc13a..2d8a2c6321fa 100644 --- a/src/librustc/lint/builtin.rs +++ b/src/librustc/lint/builtin.rs @@ -585,7 +585,7 @@ impl BuiltinLintDiagnostics { let introduced = if is_imported { "imported" } else { "defined" }; db.span_label( span, - format!("the item `{}` was already {} here", ident, introduced) + format!("the item `{}` is already {} here", ident, introduced) ); } } diff --git a/src/test/ui/lint/lint-unused-imports.stderr b/src/test/ui/lint/lint-unused-imports.stderr index b37f25ec0174..96d71a228a5f 100644 --- a/src/test/ui/lint/lint-unused-imports.stderr +++ b/src/test/ui/lint/lint-unused-imports.stderr @@ -45,7 +45,7 @@ LL | | fn f() { LL | | self::g(); LL | | } LL | | } - | |_- the item `g` was already defined here + | |_- the item `g` is already defined here error: unused import: `self::g` --> $DIR/lint-unused-imports.rs:68:9 @@ -57,7 +57,7 @@ error: the item `foo` is imported redundantly --> $DIR/lint-unused-imports.rs:78:9 | LL | use test2::{foo, bar}; - | --- the item `foo` was already imported here + | --- the item `foo` is already imported here ... LL | use test2::foo; | ^^^^^^^^^^ diff --git a/src/test/ui/lint/use-redundant.stderr b/src/test/ui/lint/use-redundant.stderr index 82d2312779e6..3554443590ab 100644 --- a/src/test/ui/lint/use-redundant.stderr +++ b/src/test/ui/lint/use-redundant.stderr @@ -20,7 +20,7 @@ warning: the item `Bar` is imported redundantly --> $DIR/use-redundant.rs:21:9 | LL | use crate::foo::Bar; - | --------------- the item `Bar` was already imported here + | --------------- the item `Bar` is already imported here ... LL | use crate::foo::Bar; | ^^^^^^^^^^^^^^^ @@ -29,7 +29,7 @@ warning: the item `S` is imported redundantly --> $DIR/use-redundant.rs:25:9 | LL | use m1::*; - | ----- the item `S` was already imported here + | ----- the item `S` is already imported here ... LL | use m1::S; | ^^^^^ From c244c411e4c869a76c7a8163b67840bdf83674e6 Mon Sep 17 00:00:00 2001 From: Fabian Drinck Date: Mon, 18 Mar 2019 17:50:49 +0100 Subject: [PATCH 20/30] Handle glob import in redundancy check --- src/librustc_resolve/resolve_imports.rs | 5 ++++- src/test/ui/lint/use-redundant.stderr | 9 --------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/src/librustc_resolve/resolve_imports.rs b/src/librustc_resolve/resolve_imports.rs index 99e20c398d92..7ea07f5e0cbc 100644 --- a/src/librustc_resolve/resolve_imports.rs +++ b/src/librustc_resolve/resolve_imports.rs @@ -1294,7 +1294,10 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> { directive.span, ) { Ok(other_binding) => { - is_redundant[ns] = Some(binding.def() == other_binding.def()); + is_redundant[ns] = Some( + binding.def() == other_binding.def() + && !other_binding.is_ambiguity() + ); redundant_span[ns] = Some((other_binding.span, other_binding.is_import())); } diff --git a/src/test/ui/lint/use-redundant.stderr b/src/test/ui/lint/use-redundant.stderr index 3554443590ab..fbd9f81f18f8 100644 --- a/src/test/ui/lint/use-redundant.stderr +++ b/src/test/ui/lint/use-redundant.stderr @@ -25,12 +25,3 @@ LL | use crate::foo::Bar; LL | use crate::foo::Bar; | ^^^^^^^^^^^^^^^ -warning: the item `S` is imported redundantly - --> $DIR/use-redundant.rs:25:9 - | -LL | use m1::*; - | ----- the item `S` is already imported here -... -LL | use m1::S; - | ^^^^^ - From 2ade4430402dcbe3bba9a17a13cb290f73f5a477 Mon Sep 17 00:00:00 2001 From: Fabian Drinck Date: Mon, 18 Mar 2019 17:51:00 +0100 Subject: [PATCH 21/30] Restore test --- src/test/run-pass/rfcs/rfc-2126-extern-absolute-paths/basic.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/test/run-pass/rfcs/rfc-2126-extern-absolute-paths/basic.rs b/src/test/run-pass/rfcs/rfc-2126-extern-absolute-paths/basic.rs index f25d81f1c9a5..566b3581046d 100644 --- a/src/test/run-pass/rfcs/rfc-2126-extern-absolute-paths/basic.rs +++ b/src/test/run-pass/rfcs/rfc-2126-extern-absolute-paths/basic.rs @@ -4,9 +4,12 @@ // compile-flags:--extern xcrate // edition:2018 +#![allow(unused_imports)] + use xcrate::Z; fn f() { + use xcrate; use xcrate as ycrate; let s = xcrate::S; assert_eq!(format!("{:?}", s), "S"); From aec518addd7ab4624ccd57ea143395483dfc690a Mon Sep 17 00:00:00 2001 From: Yuki OKUSHI Date: Sun, 31 Mar 2019 07:13:59 +0900 Subject: [PATCH 22/30] Fix test --- src/test/codegen/instrument-mcount.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/codegen/instrument-mcount.rs b/src/test/codegen/instrument-mcount.rs index bd01556d986a..c72d09f7a03d 100644 --- a/src/test/codegen/instrument-mcount.rs +++ b/src/test/codegen/instrument-mcount.rs @@ -3,5 +3,5 @@ #![crate_type = "lib"] -// CHECK: attributes #{{.*}} "instrument-function-entry-inlined"="{{_*}}mcount" "no-frame-pointer-elim"="true" +// CHECK: attributes #{{.*}} "instrument-function-entry-inlined"="{{.*}}mcount{{.*}}" "no-frame-pointer-elim"="true" pub fn foo() {} From 70fa616a23b59e4b7b537e55f5b95ec3e963b2fd Mon Sep 17 00:00:00 2001 From: Jean-Marie Comets Date: Sun, 31 Mar 2019 10:54:00 +0200 Subject: [PATCH 23/30] Stabilize refcell_replace_swap feature, closes #43570 --- src/libcore/cell.rs | 3 +-- src/libcore/tests/lib.rs | 1 - src/librustc/lib.rs | 1 - src/librustc_typeck/lib.rs | 1 - src/libsyntax/feature_gate.rs | 2 ++ 5 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 753f10e6a0ad..99169a6a259c 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -711,7 +711,6 @@ impl RefCell { /// # Examples /// /// ``` - /// #![feature(refcell_replace_swap)] /// use std::cell::RefCell; /// let cell = RefCell::new(5); /// let old_value = cell.replace_with(|&mut old| old + 1); @@ -719,7 +718,7 @@ impl RefCell { /// assert_eq!(cell, RefCell::new(6)); /// ``` #[inline] - #[unstable(feature = "refcell_replace_swap", issue="43570")] + #[stable(feature = "refcell_replace_swap", since="1.35.0")] pub fn replace_with T>(&self, f: F) -> T { let mut_borrow = &mut *self.borrow_mut(); let replacement = f(mut_borrow); diff --git a/src/libcore/tests/lib.rs b/src/libcore/tests/lib.rs index 2ed25a341021..5e0dbb7ab2f1 100644 --- a/src/libcore/tests/lib.rs +++ b/src/libcore/tests/lib.rs @@ -16,7 +16,6 @@ #![feature(pattern)] #![feature(range_is_empty)] #![feature(raw)] -#![feature(refcell_replace_swap)] #![feature(slice_patterns)] #![feature(sort_internals)] #![feature(specialization)] diff --git a/src/librustc/lib.rs b/src/librustc/lib.rs index e905c3688518..b87343b43c9f 100644 --- a/src/librustc/lib.rs +++ b/src/librustc/lib.rs @@ -45,7 +45,6 @@ #![feature(proc_macro_internals)] #![feature(optin_builtin_traits)] #![feature(range_is_empty)] -#![feature(refcell_replace_swap)] #![feature(rustc_diagnostic_macros)] #![feature(rustc_attrs)] #![feature(slice_patterns)] diff --git a/src/librustc_typeck/lib.rs b/src/librustc_typeck/lib.rs index cc90044573ce..1fa16352b867 100644 --- a/src/librustc_typeck/lib.rs +++ b/src/librustc_typeck/lib.rs @@ -64,7 +64,6 @@ This API is completely unstable and subject to change. #![feature(crate_visibility_modifier)] #![feature(exhaustive_patterns)] #![feature(nll)] -#![feature(refcell_replace_swap)] #![feature(rustc_diagnostic_macros)] #![feature(slice_patterns)] #![feature(never_type)] diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index dcb55fb572f3..d53cfddaf8de 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -703,6 +703,8 @@ declare_features! ( (accepted, extern_crate_self, "1.34.0", Some(56409), None), // support for arbitrary delimited token streams in non-macro attributes (accepted, unrestricted_attribute_tokens, "1.34.0", Some(55208), None), + // add replace and swap functions to RefCell + (accepted, refcell_replace_swap, "1.35.0", Some(43570), None), ); // If you change this, please modify `src/doc/unstable-book` as well. You must From ae6622da58d011e57489df7971a57f2cb6a6e926 Mon Sep 17 00:00:00 2001 From: Fabian Drinck Date: Sun, 31 Mar 2019 12:02:27 +0200 Subject: [PATCH 24/30] Add back missing import --- src/librustc_traits/lowering/environment.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_traits/lowering/environment.rs b/src/librustc_traits/lowering/environment.rs index eb47892ea410..3570cb102460 100644 --- a/src/librustc_traits/lowering/environment.rs +++ b/src/librustc_traits/lowering/environment.rs @@ -164,7 +164,7 @@ crate fn environment<'a, 'tcx>( tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId ) -> Environment<'tcx> { - use super::IntoFromEnvGoal; + use super::{Lower, IntoFromEnvGoal}; use rustc::hir::{Node, TraitItemKind, ImplItemKind, ItemKind, ForeignItemKind}; debug!("environment(def_id = {:?})", def_id); From c1d5314bd3b628749491e4fe201b4c6916a53ce6 Mon Sep 17 00:00:00 2001 From: Fabian Drinck Date: Sun, 31 Mar 2019 14:11:46 +0200 Subject: [PATCH 25/30] Remove redundant import --- src/librustc_codegen_ssa/traits/type_.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/librustc_codegen_ssa/traits/type_.rs b/src/librustc_codegen_ssa/traits/type_.rs index 7fb2cb9d3936..efc18d401c08 100644 --- a/src/librustc_codegen_ssa/traits/type_.rs +++ b/src/librustc_codegen_ssa/traits/type_.rs @@ -77,7 +77,6 @@ pub trait DerivedTypeMethods<'tcx>: BaseTypeMethods<'tcx> + MiscMethods<'tcx> { } fn type_has_metadata(&self, ty: Ty<'tcx>) -> bool { - use syntax_pos::DUMMY_SP; if ty.is_sized(self.tcx().at(DUMMY_SP), ty::ParamEnv::reveal_all()) { return false; } From c789a539a29b411b89411f8660d6ed2c2c4c4bbb Mon Sep 17 00:00:00 2001 From: Jean-Marie Comets Date: Sun, 31 Mar 2019 14:50:06 +0200 Subject: [PATCH 26/30] refcell_replace_swap: remove feature gate & obsolete documentation item --- src/libcore/cell.rs | 2 -- src/libsyntax/feature_gate.rs | 2 -- 2 files changed, 4 deletions(-) diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 99169a6a259c..f8c7ce101322 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -702,8 +702,6 @@ impl RefCell { /// Replaces the wrapped value with a new one computed from `f`, returning /// the old value, without deinitializing either one. /// - /// This function corresponds to [`std::mem::replace`](../mem/fn.replace.html). - /// /// # Panics /// /// Panics if the value is currently borrowed. diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index d53cfddaf8de..dcb55fb572f3 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -703,8 +703,6 @@ declare_features! ( (accepted, extern_crate_self, "1.34.0", Some(56409), None), // support for arbitrary delimited token streams in non-macro attributes (accepted, unrestricted_attribute_tokens, "1.34.0", Some(55208), None), - // add replace and swap functions to RefCell - (accepted, refcell_replace_swap, "1.35.0", Some(43570), None), ); // If you change this, please modify `src/doc/unstable-book` as well. You must From 55b7efe29f333427bdc0d664a13171490ee615cc Mon Sep 17 00:00:00 2001 From: Jaro Fietz Date: Sun, 31 Mar 2019 15:20:10 +0200 Subject: [PATCH 27/30] match match match match match --- src/test/run-pass/weird-exprs.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/test/run-pass/weird-exprs.rs b/src/test/run-pass/weird-exprs.rs index 7ce7e29e8723..ae4de92ff748 100644 --- a/src/test/run-pass/weird-exprs.rs +++ b/src/test/run-pass/weird-exprs.rs @@ -127,6 +127,21 @@ fn punch_card() -> impl std::fmt::Debug { ..=.. ..=.. .. ..=..=.. ..=..=.. .. .. ..=.. .. } +fn r#match() { + let val = match match match match match () { + () => () + } { + () => () + } { + () => () + } { + () => () + } { + () => () + }; + assert_eq!(val, ()); +} + pub fn main() { strange(); funny(); @@ -142,4 +157,5 @@ pub fn main() { union(); special_characters(); punch_card(); + r#match(); } From 7b26a43ae51fdb3c2ec7a3407e91add212c5754e Mon Sep 17 00:00:00 2001 From: Yuki OKUSHI Date: Sun, 31 Mar 2019 23:17:49 +0900 Subject: [PATCH 28/30] Use `u{1}` instead of `01` --- src/librustc_target/spec/aarch64_unknown_linux_gnu.rs | 2 +- src/librustc_target/spec/aarch64_unknown_linux_musl.rs | 2 +- src/librustc_target/spec/arm_unknown_linux_gnueabi.rs | 2 +- src/librustc_target/spec/arm_unknown_linux_gnueabihf.rs | 2 +- src/librustc_target/spec/arm_unknown_linux_musleabi.rs | 2 +- src/librustc_target/spec/arm_unknown_linux_musleabihf.rs | 2 +- src/librustc_target/spec/armv4t_unknown_linux_gnueabi.rs | 2 +- src/librustc_target/spec/armv5te_unknown_linux_gnueabi.rs | 2 +- src/librustc_target/spec/armv5te_unknown_linux_musleabi.rs | 2 +- src/librustc_target/spec/armv6_unknown_freebsd.rs | 2 +- src/librustc_target/spec/armv7_unknown_cloudabi_eabihf.rs | 2 +- src/librustc_target/spec/armv7_unknown_freebsd.rs | 2 +- src/librustc_target/spec/armv7_unknown_linux_gnueabihf.rs | 2 +- src/librustc_target/spec/armv7_unknown_linux_musleabihf.rs | 2 +- src/librustc_target/spec/i686_apple_darwin.rs | 2 +- src/librustc_target/spec/x86_64_apple_darwin.rs | 2 +- 16 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/librustc_target/spec/aarch64_unknown_linux_gnu.rs b/src/librustc_target/spec/aarch64_unknown_linux_gnu.rs index 59a285736412..b9d36c09f163 100644 --- a/src/librustc_target/spec/aarch64_unknown_linux_gnu.rs +++ b/src/librustc_target/spec/aarch64_unknown_linux_gnu.rs @@ -17,7 +17,7 @@ pub fn target() -> TargetResult { linker_flavor: LinkerFlavor::Gcc, options: TargetOptions { abi_blacklist: super::arm_base::abi_blacklist(), - target_mcount: "\01_mcount".to_string(), + target_mcount: "\u{1}_mcount".to_string(), .. base }, }) diff --git a/src/librustc_target/spec/aarch64_unknown_linux_musl.rs b/src/librustc_target/spec/aarch64_unknown_linux_musl.rs index 781b1322f5ac..968e82ca39fb 100644 --- a/src/librustc_target/spec/aarch64_unknown_linux_musl.rs +++ b/src/librustc_target/spec/aarch64_unknown_linux_musl.rs @@ -17,7 +17,7 @@ pub fn target() -> TargetResult { linker_flavor: LinkerFlavor::Gcc, options: TargetOptions { abi_blacklist: super::arm_base::abi_blacklist(), - target_mcount: "\01_mcount".to_string(), + target_mcount: "\u{1}_mcount".to_string(), .. base }, }) diff --git a/src/librustc_target/spec/arm_unknown_linux_gnueabi.rs b/src/librustc_target/spec/arm_unknown_linux_gnueabi.rs index 1173d18634e1..2f835420148f 100644 --- a/src/librustc_target/spec/arm_unknown_linux_gnueabi.rs +++ b/src/librustc_target/spec/arm_unknown_linux_gnueabi.rs @@ -18,7 +18,7 @@ pub fn target() -> TargetResult { options: TargetOptions { features: "+strict-align,+v6".to_string(), abi_blacklist: super::arm_base::abi_blacklist(), - target_mcount: "\01__gnu_mcount_nc".to_string(), + target_mcount: "\u{1}__gnu_mcount_nc".to_string(), .. base }, }) diff --git a/src/librustc_target/spec/arm_unknown_linux_gnueabihf.rs b/src/librustc_target/spec/arm_unknown_linux_gnueabihf.rs index 6fc7c46be3b1..cd4b2e1c9225 100644 --- a/src/librustc_target/spec/arm_unknown_linux_gnueabihf.rs +++ b/src/librustc_target/spec/arm_unknown_linux_gnueabihf.rs @@ -18,7 +18,7 @@ pub fn target() -> TargetResult { options: TargetOptions { features: "+strict-align,+v6,+vfp2".to_string(), abi_blacklist: super::arm_base::abi_blacklist(), - target_mcount: "\01__gnu_mcount_nc".to_string(), + target_mcount: "\u{1}__gnu_mcount_nc".to_string(), .. base } }) diff --git a/src/librustc_target/spec/arm_unknown_linux_musleabi.rs b/src/librustc_target/spec/arm_unknown_linux_musleabi.rs index 7a2a4e85e4a1..606c3f190603 100644 --- a/src/librustc_target/spec/arm_unknown_linux_musleabi.rs +++ b/src/librustc_target/spec/arm_unknown_linux_musleabi.rs @@ -23,7 +23,7 @@ pub fn target() -> TargetResult { linker_flavor: LinkerFlavor::Gcc, options: TargetOptions { abi_blacklist: super::arm_base::abi_blacklist(), - target_mcount: "\01mcount".to_string(), + target_mcount: "\u{1}mcount".to_string(), .. base }, }) diff --git a/src/librustc_target/spec/arm_unknown_linux_musleabihf.rs b/src/librustc_target/spec/arm_unknown_linux_musleabihf.rs index b7aa1952cb0b..d22156bc328e 100644 --- a/src/librustc_target/spec/arm_unknown_linux_musleabihf.rs +++ b/src/librustc_target/spec/arm_unknown_linux_musleabihf.rs @@ -23,7 +23,7 @@ pub fn target() -> TargetResult { linker_flavor: LinkerFlavor::Gcc, options: TargetOptions { abi_blacklist: super::arm_base::abi_blacklist(), - target_mcount: "\01mcount".to_string(), + target_mcount: "\u{1}mcount".to_string(), .. base }, }) diff --git a/src/librustc_target/spec/armv4t_unknown_linux_gnueabi.rs b/src/librustc_target/spec/armv4t_unknown_linux_gnueabi.rs index 59c15f1610de..e7da24843cc0 100644 --- a/src/librustc_target/spec/armv4t_unknown_linux_gnueabi.rs +++ b/src/librustc_target/spec/armv4t_unknown_linux_gnueabi.rs @@ -19,7 +19,7 @@ pub fn target() -> TargetResult { // Atomic operations provided by compiler-builtins max_atomic_width: Some(32), abi_blacklist: super::arm_base::abi_blacklist(), - target_mcount: "\01__gnu_mcount_nc".to_string(), + target_mcount: "\u{1}__gnu_mcount_nc".to_string(), .. base } }) diff --git a/src/librustc_target/spec/armv5te_unknown_linux_gnueabi.rs b/src/librustc_target/spec/armv5te_unknown_linux_gnueabi.rs index a1a61a69d052..ea586f42c269 100644 --- a/src/librustc_target/spec/armv5te_unknown_linux_gnueabi.rs +++ b/src/librustc_target/spec/armv5te_unknown_linux_gnueabi.rs @@ -19,7 +19,7 @@ pub fn target() -> TargetResult { // Atomic operations provided by compiler-builtins max_atomic_width: Some(32), abi_blacklist: super::arm_base::abi_blacklist(), - target_mcount: "\01__gnu_mcount_nc".to_string(), + target_mcount: "\u{1}__gnu_mcount_nc".to_string(), .. base } }) diff --git a/src/librustc_target/spec/armv5te_unknown_linux_musleabi.rs b/src/librustc_target/spec/armv5te_unknown_linux_musleabi.rs index 9b44a54715e7..dae5c8c3d750 100644 --- a/src/librustc_target/spec/armv5te_unknown_linux_musleabi.rs +++ b/src/librustc_target/spec/armv5te_unknown_linux_musleabi.rs @@ -22,7 +22,7 @@ pub fn target() -> TargetResult { // Atomic operations provided by compiler-builtins max_atomic_width: Some(32), abi_blacklist: super::arm_base::abi_blacklist(), - target_mcount: "\01mcount".to_string(), + target_mcount: "\u{1}mcount".to_string(), .. base } }) diff --git a/src/librustc_target/spec/armv6_unknown_freebsd.rs b/src/librustc_target/spec/armv6_unknown_freebsd.rs index e706c8281d23..a90590a39e75 100644 --- a/src/librustc_target/spec/armv6_unknown_freebsd.rs +++ b/src/librustc_target/spec/armv6_unknown_freebsd.rs @@ -18,7 +18,7 @@ pub fn target() -> TargetResult { features: "+v6,+vfp2".to_string(), max_atomic_width: Some(64), abi_blacklist: super::arm_base::abi_blacklist(), - target_mcount: "\01__gnu_mcount_nc".to_string(), + target_mcount: "\u{1}__gnu_mcount_nc".to_string(), .. base } }) diff --git a/src/librustc_target/spec/armv7_unknown_cloudabi_eabihf.rs b/src/librustc_target/spec/armv7_unknown_cloudabi_eabihf.rs index 70d4985ab860..c03f4b544ed0 100644 --- a/src/librustc_target/spec/armv7_unknown_cloudabi_eabihf.rs +++ b/src/librustc_target/spec/armv7_unknown_cloudabi_eabihf.rs @@ -20,7 +20,7 @@ pub fn target() -> TargetResult { target_vendor: "unknown".to_string(), linker_flavor: LinkerFlavor::Gcc, options: TargetOptions { - target_mcount: "\01mcount".to_string(), + target_mcount: "\u{1}mcount".to_string(), .. base }, }) diff --git a/src/librustc_target/spec/armv7_unknown_freebsd.rs b/src/librustc_target/spec/armv7_unknown_freebsd.rs index 0f9ee5d44a95..ca7ab474bef8 100644 --- a/src/librustc_target/spec/armv7_unknown_freebsd.rs +++ b/src/librustc_target/spec/armv7_unknown_freebsd.rs @@ -18,7 +18,7 @@ pub fn target() -> TargetResult { features: "+v7,+vfp3,+d16,+thumb2,-neon".to_string(), max_atomic_width: Some(64), abi_blacklist: super::arm_base::abi_blacklist(), - target_mcount: "\01__gnu_mcount_nc".to_string(), + target_mcount: "\u{1}__gnu_mcount_nc".to_string(), .. base } }) diff --git a/src/librustc_target/spec/armv7_unknown_linux_gnueabihf.rs b/src/librustc_target/spec/armv7_unknown_linux_gnueabihf.rs index afba6f608ee0..f0952cccb208 100644 --- a/src/librustc_target/spec/armv7_unknown_linux_gnueabihf.rs +++ b/src/librustc_target/spec/armv7_unknown_linux_gnueabihf.rs @@ -23,7 +23,7 @@ pub fn target() -> TargetResult { cpu: "generic".to_string(), max_atomic_width: Some(64), abi_blacklist: super::arm_base::abi_blacklist(), - target_mcount: "\01__gnu_mcount_nc".to_string(), + target_mcount: "\u{1}__gnu_mcount_nc".to_string(), .. base } }) diff --git a/src/librustc_target/spec/armv7_unknown_linux_musleabihf.rs b/src/librustc_target/spec/armv7_unknown_linux_musleabihf.rs index 516d0401adf5..a9974f6b80c9 100644 --- a/src/librustc_target/spec/armv7_unknown_linux_musleabihf.rs +++ b/src/librustc_target/spec/armv7_unknown_linux_musleabihf.rs @@ -26,7 +26,7 @@ pub fn target() -> TargetResult { cpu: "generic".to_string(), max_atomic_width: Some(64), abi_blacklist: super::arm_base::abi_blacklist(), - target_mcount: "\01mcount".to_string(), + target_mcount: "\u{1}mcount".to_string(), .. base } }) diff --git a/src/librustc_target/spec/i686_apple_darwin.rs b/src/librustc_target/spec/i686_apple_darwin.rs index a82ff33ce1db..58c59cc87284 100644 --- a/src/librustc_target/spec/i686_apple_darwin.rs +++ b/src/librustc_target/spec/i686_apple_darwin.rs @@ -20,7 +20,7 @@ pub fn target() -> TargetResult { target_vendor: "apple".to_string(), linker_flavor: LinkerFlavor::Gcc, options: TargetOptions { - target_mcount: "\01mcount".to_string(), + target_mcount: "\u{1}mcount".to_string(), .. base }, }) diff --git a/src/librustc_target/spec/x86_64_apple_darwin.rs b/src/librustc_target/spec/x86_64_apple_darwin.rs index fecc20c01c5e..c54181741b3c 100644 --- a/src/librustc_target/spec/x86_64_apple_darwin.rs +++ b/src/librustc_target/spec/x86_64_apple_darwin.rs @@ -20,7 +20,7 @@ pub fn target() -> TargetResult { target_vendor: "apple".to_string(), linker_flavor: LinkerFlavor::Gcc, options: TargetOptions { - target_mcount: "\01mcount".to_string(), + target_mcount: "\u{1}mcount".to_string(), .. base }, }) From c056a79f354b9b85cfeb9a6d32632edea4719263 Mon Sep 17 00:00:00 2001 From: Aaron Power Date: Sun, 31 Mar 2019 16:54:05 +0200 Subject: [PATCH 29/30] Remove #[doc(hidden)] from Error::type_id --- src/libstd/error.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libstd/error.rs b/src/libstd/error.rs index 3eb289501cb0..6c80a23e2e04 100644 --- a/src/libstd/error.rs +++ b/src/libstd/error.rs @@ -197,7 +197,6 @@ pub trait Error: Debug + Display { fn source(&self) -> Option<&(dyn Error + 'static)> { None } /// Gets the `TypeId` of `self` - #[doc(hidden)] #[stable(feature = "error_type_id", since = "1.34.0")] fn type_id(&self) -> TypeId where Self: 'static { TypeId::of::() From 07021e07edf30384203b43024f5bc81ac84a9574 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Mon, 1 Apr 2019 00:00:43 +0900 Subject: [PATCH 30/30] Allow closure to unsafe fn coercion --- src/librustc/middle/expr_use_visitor.rs | 2 +- src/librustc/middle/mem_categorization.rs | 2 +- src/librustc/mir/mod.rs | 5 +++-- src/librustc/ty/adjustment.rs | 5 +++-- src/librustc/ty/context.rs | 8 ++++++-- src/librustc/ty/structural_impls.rs | 6 +++--- src/librustc_codegen_ssa/mir/rvalue.rs | 2 +- src/librustc_mir/borrow_check/nll/type_check/mod.rs | 4 ++-- src/librustc_mir/build/expr/as_rvalue.rs | 4 ++-- src/librustc_mir/hair/cx/expr.rs | 4 ++-- src/librustc_mir/hair/mod.rs | 1 + src/librustc_mir/interpret/cast.rs | 2 +- src/librustc_mir/monomorphize/collector.rs | 2 +- src/librustc_mir/transform/qualify_consts.rs | 2 +- src/librustc_mir/transform/qualify_min_const_fn.rs | 2 +- src/librustc_passes/rvalue_promotion.rs | 2 +- src/librustc_typeck/check/coercion.rs | 12 ++++++++---- .../coerce-unsafe-closure-to-unsafe-fn-ptr.rs | 5 +++++ src/test/run-pass/typeck-closure-to-unsafe-fn-ptr.rs | 7 +++++++ 19 files changed, 50 insertions(+), 27 deletions(-) create mode 100644 src/test/compile-fail/coerce-unsafe-closure-to-unsafe-fn-ptr.rs create mode 100644 src/test/run-pass/typeck-closure-to-unsafe-fn-ptr.rs diff --git a/src/librustc/middle/expr_use_visitor.rs b/src/librustc/middle/expr_use_visitor.rs index 01e57273e54a..4f630fe9a391 100644 --- a/src/librustc/middle/expr_use_visitor.rs +++ b/src/librustc/middle/expr_use_visitor.rs @@ -707,7 +707,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> { adjustment::Adjust::NeverToAny | adjustment::Adjust::ReifyFnPointer | adjustment::Adjust::UnsafeFnPointer | - adjustment::Adjust::ClosureFnPointer | + adjustment::Adjust::ClosureFnPointer(_) | adjustment::Adjust::MutToConstPointer | adjustment::Adjust::Unsize => { // Creating a closure/fn-pointer or unsizing consumes diff --git a/src/librustc/middle/mem_categorization.rs b/src/librustc/middle/mem_categorization.rs index 4b169dea06c7..1a3fef18404e 100644 --- a/src/librustc/middle/mem_categorization.rs +++ b/src/librustc/middle/mem_categorization.rs @@ -621,7 +621,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> { adjustment::Adjust::NeverToAny | adjustment::Adjust::ReifyFnPointer | adjustment::Adjust::UnsafeFnPointer | - adjustment::Adjust::ClosureFnPointer | + adjustment::Adjust::ClosureFnPointer(_) | adjustment::Adjust::MutToConstPointer | adjustment::Adjust::Borrow(_) | adjustment::Adjust::Unsize => { diff --git a/src/librustc/mir/mod.rs b/src/librustc/mir/mod.rs index d35ee1e57d5c..7b419e306db6 100644 --- a/src/librustc/mir/mod.rs +++ b/src/librustc/mir/mod.rs @@ -2247,8 +2247,9 @@ pub enum CastKind { /// Converts unique, zero-sized type for a fn to fn() ReifyFnPointer, - /// Converts non capturing closure to fn() - ClosureFnPointer, + /// Converts non capturing closure to fn() or unsafe fn(). + /// It cannot convert a closure that requires unsafe. + ClosureFnPointer(hir::Unsafety), /// Converts safe fn() to unsafe fn() UnsafeFnPointer, diff --git a/src/librustc/ty/adjustment.rs b/src/librustc/ty/adjustment.rs index f9149ce0f6e6..c2ef08c4c40f 100644 --- a/src/librustc/ty/adjustment.rs +++ b/src/librustc/ty/adjustment.rs @@ -62,8 +62,9 @@ pub enum Adjust<'tcx> { /// Go from a safe fn pointer to an unsafe fn pointer. UnsafeFnPointer, - /// Go from a non-capturing closure to an fn pointer. - ClosureFnPointer, + /// Go from a non-capturing closure to an fn pointer or an unsafe fn pointer. + /// It cannot convert a closure that requires unsafe. + ClosureFnPointer(hir::Unsafety), /// Go from a mut raw pointer to a const raw pointer. MutToConstPointer, diff --git a/src/librustc/ty/context.rs b/src/librustc/ty/context.rs index ea003ba1ac70..77330c7a9d1b 100644 --- a/src/librustc/ty/context.rs +++ b/src/librustc/ty/context.rs @@ -2441,7 +2441,11 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { /// type with the same signature. Detuples and so forth -- so /// e.g., if we have a sig with `Fn<(u32, i32)>` then you would get /// a `fn(u32, i32)`. - pub fn coerce_closure_fn_ty(self, sig: PolyFnSig<'tcx>) -> Ty<'tcx> { + /// `unsafety` determines the unsafety of the `fn` type. If you pass + /// `hir::Unsafety::Unsafe` in the previous example, then you would get + /// an `unsafe fn (u32, i32)`. + /// It cannot convert a closure that requires unsafe. + pub fn coerce_closure_fn_ty(self, sig: PolyFnSig<'tcx>, unsafety: hir::Unsafety) -> Ty<'tcx> { let converted_sig = sig.map_bound(|s| { let params_iter = match s.inputs()[0].sty { ty::Tuple(params) => { @@ -2453,7 +2457,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { params_iter, s.output(), s.c_variadic, - hir::Unsafety::Normal, + unsafety, abi::Abi::Rust, ) }); diff --git a/src/librustc/ty/structural_impls.rs b/src/librustc/ty/structural_impls.rs index cbdda7326928..4f1fda3f4e53 100644 --- a/src/librustc/ty/structural_impls.rs +++ b/src/librustc/ty/structural_impls.rs @@ -630,8 +630,8 @@ impl<'a, 'tcx> Lift<'tcx> for ty::adjustment::Adjust<'a> { Some(ty::adjustment::Adjust::ReifyFnPointer), ty::adjustment::Adjust::UnsafeFnPointer => Some(ty::adjustment::Adjust::UnsafeFnPointer), - ty::adjustment::Adjust::ClosureFnPointer => - Some(ty::adjustment::Adjust::ClosureFnPointer), + ty::adjustment::Adjust::ClosureFnPointer(unsafety) => + Some(ty::adjustment::Adjust::ClosureFnPointer(unsafety)), ty::adjustment::Adjust::MutToConstPointer => Some(ty::adjustment::Adjust::MutToConstPointer), ty::adjustment::Adjust::Unsize => @@ -1187,7 +1187,7 @@ EnumTypeFoldableImpl! { (ty::adjustment::Adjust::NeverToAny), (ty::adjustment::Adjust::ReifyFnPointer), (ty::adjustment::Adjust::UnsafeFnPointer), - (ty::adjustment::Adjust::ClosureFnPointer), + (ty::adjustment::Adjust::ClosureFnPointer)(a), (ty::adjustment::Adjust::MutToConstPointer), (ty::adjustment::Adjust::Unsize), (ty::adjustment::Adjust::Deref)(a), diff --git a/src/librustc_codegen_ssa/mir/rvalue.rs b/src/librustc_codegen_ssa/mir/rvalue.rs index 7a31c5b3950e..5e8723029651 100644 --- a/src/librustc_codegen_ssa/mir/rvalue.rs +++ b/src/librustc_codegen_ssa/mir/rvalue.rs @@ -193,7 +193,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { } } } - mir::CastKind::ClosureFnPointer => { + mir::CastKind::ClosureFnPointer(_) => { match operand.layout.ty.sty { ty::Closure(def_id, substs) => { let instance = monomorphize::resolve_closure( diff --git a/src/librustc_mir/borrow_check/nll/type_check/mod.rs b/src/librustc_mir/borrow_check/nll/type_check/mod.rs index a3561515aaa3..3b559b28f123 100644 --- a/src/librustc_mir/borrow_check/nll/type_check/mod.rs +++ b/src/librustc_mir/borrow_check/nll/type_check/mod.rs @@ -1999,14 +1999,14 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> { } } - CastKind::ClosureFnPointer => { + CastKind::ClosureFnPointer(unsafety) => { let sig = match op.ty(mir, tcx).sty { ty::Closure(def_id, substs) => { substs.closure_sig_ty(def_id, tcx).fn_sig(tcx) } _ => bug!(), }; - let ty_fn_ptr_from = tcx.coerce_closure_fn_ty(sig); + let ty_fn_ptr_from = tcx.coerce_closure_fn_ty(sig, *unsafety); if let Err(terr) = self.eq_types( ty_fn_ptr_from, diff --git a/src/librustc_mir/build/expr/as_rvalue.rs b/src/librustc_mir/build/expr/as_rvalue.rs index 9e12a7e6fa93..84f74484e606 100644 --- a/src/librustc_mir/build/expr/as_rvalue.rs +++ b/src/librustc_mir/build/expr/as_rvalue.rs @@ -162,9 +162,9 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { let source = unpack!(block = this.as_operand(block, scope, source)); block.and(Rvalue::Cast(CastKind::UnsafeFnPointer, source, expr.ty)) } - ExprKind::ClosureFnPointer { source } => { + ExprKind::ClosureFnPointer { source, unsafety } => { let source = unpack!(block = this.as_operand(block, scope, source)); - block.and(Rvalue::Cast(CastKind::ClosureFnPointer, source, expr.ty)) + block.and(Rvalue::Cast(CastKind::ClosureFnPointer(unsafety), source, expr.ty)) } ExprKind::MutToConstPointer { source } => { let source = unpack!(block = this.as_operand(block, scope, source)); diff --git a/src/librustc_mir/hair/cx/expr.rs b/src/librustc_mir/hair/cx/expr.rs index e4f92f81e9ff..91113dc2271b 100644 --- a/src/librustc_mir/hair/cx/expr.rs +++ b/src/librustc_mir/hair/cx/expr.rs @@ -81,8 +81,8 @@ fn apply_adjustment<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>, Adjust::UnsafeFnPointer => { ExprKind::UnsafeFnPointer { source: expr.to_ref() } } - Adjust::ClosureFnPointer => { - ExprKind::ClosureFnPointer { source: expr.to_ref() } + Adjust::ClosureFnPointer(unsafety) => { + ExprKind::ClosureFnPointer { source: expr.to_ref(), unsafety } } Adjust::NeverToAny => { ExprKind::NeverToAny { source: expr.to_ref() } diff --git a/src/librustc_mir/hair/mod.rs b/src/librustc_mir/hair/mod.rs index 3a38876bb680..a661649db0fd 100644 --- a/src/librustc_mir/hair/mod.rs +++ b/src/librustc_mir/hair/mod.rs @@ -185,6 +185,7 @@ pub enum ExprKind<'tcx> { }, ClosureFnPointer { source: ExprRef<'tcx>, + unsafety: hir::Unsafety, }, UnsafeFnPointer { source: ExprRef<'tcx>, diff --git a/src/librustc_mir/interpret/cast.rs b/src/librustc_mir/interpret/cast.rs index 003c2182d0b4..b60cc99e51c1 100644 --- a/src/librustc_mir/interpret/cast.rs +++ b/src/librustc_mir/interpret/cast.rs @@ -105,7 +105,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> InterpretCx<'a, 'mir, 'tcx, M> } } - ClosureFnPointer => { + ClosureFnPointer(_) => { // The src operand does not matter, just its type match src.layout.ty.sty { ty::Closure(def_id, substs) => { diff --git a/src/librustc_mir/monomorphize/collector.rs b/src/librustc_mir/monomorphize/collector.rs index 0e8ab2ba2a5a..45b346b64136 100644 --- a/src/librustc_mir/monomorphize/collector.rs +++ b/src/librustc_mir/monomorphize/collector.rs @@ -563,7 +563,7 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> { ); visit_fn_use(self.tcx, fn_ty, false, &mut self.output); } - mir::Rvalue::Cast(mir::CastKind::ClosureFnPointer, ref operand, _) => { + mir::Rvalue::Cast(mir::CastKind::ClosureFnPointer(_), ref operand, _) => { let source_ty = operand.ty(self.mir, self.tcx); let source_ty = self.tcx.subst_and_normalize_erasing_regions( self.param_substs, diff --git a/src/librustc_mir/transform/qualify_consts.rs b/src/librustc_mir/transform/qualify_consts.rs index 0b9ad85e6b1c..9bd5fce31f1a 100644 --- a/src/librustc_mir/transform/qualify_consts.rs +++ b/src/librustc_mir/transform/qualify_consts.rs @@ -1105,7 +1105,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Checker<'a, 'tcx> { Rvalue::CheckedBinaryOp(..) | Rvalue::Cast(CastKind::ReifyFnPointer, ..) | Rvalue::Cast(CastKind::UnsafeFnPointer, ..) | - Rvalue::Cast(CastKind::ClosureFnPointer, ..) | + Rvalue::Cast(CastKind::ClosureFnPointer(_), ..) | Rvalue::Cast(CastKind::Unsize, ..) | Rvalue::Cast(CastKind::MutToConstPointer, ..) | Rvalue::Discriminant(..) | diff --git a/src/librustc_mir/transform/qualify_min_const_fn.rs b/src/librustc_mir/transform/qualify_min_const_fn.rs index 8742c5d759c8..87459571b529 100644 --- a/src/librustc_mir/transform/qualify_min_const_fn.rs +++ b/src/librustc_mir/transform/qualify_min_const_fn.rs @@ -156,7 +156,7 @@ fn check_rvalue( check_operand(tcx, mir, operand, span) } Rvalue::Cast(CastKind::UnsafeFnPointer, _, _) | - Rvalue::Cast(CastKind::ClosureFnPointer, _, _) | + Rvalue::Cast(CastKind::ClosureFnPointer(_), _, _) | Rvalue::Cast(CastKind::ReifyFnPointer, _, _) => Err(( span, "function pointer casts are not allowed in const fn".into(), diff --git a/src/librustc_passes/rvalue_promotion.rs b/src/librustc_passes/rvalue_promotion.rs index a0a0d7be1b95..7c37c38f2d74 100644 --- a/src/librustc_passes/rvalue_promotion.rs +++ b/src/librustc_passes/rvalue_promotion.rs @@ -586,7 +586,7 @@ fn check_adjustments<'a, 'tcx>( Adjust::NeverToAny | Adjust::ReifyFnPointer | Adjust::UnsafeFnPointer | - Adjust::ClosureFnPointer | + Adjust::ClosureFnPointer(_) | Adjust::MutToConstPointer | Adjust::Borrow(_) | Adjust::Unsize => {} diff --git a/src/librustc_typeck/check/coercion.rs b/src/librustc_typeck/check/coercion.rs index ac8b639edbfa..c470bc09e8cd 100644 --- a/src/librustc_typeck/check/coercion.rs +++ b/src/librustc_typeck/check/coercion.rs @@ -225,7 +225,8 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> { } ty::Closure(def_id_a, substs_a) => { // Non-capturing closures are coercible to - // function pointers + // function pointers or unsafe function pointers. + // It cannot convert closures that require unsafe. self.coerce_closure_to_fn(a, def_id_a, substs_a, b) } _ => { @@ -714,16 +715,19 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> { let hir_id_a = self.tcx.hir().as_local_hir_id(def_id_a).unwrap(); match b.sty { - ty::FnPtr(_) if self.tcx.with_freevars(hir_id_a, |v| v.is_empty()) => { + ty::FnPtr(fn_ty) if self.tcx.with_freevars(hir_id_a, |v| v.is_empty()) => { // We coerce the closure, which has fn type // `extern "rust-call" fn((arg0,arg1,...)) -> _` // to // `fn(arg0,arg1,...) -> _` + // or + // `unsafe fn(arg0,arg1,...) -> _` let sig = self.closure_sig(def_id_a, substs_a); - let pointer_ty = self.tcx.coerce_closure_fn_ty(sig); + let unsafety = fn_ty.unsafety(); + let pointer_ty = self.tcx.coerce_closure_fn_ty(sig, unsafety); debug!("coerce_closure_to_fn(a={:?}, b={:?}, pty={:?})", a, b, pointer_ty); - self.unify_and(pointer_ty, b, simple(Adjust::ClosureFnPointer)) + self.unify_and(pointer_ty, b, simple(Adjust::ClosureFnPointer(unsafety))) } _ => self.unify_and(a, b, identity), } diff --git a/src/test/compile-fail/coerce-unsafe-closure-to-unsafe-fn-ptr.rs b/src/test/compile-fail/coerce-unsafe-closure-to-unsafe-fn-ptr.rs new file mode 100644 index 000000000000..36777693faba --- /dev/null +++ b/src/test/compile-fail/coerce-unsafe-closure-to-unsafe-fn-ptr.rs @@ -0,0 +1,5 @@ +fn main() { + let _: unsafe fn() = || { ::std::pin::Pin::new_unchecked(&0_u8); }; + //~^ ERROR E0133 + let _: unsafe fn() = || unsafe { ::std::pin::Pin::new_unchecked(&0_u8); }; // OK +} diff --git a/src/test/run-pass/typeck-closure-to-unsafe-fn-ptr.rs b/src/test/run-pass/typeck-closure-to-unsafe-fn-ptr.rs new file mode 100644 index 000000000000..fe15b912d602 --- /dev/null +++ b/src/test/run-pass/typeck-closure-to-unsafe-fn-ptr.rs @@ -0,0 +1,7 @@ +unsafe fn call_unsafe(func: unsafe fn() -> ()) -> () { + func() +} + +pub fn main() { + unsafe { call_unsafe(|| {}); } +}