Skip to content

Commit c2bc344

Browse files
committed
Auto merge of rust-lang#77039 - ecstatic-morse:rollup-qv3jj4a, r=ecstatic-morse
Rollup of 13 pull requests Successful merges: - rust-lang#72734 (Reduce duplicate in liballoc reserve error handling) - rust-lang#76131 (Don't use `zip` to compare iterators during pretty-print hack) - rust-lang#76150 (Don't recommend ManuallyDrop to customize drop order) - rust-lang#76275 (Implementation of Write for some immutable ref structs) - rust-lang#76489 (Add explanation for E0756) - rust-lang#76581 (do not ICE on bound variables, return `TooGeneric` instead) - rust-lang#76655 (Make some methods of `Pin` unstable const) - rust-lang#76783 (Only get ImplKind::Impl once) - rust-lang#76807 (Use const-checking to forbid use of unstable features in const-stable functions) - rust-lang#76888 (use if let instead of single match arm expressions) - rust-lang#76914 (extend `Ty` and `TyCtxt` lints to self types) - rust-lang#77022 (Reduce boilerplate for BytePos and CharPos) - rust-lang#77032 (lint missing docs for extern items) Failed merges: r? `@ghost`
2 parents 4519845 + 0863f9a commit c2bc344

Some content is hidden

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

49 files changed

+762
-322
lines changed

compiler/rustc_error_codes/src/error_codes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ E0752: include_str!("./error_codes/E0752.md"),
441441
E0753: include_str!("./error_codes/E0753.md"),
442442
E0754: include_str!("./error_codes/E0754.md"),
443443
E0755: include_str!("./error_codes/E0755.md"),
444+
E0756: include_str!("./error_codes/E0756.md"),
444445
E0758: include_str!("./error_codes/E0758.md"),
445446
E0759: include_str!("./error_codes/E0759.md"),
446447
E0760: include_str!("./error_codes/E0760.md"),
@@ -633,7 +634,6 @@ E0774: include_str!("./error_codes/E0774.md"),
633634
E0722, // Malformed `#[optimize]` attribute
634635
E0726, // non-explicit (not `'_`) elided lifetime in unsupported position
635636
// E0738, // Removed; errored on `#[track_caller] fn`s in `extern "Rust" { ... }`.
636-
E0756, // `#[ffi_const]` is only allowed on foreign functions
637637
E0757, // `#[ffi_const]` functions cannot be `#[ffi_pure]`
638638
E0772, // `'static' obligation coming from `impl dyn Trait {}` or `impl Foo for dyn Bar {}`.
639639
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
The `ffi_const` attribute was used on something other than a foreign function
2+
declaration.
3+
4+
Erroneous code example:
5+
6+
```compile_fail,E0756
7+
#![feature(ffi_const)]
8+
9+
#[ffi_const] // error!
10+
pub fn foo() {}
11+
# fn main() {}
12+
```
13+
14+
The `ffi_const` attribute can only be used on foreign function declarations
15+
which have no side effects except for their return value:
16+
17+
```
18+
#![feature(ffi_const)]
19+
20+
extern "C" {
21+
#[ffi_const] // ok!
22+
pub fn strlen(s: *const i8) -> i32;
23+
}
24+
# fn main() {}
25+
```
26+
27+
You can get more information about it in the [unstable Rust Book].
28+
29+
[unstable Rust Book]: https://doc.rust-lang.org/nightly/unstable-book/language-features/ffi-const.html

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -488,18 +488,16 @@ impl<'tcx> Visitor<'tcx> for HirTraitObjectVisitor {
488488
}
489489

490490
fn visit_ty(&mut self, t: &'tcx hir::Ty<'tcx>) {
491-
match t.kind {
492-
TyKind::TraitObject(
493-
poly_trait_refs,
494-
Lifetime { name: LifetimeName::ImplicitObjectLifetimeDefault, .. },
495-
) => {
496-
for ptr in poly_trait_refs {
497-
if Some(self.1) == ptr.trait_ref.trait_def_id() {
498-
self.0.push(ptr.span);
499-
}
491+
if let TyKind::TraitObject(
492+
poly_trait_refs,
493+
Lifetime { name: LifetimeName::ImplicitObjectLifetimeDefault, .. },
494+
) = t.kind
495+
{
496+
for ptr in poly_trait_refs {
497+
if Some(self.1) == ptr.trait_ref.trait_def_id() {
498+
self.0.push(ptr.span);
500499
}
501500
}
502-
_ => {}
503501
}
504502
walk_ty(self, t);
505503
}

compiler/rustc_lint/src/builtin.rs

+13
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,19 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
613613
);
614614
}
615615

616+
fn check_foreign_item(&mut self, cx: &LateContext<'_>, foreign_item: &hir::ForeignItem<'_>) {
617+
let def_id = cx.tcx.hir().local_def_id(foreign_item.hir_id);
618+
let (article, desc) = cx.tcx.article_and_description(def_id.to_def_id());
619+
self.check_missing_docs_attrs(
620+
cx,
621+
Some(foreign_item.hir_id),
622+
&foreign_item.attrs,
623+
foreign_item.span,
624+
article,
625+
desc,
626+
);
627+
}
628+
616629
fn check_struct_field(&mut self, cx: &LateContext<'_>, sf: &hir::StructField<'_>) {
617630
if !sf.is_positional() {
618631
self.check_missing_docs_attrs(

compiler/rustc_lint/src/internal.rs

+27-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext}
55
use rustc_ast::{Item, ItemKind};
66
use rustc_data_structures::fx::FxHashMap;
77
use rustc_errors::Applicability;
8+
use rustc_hir::def::Res;
89
use rustc_hir::{GenericArg, HirId, MutTy, Mutability, Path, PathSegment, QPath, Ty, TyKind};
10+
use rustc_middle::ty;
911
use rustc_session::{declare_lint_pass, declare_tool_lint, impl_lint_pass};
1012
use rustc_span::hygiene::{ExpnKind, MacroKind};
1113
use rustc_span::symbol::{sym, Ident, Symbol};
@@ -177,11 +179,31 @@ fn lint_ty_kind_usage(cx: &LateContext<'_>, segment: &PathSegment<'_>) -> bool {
177179
fn is_ty_or_ty_ctxt(cx: &LateContext<'_>, ty: &Ty<'_>) -> Option<String> {
178180
if let TyKind::Path(qpath) = &ty.kind {
179181
if let QPath::Resolved(_, path) = qpath {
180-
let did = path.res.opt_def_id()?;
181-
if cx.tcx.is_diagnostic_item(sym::Ty, did) {
182-
return Some(format!("Ty{}", gen_args(path.segments.last().unwrap())));
183-
} else if cx.tcx.is_diagnostic_item(sym::TyCtxt, did) {
184-
return Some(format!("TyCtxt{}", gen_args(path.segments.last().unwrap())));
182+
match path.res {
183+
Res::Def(_, did) => {
184+
if cx.tcx.is_diagnostic_item(sym::Ty, did) {
185+
return Some(format!("Ty{}", gen_args(path.segments.last().unwrap())));
186+
} else if cx.tcx.is_diagnostic_item(sym::TyCtxt, did) {
187+
return Some(format!("TyCtxt{}", gen_args(path.segments.last().unwrap())));
188+
}
189+
}
190+
// Only lint on `&Ty` and `&TyCtxt` if it is used outside of a trait.
191+
Res::SelfTy(None, Some((did, _))) => {
192+
if let ty::Adt(adt, substs) = cx.tcx.type_of(did).kind() {
193+
if cx.tcx.is_diagnostic_item(sym::Ty, adt.did) {
194+
// NOTE: This path is currently unreachable as `Ty<'tcx>` is
195+
// defined as a type alias meaning that `impl<'tcx> Ty<'tcx>`
196+
// is not actually allowed.
197+
//
198+
// I(@lcnr) still kept this branch in so we don't miss this
199+
// if we ever change it in the future.
200+
return Some(format!("Ty<{}>", substs[0]));
201+
} else if cx.tcx.is_diagnostic_item(sym::TyCtxt, adt.did) {
202+
return Some(format!("TyCtxt<{}>", substs[0]));
203+
}
204+
}
205+
}
206+
_ => (),
185207
}
186208
}
187209
}

compiler/rustc_middle/src/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1179,7 +1179,7 @@ impl<'tcx> TyCtxt<'tcx> {
11791179
self.mk_const(ty::Const { val: ty::ConstKind::Error(DelaySpanBugEmitted(())), ty })
11801180
}
11811181

1182-
pub fn consider_optimizing<T: Fn() -> String>(&self, msg: T) -> bool {
1182+
pub fn consider_optimizing<T: Fn() -> String>(self, msg: T) -> bool {
11831183
let cname = self.crate_name(LOCAL_CRATE).as_str();
11841184
self.sess.consider_optimizing(&cname, msg)
11851185
}

compiler/rustc_middle/src/ty/error.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -834,14 +834,11 @@ fn foo(&self) -> Self::T { String::new() }
834834
kind: hir::ItemKind::Impl { items, .. }, ..
835835
})) => {
836836
for item in &items[..] {
837-
match item.kind {
838-
hir::AssocItemKind::Type => {
839-
if self.type_of(self.hir().local_def_id(item.id.hir_id)) == found {
840-
db.span_label(item.span, "expected this associated type");
841-
return true;
842-
}
837+
if let hir::AssocItemKind::Type = item.kind {
838+
if self.type_of(self.hir().local_def_id(item.id.hir_id)) == found {
839+
db.span_label(item.span, "expected this associated type");
840+
return true;
843841
}
844-
_ => {}
845842
}
846843
}
847844
}
@@ -853,7 +850,7 @@ fn foo(&self) -> Self::T { String::new() }
853850
/// Given a slice of `hir::GenericBound`s, if any of them corresponds to the `trait_ref`
854851
/// requirement, provide a strucuted suggestion to constrain it to a given type `ty`.
855852
fn constrain_generic_bound_associated_type_structured_suggestion(
856-
&self,
853+
self,
857854
db: &mut DiagnosticBuilder<'_>,
858855
trait_ref: &ty::TraitRef<'tcx>,
859856
bounds: hir::GenericBounds<'_>,
@@ -877,7 +874,7 @@ fn foo(&self) -> Self::T { String::new() }
877874
/// Given a span corresponding to a bound, provide a structured suggestion to set an
878875
/// associated type to a given type `ty`.
879876
fn constrain_associated_type_structured_suggestion(
880-
&self,
877+
self,
881878
db: &mut DiagnosticBuilder<'_>,
882879
span: Span,
883880
assoc: &ty::AssocItem,

compiler/rustc_middle/src/ty/layout.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1259,11 +1259,11 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
12591259
tcx.layout_raw(param_env.and(normalized))?
12601260
}
12611261

1262-
ty::Bound(..) | ty::Placeholder(..) | ty::GeneratorWitness(..) | ty::Infer(_) => {
1262+
ty::Placeholder(..) | ty::GeneratorWitness(..) | ty::Infer(_) => {
12631263
bug!("Layout::compute: unexpected type `{}`", ty)
12641264
}
12651265

1266-
ty::Param(_) | ty::Error(_) => {
1266+
ty::Bound(..) | ty::Param(_) | ty::Error(_) => {
12671267
return Err(LayoutError::Unknown(ty));
12681268
}
12691269
})

compiler/rustc_middle/src/ty/print/pretty.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -2125,17 +2125,10 @@ fn for_each_def(tcx: TyCtxt<'_>, mut collect_fn: impl for<'b> FnMut(&'b Ident, N
21252125
// Iterate all local crate items no matter where they are defined.
21262126
let hir = tcx.hir();
21272127
for item in hir.krate().items.values() {
2128-
if item.ident.name.as_str().is_empty() {
2128+
if item.ident.name.as_str().is_empty() || matches!(item.kind, ItemKind::Use(_, _)) {
21292129
continue;
21302130
}
21312131

2132-
match item.kind {
2133-
ItemKind::Use(_, _) => {
2134-
continue;
2135-
}
2136-
_ => {}
2137-
}
2138-
21392132
if let Some(local_def_id) = hir.definitions().opt_hir_id_to_local_def_id(item.hir_id) {
21402133
let def_id = local_def_id.to_def_id();
21412134
let ns = tcx.def_kind(def_id).ns().unwrap_or(Namespace::TypeNS);

compiler/rustc_mir/src/interpret/operand.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -549,15 +549,13 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
549549
};
550550
// Early-return cases.
551551
let val_val = match val.val {
552-
ty::ConstKind::Param(_) => throw_inval!(TooGeneric),
552+
ty::ConstKind::Param(_) | ty::ConstKind::Bound(..) => throw_inval!(TooGeneric),
553553
ty::ConstKind::Error(_) => throw_inval!(TypeckError(ErrorReported)),
554554
ty::ConstKind::Unevaluated(def, substs, promoted) => {
555555
let instance = self.resolve(def, substs)?;
556556
return Ok(self.eval_to_allocation(GlobalId { instance, promoted })?.into());
557557
}
558-
ty::ConstKind::Infer(..)
559-
| ty::ConstKind::Bound(..)
560-
| ty::ConstKind::Placeholder(..) => {
558+
ty::ConstKind::Infer(..) | ty::ConstKind::Placeholder(..) => {
561559
span_bug!(self.cur_span(), "const_to_op: Unexpected ConstKind {:?}", val)
562560
}
563561
ty::ConstKind::Value(val_val) => val_val,

compiler/rustc_mir/src/transform/check_consts/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
//! has interior mutability or needs to be dropped, as well as the visitor that emits errors when
55
//! it finds operations that are invalid in a certain context.
66
7+
use rustc_attr as attr;
78
use rustc_hir as hir;
89
use rustc_hir::def_id::{DefId, LocalDefId};
910
use rustc_middle::mir;
1011
use rustc_middle::ty::{self, TyCtxt};
12+
use rustc_span::Symbol;
1113

1214
pub use self::qualifs::Qualif;
1315

@@ -55,3 +57,9 @@ impl ConstCx<'mir, 'tcx> {
5557
pub fn is_lang_panic_fn(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
5658
Some(def_id) == tcx.lang_items().panic_fn() || Some(def_id) == tcx.lang_items().begin_panic_fn()
5759
}
60+
61+
pub fn allow_internal_unstable(tcx: TyCtxt<'tcx>, def_id: DefId, feature_gate: Symbol) -> bool {
62+
let attrs = tcx.get_attrs(def_id);
63+
attr::allow_internal_unstable(&tcx.sess, attrs)
64+
.map_or(false, |mut features| features.any(|name| name == feature_gate))
65+
}

0 commit comments

Comments
 (0)