diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 38deb8eaaae6b..551e6a57b32bf 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -3183,6 +3183,20 @@ pub enum Node<'hir> { } impl<'hir> Node<'hir> { + /// Get the identifier of this `Node`, if applicable. + /// + /// # Edge cases + /// + /// Calling `.ident()` on a [`Node::Ctor`] will return `None` + /// because `Ctor`s do not have identifiers themselves. + /// Instead, call `.ident()` on the parent struct/variant, like so: + /// + /// ```ignore (illustrative) + /// ctor + /// .ctor_hir_id() + /// .and_then(|ctor_id| tcx.hir().find(tcx.hir().get_parent_node(ctor_id))) + /// .and_then(|parent| parent.ident()) + /// ``` pub fn ident(&self) -> Option { match self { Node::TraitItem(TraitItem { ident, .. }) @@ -3191,8 +3205,25 @@ impl<'hir> Node<'hir> { | Node::Field(FieldDef { ident, .. }) | Node::Variant(Variant { ident, .. }) | Node::MacroDef(MacroDef { ident, .. }) - | Node::Item(Item { ident, .. }) => Some(*ident), - _ => None, + | Node::Item(Item { ident, .. }) + | Node::PathSegment(PathSegment { ident, .. }) => Some(*ident), + Node::Lifetime(lt) => Some(lt.name.ident()), + Node::GenericParam(p) => Some(p.name.ident()), + Node::Param(..) + | Node::AnonConst(..) + | Node::Expr(..) + | Node::Stmt(..) + | Node::Block(..) + | Node::Ctor(..) + | Node::Pat(..) + | Node::Binding(..) + | Node::Arm(..) + | Node::Local(..) + | Node::Visibility(..) + | Node::Crate(..) + | Node::Ty(..) + | Node::TraitRef(..) + | Node::Infer(..) => None, } } diff --git a/compiler/rustc_mir/src/borrow_check/diagnostics/bound_region_errors.rs b/compiler/rustc_mir/src/borrow_check/diagnostics/bound_region_errors.rs index d0284dd030236..ac30093ba8260 100644 --- a/compiler/rustc_mir/src/borrow_check/diagnostics/bound_region_errors.rs +++ b/compiler/rustc_mir/src/borrow_check/diagnostics/bound_region_errors.rs @@ -9,7 +9,7 @@ use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable}; use rustc_span::Span; use rustc_trait_selection::traits::query::type_op; use rustc_trait_selection::traits::{SelectionContext, TraitEngineExt as _}; -use rustc_traits::type_op_prove_predicate_with_span; +use rustc_traits::{type_op_ascribe_user_type_with_span, type_op_prove_predicate_with_span}; use std::fmt; use std::rc::Rc; @@ -104,10 +104,11 @@ impl<'tcx, T: Copy + fmt::Display + TypeFoldable<'tcx> + 'tcx> ToUniverseInfo<'t impl<'tcx> ToUniverseInfo<'tcx> for Canonical<'tcx, ty::ParamEnvAnd<'tcx, type_op::AscribeUserType<'tcx>>> { - fn to_universe_info(self, _base_universe: ty::UniverseIndex) -> UniverseInfo<'tcx> { - // Ascribe user type isn't usually called on types that have different - // bound regions. - UniverseInfo::other() + fn to_universe_info(self, base_universe: ty::UniverseIndex) -> UniverseInfo<'tcx> { + UniverseInfo(UniverseInfoInner::TypeOp(Rc::new(AscribeUserTypeQuery { + canonical_query: self, + base_universe, + }))) } } @@ -267,6 +268,37 @@ where } } +struct AscribeUserTypeQuery<'tcx> { + canonical_query: Canonical<'tcx, ty::ParamEnvAnd<'tcx, type_op::AscribeUserType<'tcx>>>, + base_universe: ty::UniverseIndex, +} + +impl TypeOpInfo<'tcx> for AscribeUserTypeQuery<'tcx> { + fn fallback_error(&self, tcx: TyCtxt<'tcx>, span: Span) -> DiagnosticBuilder<'tcx> { + // FIXME: This error message isn't great, but it doesn't show up in the existing UI tests, + // and is only the fallback when the nice error fails. Consider improving this some more. + tcx.sess.struct_span_err(span, "higher-ranked lifetime error") + } + + fn base_universe(&self) -> ty::UniverseIndex { + self.base_universe + } + + fn nice_error( + &self, + tcx: TyCtxt<'tcx>, + span: Span, + placeholder_region: ty::Region<'tcx>, + error_region: Option>, + ) -> Option> { + tcx.infer_ctxt().enter_with_canonical(span, &self.canonical_query, |ref infcx, key, _| { + let mut fulfill_cx = >::new(tcx); + type_op_ascribe_user_type_with_span(infcx, &mut *fulfill_cx, key, Some(span)).ok()?; + try_extract_error_from_fulfill_cx(fulfill_cx, infcx, placeholder_region, error_region) + }) + } +} + fn try_extract_error_from_fulfill_cx<'tcx>( mut fulfill_cx: Box + 'tcx>, infcx: &InferCtxt<'_, 'tcx>, diff --git a/compiler/rustc_trait_selection/src/infer.rs b/compiler/rustc_trait_selection/src/infer.rs index ea074192d23b1..c90649353e80f 100644 --- a/compiler/rustc_trait_selection/src/infer.rs +++ b/compiler/rustc_trait_selection/src/infer.rs @@ -44,6 +44,10 @@ pub trait InferCtxtExt<'tcx> { /// - the self type /// - the *other* type parameters of the trait, excluding the self-type /// - the parameter environment + /// + /// Invokes `evaluate_obligation`, so in the event that evaluating + /// `Ty: Trait` causes overflow, EvaluatedToRecur (or EvaluatedToUnknown) + /// will be returned. fn type_implements_trait( &self, trait_def_id: DefId, @@ -117,7 +121,7 @@ impl<'cx, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'cx, 'tcx> { recursion_depth: 0, predicate: trait_ref.without_const().to_predicate(self.tcx), }; - self.evaluate_obligation_no_overflow(&obligation) + self.evaluate_obligation(&obligation).unwrap_or(traits::EvaluationResult::EvaluatedToErr) } } diff --git a/compiler/rustc_traits/src/lib.rs b/compiler/rustc_traits/src/lib.rs index 8dd7c5bdfaebc..48c46c3069328 100644 --- a/compiler/rustc_traits/src/lib.rs +++ b/compiler/rustc_traits/src/lib.rs @@ -19,7 +19,7 @@ mod normalize_erasing_regions; mod normalize_projection_ty; mod type_op; -pub use type_op::type_op_prove_predicate_with_span; +pub use type_op::{type_op_ascribe_user_type_with_span, type_op_prove_predicate_with_span}; use rustc_middle::ty::query::Providers; diff --git a/compiler/rustc_traits/src/type_op.rs b/compiler/rustc_traits/src/type_op.rs index c2e0a9987858e..a76fb84261615 100644 --- a/compiler/rustc_traits/src/type_op.rs +++ b/compiler/rustc_traits/src/type_op.rs @@ -40,18 +40,28 @@ fn type_op_ascribe_user_type<'tcx>( canonicalized: Canonical<'tcx, ParamEnvAnd<'tcx, AscribeUserType<'tcx>>>, ) -> Result<&'tcx Canonical<'tcx, QueryResponse<'tcx, ()>>, NoSolution> { tcx.infer_ctxt().enter_canonical_trait_query(&canonicalized, |infcx, fulfill_cx, key| { - let (param_env, AscribeUserType { mir_ty, def_id, user_substs }) = key.into_parts(); - - debug!( - "type_op_ascribe_user_type: mir_ty={:?} def_id={:?} user_substs={:?}", - mir_ty, def_id, user_substs - ); + type_op_ascribe_user_type_with_span(infcx, fulfill_cx, key, None) + }) +} - let mut cx = AscribeUserTypeCx { infcx, param_env, fulfill_cx }; - cx.relate_mir_and_user_ty(mir_ty, def_id, user_substs)?; +/// The core of the `type_op_ascribe_user_type` query: for diagnostics purposes in NLL HRTB errors, +/// this query can be re-run to better track the span of the obligation cause, and improve the error +/// message. Do not call directly unless you're in that very specific context. +pub fn type_op_ascribe_user_type_with_span<'a, 'tcx: 'a>( + infcx: &'a InferCtxt<'a, 'tcx>, + fulfill_cx: &'a mut dyn TraitEngine<'tcx>, + key: ParamEnvAnd<'tcx, AscribeUserType<'tcx>>, + span: Option, +) -> Result<(), NoSolution> { + let (param_env, AscribeUserType { mir_ty, def_id, user_substs }) = key.into_parts(); + debug!( + "type_op_ascribe_user_type: mir_ty={:?} def_id={:?} user_substs={:?}", + mir_ty, def_id, user_substs + ); - Ok(()) - }) + let mut cx = AscribeUserTypeCx { infcx, param_env, fulfill_cx }; + cx.relate_mir_and_user_ty(mir_ty, def_id, user_substs, span)?; + Ok(()) } struct AscribeUserTypeCx<'me, 'tcx> { @@ -85,10 +95,15 @@ impl AscribeUserTypeCx<'me, 'tcx> { Ok(()) } - fn prove_predicate(&mut self, predicate: Predicate<'tcx>) { + fn prove_predicate(&mut self, predicate: Predicate<'tcx>, span: Option) { + let cause = if let Some(span) = span { + ObligationCause::dummy_with_span(span) + } else { + ObligationCause::dummy() + }; self.fulfill_cx.register_predicate_obligation( self.infcx, - Obligation::new(ObligationCause::dummy(), self.param_env, predicate), + Obligation::new(cause, self.param_env, predicate), ); } @@ -108,6 +123,7 @@ impl AscribeUserTypeCx<'me, 'tcx> { mir_ty: Ty<'tcx>, def_id: DefId, user_substs: UserSubsts<'tcx>, + span: Option, ) -> Result<(), NoSolution> { let UserSubsts { user_self_ty, substs } = user_substs; let tcx = self.tcx(); @@ -129,7 +145,7 @@ impl AscribeUserTypeCx<'me, 'tcx> { debug!(?instantiated_predicates.predicates); for instantiated_predicate in instantiated_predicates.predicates { let instantiated_predicate = self.normalize(instantiated_predicate); - self.prove_predicate(instantiated_predicate); + self.prove_predicate(instantiated_predicate, span); } if let Some(UserSelfTy { impl_def_id, self_ty }) = user_self_ty { @@ -141,6 +157,7 @@ impl AscribeUserTypeCx<'me, 'tcx> { self.prove_predicate( ty::PredicateKind::WellFormed(impl_self_ty.into()).to_predicate(self.tcx()), + span, ); } @@ -155,7 +172,10 @@ impl AscribeUserTypeCx<'me, 'tcx> { // them? This would only be relevant if some input // type were ill-formed but did not appear in `ty`, // which...could happen with normalization... - self.prove_predicate(ty::PredicateKind::WellFormed(ty.into()).to_predicate(self.tcx())); + self.prove_predicate( + ty::PredicateKind::WellFormed(ty.into()).to_predicate(self.tcx()), + span, + ); Ok(()) } } diff --git a/compiler/rustc_ty_utils/src/ty.rs b/compiler/rustc_ty_utils/src/ty.rs index 8332e7384110e..c10f6dc3401bd 100644 --- a/compiler/rustc_ty_utils/src/ty.rs +++ b/compiler/rustc_ty_utils/src/ty.rs @@ -223,7 +223,18 @@ fn associated_items(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AssocItems<'_> { } fn def_ident_span(tcx: TyCtxt<'_>, def_id: DefId) -> Option { - tcx.hir().get_if_local(def_id).and_then(|node| node.ident()).map(|ident| ident.span) + tcx.hir() + .get_if_local(def_id) + .and_then(|node| match node { + // A `Ctor` doesn't have an identifier itself, but its parent + // struct/variant does. Compare with `hir::Map::opt_span`. + hir::Node::Ctor(ctor) => ctor + .ctor_hir_id() + .and_then(|ctor_id| tcx.hir().find(tcx.hir().get_parent_node(ctor_id))) + .and_then(|parent| parent.ident()), + _ => node.ident(), + }) + .map(|ident| ident.span) } /// If the given `DefId` describes an item belonging to a trait, diff --git a/compiler/rustc_typeck/src/check/pat.rs b/compiler/rustc_typeck/src/check/pat.rs index 392262628c0c6..016b3f7a87cf3 100644 --- a/compiler/rustc_typeck/src/check/pat.rs +++ b/compiler/rustc_typeck/src/check/pat.rs @@ -15,7 +15,7 @@ use rustc_span::hygiene::DesugaringKind; use rustc_span::lev_distance::find_best_match_for_name; use rustc_span::source_map::{Span, Spanned}; use rustc_span::symbol::Ident; -use rustc_span::{BytePos, DUMMY_SP}; +use rustc_span::{BytePos, MultiSpan, DUMMY_SP}; use rustc_trait_selection::autoderef::Autoderef; use rustc_trait_selection::traits::{ObligationCause, Pattern}; use ty::VariantDef; @@ -990,10 +990,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ) { let subpats_ending = pluralize!(subpats.len()); let fields_ending = pluralize!(fields.len()); + + let subpat_spans = if subpats.is_empty() { + vec![pat_span] + } else { + subpats.iter().map(|p| p.span).collect() + }; + let last_subpat_span = *subpat_spans.last().unwrap(); let res_span = self.tcx.def_span(res.def_id()); + let def_ident_span = self.tcx.def_ident_span(res.def_id()).unwrap_or(res_span); + let field_def_spans = if fields.is_empty() { + vec![res_span] + } else { + fields.iter().map(|f| f.ident.span).collect() + }; + let last_field_def_span = *field_def_spans.last().unwrap(); + let mut err = struct_span_err!( self.tcx.sess, - pat_span, + MultiSpan::from_spans(subpat_spans.clone()), E0023, "this pattern has {} field{}, but the corresponding {} has {} field{}", subpats.len(), @@ -1003,10 +1018,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { fields_ending, ); err.span_label( - pat_span, - format!("expected {} field{}, found {}", fields.len(), fields_ending, subpats.len(),), - ) - .span_label(res_span, format!("{} defined here", res.descr())); + last_subpat_span, + &format!("expected {} field{}, found {}", fields.len(), fields_ending, subpats.len()), + ); + if self.tcx.sess.source_map().is_multiline(qpath.span().between(last_subpat_span)) { + err.span_label(qpath.span(), ""); + } + if self.tcx.sess.source_map().is_multiline(def_ident_span.between(last_field_def_span)) { + err.span_label(def_ident_span, format!("{} defined here", res.descr())); + } + for span in &field_def_spans[..field_def_spans.len() - 1] { + err.span_label(*span, ""); + } + err.span_label( + last_field_def_span, + &format!("{} has {} field{}", res.descr(), fields.len(), fields_ending), + ); // Identify the case `Some(x, y)` where the expected type is e.g. `Option<(T, U)>`. // More generally, the expected type wants a tuple variant with one field of an diff --git a/library/alloc/src/collections/mod.rs b/library/alloc/src/collections/mod.rs index 0d442011921ba..4e31df8b4b8c2 100644 --- a/library/alloc/src/collections/mod.rs +++ b/library/alloc/src/collections/mod.rs @@ -117,12 +117,12 @@ impl From for TryReserveError { } } -#[unstable(feature = "try_reserve", reason = "new API", issue = "48043")] -impl From for TryReserveError { +#[unstable(feature = "try_reserve_kind", reason = "new API", issue = "48043")] +impl From for TryReserveErrorKind { /// Always evaluates to [`TryReserveErrorKind::CapacityOverflow`]. #[inline] fn from(_: LayoutError) -> Self { - TryReserveErrorKind::CapacityOverflow.into() + TryReserveErrorKind::CapacityOverflow } } diff --git a/library/core/primitive_docs/box_into_raw.md b/library/core/primitive_docs/box_into_raw.md new file mode 100644 index 0000000000000..9dd0344c7c7b1 --- /dev/null +++ b/library/core/primitive_docs/box_into_raw.md @@ -0,0 +1 @@ +../std/boxed/struct.Box.html#method.into_raw diff --git a/library/core/primitive_docs/fs_file.md b/library/core/primitive_docs/fs_file.md new file mode 100644 index 0000000000000..4023e340a5182 --- /dev/null +++ b/library/core/primitive_docs/fs_file.md @@ -0,0 +1 @@ +../std/fs/struct.File.html diff --git a/library/core/primitive_docs/io_bufread.md b/library/core/primitive_docs/io_bufread.md new file mode 100644 index 0000000000000..7beda2cd39085 --- /dev/null +++ b/library/core/primitive_docs/io_bufread.md @@ -0,0 +1 @@ +../std/io/trait.BufRead.html diff --git a/library/core/primitive_docs/io_read.md b/library/core/primitive_docs/io_read.md new file mode 100644 index 0000000000000..b7ecf5e273cea --- /dev/null +++ b/library/core/primitive_docs/io_read.md @@ -0,0 +1 @@ +../std/io/trait.Read.html diff --git a/library/core/primitive_docs/io_seek.md b/library/core/primitive_docs/io_seek.md new file mode 100644 index 0000000000000..db0274d291c6f --- /dev/null +++ b/library/core/primitive_docs/io_seek.md @@ -0,0 +1 @@ +../std/io/trait.Seek.html diff --git a/library/core/primitive_docs/io_write.md b/library/core/primitive_docs/io_write.md new file mode 100644 index 0000000000000..92a3b88a79c9c --- /dev/null +++ b/library/core/primitive_docs/io_write.md @@ -0,0 +1 @@ +../std/io/trait.Write.html diff --git a/library/core/primitive_docs/net_tosocketaddrs.md b/library/core/primitive_docs/net_tosocketaddrs.md new file mode 100644 index 0000000000000..4daa10ddbe2b2 --- /dev/null +++ b/library/core/primitive_docs/net_tosocketaddrs.md @@ -0,0 +1 @@ +../std/net/trait.ToSocketAddrs.html diff --git a/library/core/primitive_docs/process_exit.md b/library/core/primitive_docs/process_exit.md new file mode 100644 index 0000000000000..cae34d12d5249 --- /dev/null +++ b/library/core/primitive_docs/process_exit.md @@ -0,0 +1 @@ +../std/process/fn.exit.html diff --git a/library/core/primitive_docs/string_string.md b/library/core/primitive_docs/string_string.md new file mode 100644 index 0000000000000..303dc07b1855d --- /dev/null +++ b/library/core/primitive_docs/string_string.md @@ -0,0 +1 @@ +../std/string/struct.String.html diff --git a/library/core/src/char/methods.rs b/library/core/src/char/methods.rs index 0dadbdd1bd054..403b559984e14 100644 --- a/library/core/src/char/methods.rs +++ b/library/core/src/char/methods.rs @@ -24,7 +24,7 @@ impl char { /// decoding error. /// /// It can occur, for example, when giving ill-formed UTF-8 bytes to - /// [`String::from_utf8_lossy`](string/struct.String.html#method.from_utf8_lossy). + /// [`String::from_utf8_lossy`](../std/string/struct.String.html#method.from_utf8_lossy). #[stable(feature = "assoc_char_consts", since = "1.52.0")] pub const REPLACEMENT_CHARACTER: char = '\u{FFFD}'; diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index 1a6d1aed2fd1e..8271039da3a14 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -132,6 +132,7 @@ #![feature(decl_macro)] #![feature(doc_cfg)] #![feature(doc_notable_trait)] +#![cfg_attr(not(bootstrap), feature(doc_primitive))] #![feature(exhaustive_patterns)] #![feature(extern_types)] #![feature(fundamental)] @@ -357,3 +358,5 @@ pub mod arch { /* compiler built-in */ } } + +include!("primitive_docs.rs"); diff --git a/library/core/src/primitive_docs.rs b/library/core/src/primitive_docs.rs new file mode 100644 index 0000000000000..edd19ccdeb2ef --- /dev/null +++ b/library/core/src/primitive_docs.rs @@ -0,0 +1,1294 @@ +// `library/{std,core}/src/primitive_docs.rs` should have the same contents. +// These are different files so that relative links work properly without +// having to have `CARGO_PKG_NAME` set, but conceptually they should always be the same. +#[doc(primitive = "bool")] +#[doc(alias = "true")] +#[doc(alias = "false")] +/// The boolean type. +/// +/// The `bool` represents a value, which could only be either `true` or `false`. If you cast +/// a `bool` into an integer, `true` will be 1 and `false` will be 0. +/// +/// # Basic usage +/// +/// `bool` implements various traits, such as [`BitAnd`], [`BitOr`], [`Not`], etc., +/// which allow us to perform boolean operations using `&`, `|` and `!`. +/// +/// `if` requires a `bool` value as its conditional. [`assert!`], which is an +/// important macro in testing, checks whether an expression is `true` and panics +/// if it isn't. +/// +/// ``` +/// let bool_val = true & false | false; +/// assert!(!bool_val); +/// ``` +/// +/// [`BitAnd`]: ops::BitAnd +/// [`BitOr`]: ops::BitOr +/// [`Not`]: ops::Not +/// +/// # Examples +/// +/// A trivial example of the usage of `bool`: +/// +/// ``` +/// let praise_the_borrow_checker = true; +/// +/// // using the `if` conditional +/// if praise_the_borrow_checker { +/// println!("oh, yeah!"); +/// } else { +/// println!("what?!!"); +/// } +/// +/// // ... or, a match pattern +/// match praise_the_borrow_checker { +/// true => println!("keep praising!"), +/// false => println!("you should praise!"), +/// } +/// ``` +/// +/// Also, since `bool` implements the [`Copy`] trait, we don't +/// have to worry about the move semantics (just like the integer and float primitives). +/// +/// Now an example of `bool` cast to integer type: +/// +/// ``` +/// assert_eq!(true as i32, 1); +/// assert_eq!(false as i32, 0); +/// ``` +#[stable(feature = "rust1", since = "1.0.0")] +mod prim_bool {} + +#[doc(primitive = "never")] +#[doc(alias = "!")] +// +/// The `!` type, also called "never". +/// +/// `!` represents the type of computations which never resolve to any value at all. For example, +/// the [`exit`] function `fn exit(code: i32) -> !` exits the process without ever returning, and +/// so returns `!`. +/// +/// `break`, `continue` and `return` expressions also have type `!`. For example we are allowed to +/// write: +/// +/// ``` +/// #![feature(never_type)] +/// # fn foo() -> u32 { +/// let x: ! = { +/// return 123 +/// }; +/// # } +/// ``` +/// +/// Although the `let` is pointless here, it illustrates the meaning of `!`. Since `x` is never +/// assigned a value (because `return` returns from the entire function), `x` can be given type +/// `!`. We could also replace `return 123` with a `panic!` or a never-ending `loop` and this code +/// would still be valid. +/// +/// A more realistic usage of `!` is in this code: +/// +/// ``` +/// # fn get_a_number() -> Option { None } +/// # loop { +/// let num: u32 = match get_a_number() { +/// Some(num) => num, +/// None => break, +/// }; +/// # } +/// ``` +/// +/// Both match arms must produce values of type [`u32`], but since `break` never produces a value +/// at all we know it can never produce a value which isn't a [`u32`]. This illustrates another +/// behaviour of the `!` type - expressions with type `!` will coerce into any other type. +/// +/// [`u32`]: prim@u32 +#[doc = concat!("[`exit`]: ", include_str!("../primitive_docs/process_exit.md"))] +/// +/// # `!` and generics +/// +/// ## Infallible errors +/// +/// The main place you'll see `!` used explicitly is in generic code. Consider the [`FromStr`] +/// trait: +/// +/// ``` +/// trait FromStr: Sized { +/// type Err; +/// fn from_str(s: &str) -> Result; +/// } +/// ``` +/// +/// When implementing this trait for [`String`] we need to pick a type for [`Err`]. And since +/// converting a string into a string will never result in an error, the appropriate type is `!`. +/// (Currently the type actually used is an enum with no variants, though this is only because `!` +/// was added to Rust at a later date and it may change in the future.) With an [`Err`] type of +/// `!`, if we have to call [`String::from_str`] for some reason the result will be a +/// [`Result`] which we can unpack like this: +/// +/// ``` +/// #![feature(exhaustive_patterns)] +/// use std::str::FromStr; +/// let Ok(s) = String::from_str("hello"); +/// ``` +/// +/// Since the [`Err`] variant contains a `!`, it can never occur. If the `exhaustive_patterns` +/// feature is present this means we can exhaustively match on [`Result`] by just taking the +/// [`Ok`] variant. This illustrates another behaviour of `!` - it can be used to "delete" certain +/// enum variants from generic types like `Result`. +/// +/// ## Infinite loops +/// +/// While [`Result`] is very useful for removing errors, `!` can also be used to remove +/// successes as well. If we think of [`Result`] as "if this function returns, it has not +/// errored," we get a very intuitive idea of [`Result`] as well: if the function returns, it +/// *has* errored. +/// +/// For example, consider the case of a simple web server, which can be simplified to: +/// +/// ```ignore (hypothetical-example) +/// loop { +/// let (client, request) = get_request().expect("disconnected"); +/// let response = request.process(); +/// response.send(client); +/// } +/// ``` +/// +/// Currently, this isn't ideal, because we simply panic whenever we fail to get a new connection. +/// Instead, we'd like to keep track of this error, like this: +/// +/// ```ignore (hypothetical-example) +/// loop { +/// match get_request() { +/// Err(err) => break err, +/// Ok((client, request)) => { +/// let response = request.process(); +/// response.send(client); +/// }, +/// } +/// } +/// ``` +/// +/// Now, when the server disconnects, we exit the loop with an error instead of panicking. While it +/// might be intuitive to simply return the error, we might want to wrap it in a [`Result`] +/// instead: +/// +/// ```ignore (hypothetical-example) +/// fn server_loop() -> Result { +/// loop { +/// let (client, request) = get_request()?; +/// let response = request.process(); +/// response.send(client); +/// } +/// } +/// ``` +/// +/// Now, we can use `?` instead of `match`, and the return type makes a lot more sense: if the loop +/// ever stops, it means that an error occurred. We don't even have to wrap the loop in an `Ok` +/// because `!` coerces to `Result` automatically. +/// +/// [`String::from_str`]: str::FromStr::from_str +#[doc = concat!("[`String`]: ", include_str!("../primitive_docs/string_string.md"))] +/// [`FromStr`]: str::FromStr +/// +/// # `!` and traits +/// +/// When writing your own traits, `!` should have an `impl` whenever there is an obvious `impl` +/// which doesn't `panic!`. The reason is that functions returning an `impl Trait` where `!` +/// does not have an `impl` of `Trait` cannot diverge as their only possible code path. In other +/// words, they can't return `!` from every code path. As an example, this code doesn't compile: +/// +/// ```compile_fail +/// use std::ops::Add; +/// +/// fn foo() -> impl Add { +/// unimplemented!() +/// } +/// ``` +/// +/// But this code does: +/// +/// ``` +/// use std::ops::Add; +/// +/// fn foo() -> impl Add { +/// if true { +/// unimplemented!() +/// } else { +/// 0 +/// } +/// } +/// ``` +/// +/// The reason is that, in the first example, there are many possible types that `!` could coerce +/// to, because many types implement `Add`. However, in the second example, +/// the `else` branch returns a `0`, which the compiler infers from the return type to be of type +/// `u32`. Since `u32` is a concrete type, `!` can and will be coerced to it. See issue [#36375] +/// for more information on this quirk of `!`. +/// +/// [#36375]: https://github.com/rust-lang/rust/issues/36375 +/// +/// As it turns out, though, most traits can have an `impl` for `!`. Take [`Debug`] +/// for example: +/// +/// ``` +/// #![feature(never_type)] +/// # use std::fmt; +/// # trait Debug { +/// # fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result; +/// # } +/// impl Debug for ! { +/// fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { +/// *self +/// } +/// } +/// ``` +/// +/// Once again we're using `!`'s ability to coerce into any other type, in this case +/// [`fmt::Result`]. Since this method takes a `&!` as an argument we know that it can never be +/// called (because there is no value of type `!` for it to be called with). Writing `*self` +/// essentially tells the compiler "We know that this code can never be run, so just treat the +/// entire function body as having type [`fmt::Result`]". This pattern can be used a lot when +/// implementing traits for `!`. Generally, any trait which only has methods which take a `self` +/// parameter should have such an impl. +/// +/// On the other hand, one trait which would not be appropriate to implement is [`Default`]: +/// +/// ``` +/// trait Default { +/// fn default() -> Self; +/// } +/// ``` +/// +/// Since `!` has no values, it has no default value either. It's true that we could write an +/// `impl` for this which simply panics, but the same is true for any type (we could `impl +/// Default` for (eg.) [`File`] by just making [`default()`] panic.) +/// +#[doc = concat!("[`File`]: ", include_str!("../primitive_docs/fs_file.md"))] +/// [`Debug`]: fmt::Debug +/// [`default()`]: Default::default +/// +#[unstable(feature = "never_type", issue = "35121")] +mod prim_never {} + +#[doc(primitive = "char")] +/// A character type. +/// +/// The `char` type represents a single character. More specifically, since +/// 'character' isn't a well-defined concept in Unicode, `char` is a '[Unicode +/// scalar value]', which is similar to, but not the same as, a '[Unicode code +/// point]'. +/// +/// [Unicode scalar value]: https://www.unicode.org/glossary/#unicode_scalar_value +/// [Unicode code point]: https://www.unicode.org/glossary/#code_point +/// +/// This documentation describes a number of methods and trait implementations on the +/// `char` type. For technical reasons, there is additional, separate +/// documentation in [the `std::char` module](char/index.html) as well. +/// +/// # Representation +/// +/// `char` is always four bytes in size. This is a different representation than +/// a given character would have as part of a [`String`]. For example: +/// +/// ``` +/// let v = vec!['h', 'e', 'l', 'l', 'o']; +/// +/// // five elements times four bytes for each element +/// assert_eq!(20, v.len() * std::mem::size_of::()); +/// +/// let s = String::from("hello"); +/// +/// // five elements times one byte per element +/// assert_eq!(5, s.len() * std::mem::size_of::()); +/// ``` +/// +#[doc = concat!("[`String`]: ", include_str!("../primitive_docs/string_string.md"))] +/// +/// As always, remember that a human intuition for 'character' might not map to +/// Unicode's definitions. For example, despite looking similar, the 'é' +/// character is one Unicode code point while 'é' is two Unicode code points: +/// +/// ``` +/// let mut chars = "é".chars(); +/// // U+00e9: 'latin small letter e with acute' +/// assert_eq!(Some('\u{00e9}'), chars.next()); +/// assert_eq!(None, chars.next()); +/// +/// let mut chars = "é".chars(); +/// // U+0065: 'latin small letter e' +/// assert_eq!(Some('\u{0065}'), chars.next()); +/// // U+0301: 'combining acute accent' +/// assert_eq!(Some('\u{0301}'), chars.next()); +/// assert_eq!(None, chars.next()); +/// ``` +/// +/// This means that the contents of the first string above _will_ fit into a +/// `char` while the contents of the second string _will not_. Trying to create +/// a `char` literal with the contents of the second string gives an error: +/// +/// ```text +/// error: character literal may only contain one codepoint: 'é' +/// let c = 'é'; +/// ^^^ +/// ``` +/// +/// Another implication of the 4-byte fixed size of a `char` is that +/// per-`char` processing can end up using a lot more memory: +/// +/// ``` +/// let s = String::from("love: ❤️"); +/// let v: Vec = s.chars().collect(); +/// +/// assert_eq!(12, std::mem::size_of_val(&s[..])); +/// assert_eq!(32, std::mem::size_of_val(&v[..])); +/// ``` +#[stable(feature = "rust1", since = "1.0.0")] +mod prim_char {} + +#[doc(primitive = "unit")] +#[doc(alias = "(")] +#[doc(alias = ")")] +#[doc(alias = "()")] +// +/// The `()` type, also called "unit". +/// +/// The `()` type has exactly one value `()`, and is used when there +/// is no other meaningful value that could be returned. `()` is most +/// commonly seen implicitly: functions without a `-> ...` implicitly +/// have return type `()`, that is, these are equivalent: +/// +/// ```rust +/// fn long() -> () {} +/// +/// fn short() {} +/// ``` +/// +/// The semicolon `;` can be used to discard the result of an +/// expression at the end of a block, making the expression (and thus +/// the block) evaluate to `()`. For example, +/// +/// ```rust +/// fn returns_i64() -> i64 { +/// 1i64 +/// } +/// fn returns_unit() { +/// 1i64; +/// } +/// +/// let is_i64 = { +/// returns_i64() +/// }; +/// let is_unit = { +/// returns_i64(); +/// }; +/// ``` +/// +#[stable(feature = "rust1", since = "1.0.0")] +mod prim_unit {} + +#[doc(alias = "ptr")] +#[doc(primitive = "pointer")] +// +/// Raw, unsafe pointers, `*const T`, and `*mut T`. +/// +/// *[See also the `std::ptr` module](ptr).* +/// +/// Working with raw pointers in Rust is uncommon, typically limited to a few patterns. +/// Raw pointers can be unaligned or [`null`]. However, when a raw pointer is +/// dereferenced (using the `*` operator), it must be non-null and aligned. +/// +/// Storing through a raw pointer using `*ptr = data` calls `drop` on the old value, so +/// [`write`] must be used if the type has drop glue and memory is not already +/// initialized - otherwise `drop` would be called on the uninitialized memory. +/// +/// Use the [`null`] and [`null_mut`] functions to create null pointers, and the +/// [`is_null`] method of the `*const T` and `*mut T` types to check for null. +/// The `*const T` and `*mut T` types also define the [`offset`] method, for +/// pointer math. +/// +/// # Common ways to create raw pointers +/// +/// ## 1. Coerce a reference (`&T`) or mutable reference (`&mut T`). +/// +/// ``` +/// let my_num: i32 = 10; +/// let my_num_ptr: *const i32 = &my_num; +/// let mut my_speed: i32 = 88; +/// let my_speed_ptr: *mut i32 = &mut my_speed; +/// ``` +/// +/// To get a pointer to a boxed value, dereference the box: +/// +/// ``` +/// let my_num: Box = Box::new(10); +/// let my_num_ptr: *const i32 = &*my_num; +/// let mut my_speed: Box = Box::new(88); +/// let my_speed_ptr: *mut i32 = &mut *my_speed; +/// ``` +/// +/// This does not take ownership of the original allocation +/// and requires no resource management later, +/// but you must not use the pointer after its lifetime. +/// +/// ## 2. Consume a box (`Box`). +/// +/// The [`into_raw`] function consumes a box and returns +/// the raw pointer. It doesn't destroy `T` or deallocate any memory. +/// +/// ``` +/// let my_speed: Box = Box::new(88); +/// let my_speed: *mut i32 = Box::into_raw(my_speed); +/// +/// // By taking ownership of the original `Box` though +/// // we are obligated to put it together later to be destroyed. +/// unsafe { +/// drop(Box::from_raw(my_speed)); +/// } +/// ``` +/// +/// Note that here the call to [`drop`] is for clarity - it indicates +/// that we are done with the given value and it should be destroyed. +/// +/// ## 3. Create it using `ptr::addr_of!` +/// +/// Instead of coercing a reference to a raw pointer, you can use the macros +/// [`ptr::addr_of!`] (for `*const T`) and [`ptr::addr_of_mut!`] (for `*mut T`). +/// These macros allow you to create raw pointers to fields to which you cannot +/// create a reference (without causing undefined behaviour), such as an +/// unaligned field. This might be necessary if packed structs or uninitialized +/// memory is involved. +/// +/// ``` +/// #[derive(Debug, Default, Copy, Clone)] +/// #[repr(C, packed)] +/// struct S { +/// aligned: u8, +/// unaligned: u32, +/// } +/// let s = S::default(); +/// let p = std::ptr::addr_of!(s.unaligned); // not allowed with coercion +/// ``` +/// +/// ## 4. Get it from C. +/// +/// ``` +/// # #![feature(rustc_private)] +/// extern crate libc; +/// +/// use std::mem; +/// +/// unsafe { +/// let my_num: *mut i32 = libc::malloc(mem::size_of::()) as *mut i32; +/// if my_num.is_null() { +/// panic!("failed to allocate memory"); +/// } +/// libc::free(my_num as *mut libc::c_void); +/// } +/// ``` +/// +/// Usually you wouldn't literally use `malloc` and `free` from Rust, +/// but C APIs hand out a lot of pointers generally, so are a common source +/// of raw pointers in Rust. +/// +/// [`null`]: ptr::null +/// [`null_mut`]: ptr::null_mut +/// [`is_null`]: pointer::is_null +/// [`offset`]: pointer::offset +#[doc = concat!("[`into_raw`]: ", include_str!("../primitive_docs/box_into_raw.md"))] +/// [`drop`]: mem::drop +/// [`write`]: ptr::write +#[stable(feature = "rust1", since = "1.0.0")] +mod prim_pointer {} + +#[doc(alias = "[]")] +#[doc(alias = "[T;N]")] // unfortunately, rustdoc doesn't have fuzzy search for aliases +#[doc(alias = "[T; N]")] +#[doc(primitive = "array")] +/// A fixed-size array, denoted `[T; N]`, for the element type, `T`, and the +/// non-negative compile-time constant size, `N`. +/// +/// There are two syntactic forms for creating an array: +/// +/// * A list with each element, i.e., `[x, y, z]`. +/// * A repeat expression `[x; N]`, which produces an array with `N` copies of `x`. +/// The type of `x` must be [`Copy`]. +/// +/// Note that `[expr; 0]` is allowed, and produces an empty array. +/// This will still evaluate `expr`, however, and immediately drop the resulting value, so +/// be mindful of side effects. +/// +/// Arrays of *any* size implement the following traits if the element type allows it: +/// +/// - [`Copy`] +/// - [`Clone`] +/// - [`Debug`] +/// - [`IntoIterator`] (implemented for `[T; N]`, `&[T; N]` and `&mut [T; N]`) +/// - [`PartialEq`], [`PartialOrd`], [`Eq`], [`Ord`] +/// - [`Hash`] +/// - [`AsRef`], [`AsMut`] +/// - [`Borrow`], [`BorrowMut`] +/// +/// Arrays of sizes from 0 to 32 (inclusive) implement the [`Default`] trait +/// if the element type allows it. As a stopgap, trait implementations are +/// statically generated up to size 32. +/// +/// Arrays coerce to [slices (`[T]`)][slice], so a slice method may be called on +/// an array. Indeed, this provides most of the API for working with arrays. +/// Slices have a dynamic size and do not coerce to arrays. +/// +/// You can move elements out of an array with a [slice pattern]. If you want +/// one element, see [`mem::replace`]. +/// +/// # Examples +/// +/// ``` +/// let mut array: [i32; 3] = [0; 3]; +/// +/// array[1] = 1; +/// array[2] = 2; +/// +/// assert_eq!([1, 2], &array[1..]); +/// +/// // This loop prints: 0 1 2 +/// for x in array { +/// print!("{} ", x); +/// } +/// ``` +/// +/// You can also iterate over reference to the array's elements: +/// +/// ``` +/// let array: [i32; 3] = [0; 3]; +/// +/// for x in &array { } +/// ``` +/// +/// You can use a [slice pattern] to move elements out of an array: +/// +/// ``` +/// fn move_away(_: String) { /* Do interesting things. */ } +/// +/// let [john, roa] = ["John".to_string(), "Roa".to_string()]; +/// move_away(john); +/// move_away(roa); +/// ``` +/// +/// # Editions +/// +/// Prior to Rust 1.53, arrays did not implement `IntoIterator` by value, so the method call +/// `array.into_iter()` auto-referenced into a slice iterator. Right now, the old behavior +/// is preserved in the 2015 and 2018 editions of Rust for compatibility, ignoring +/// `IntoIterator` by value. In the future, the behavior on the 2015 and 2018 edition +/// might be made consistent to the behavior of later editions. +/// +/// ```rust,edition2018 +/// # #![allow(array_into_iter)] // override our `deny(warnings)` +/// let array: [i32; 3] = [0; 3]; +/// +/// // This creates a slice iterator, producing references to each value. +/// for item in array.into_iter().enumerate() { +/// let (i, x): (usize, &i32) = item; +/// println!("array[{}] = {}", i, x); +/// } +/// +/// // The `array_into_iter` lint suggests this change for future compatibility: +/// for item in array.iter().enumerate() { +/// let (i, x): (usize, &i32) = item; +/// println!("array[{}] = {}", i, x); +/// } +/// +/// // You can explicitly iterate an array by value using +/// // `IntoIterator::into_iter` or `std::array::IntoIter::new`: +/// for item in IntoIterator::into_iter(array).enumerate() { +/// let (i, x): (usize, i32) = item; +/// println!("array[{}] = {}", i, x); +/// } +/// ``` +/// +/// Starting in the 2021 edition, `array.into_iter()` will use `IntoIterator` normally to iterate +/// by value, and `iter()` should be used to iterate by reference like previous editions. +/// +/// ```rust,edition2021,ignore +/// # // FIXME: ignored because 2021 testing is still unstable +/// let array: [i32; 3] = [0; 3]; +/// +/// // This iterates by reference: +/// for item in array.iter().enumerate() { +/// let (i, x): (usize, &i32) = item; +/// println!("array[{}] = {}", i, x); +/// } +/// +/// // This iterates by value: +/// for item in array.into_iter().enumerate() { +/// let (i, x): (usize, i32) = item; +/// println!("array[{}] = {}", i, x); +/// } +/// ``` +/// +/// Future language versions might start treating the `array.into_iter()` +/// syntax on editions 2015 and 2018 the same as on edition 2021. So code using +/// those older editions should still be written with this change in mind, to +/// prevent breakage in the future. The safest way to accomplish this is to +/// avoid the `into_iter` syntax on those editions. If an edition update is not +/// viable/desired, there are multiple alternatives: +/// * use `iter`, equivalent to the old behavior, creating references +/// * use [`array::IntoIter`], equivalent to the post-2021 behavior (Rust 1.51+) +/// * replace `for ... in array.into_iter() {` with `for ... in array {`, +/// equivalent to the post-2021 behavior (Rust 1.53+) +/// +/// ```rust,edition2018 +/// use std::array::IntoIter; +/// +/// let array: [i32; 3] = [0; 3]; +/// +/// // This iterates by reference: +/// for item in array.iter() { +/// let x: &i32 = item; +/// println!("{}", x); +/// } +/// +/// // This iterates by value: +/// for item in IntoIter::new(array) { +/// let x: i32 = item; +/// println!("{}", x); +/// } +/// +/// // This iterates by value: +/// for item in array { +/// let x: i32 = item; +/// println!("{}", x); +/// } +/// +/// // IntoIter can also start a chain. +/// // This iterates by value: +/// for item in IntoIter::new(array).enumerate() { +/// let (i, x): (usize, i32) = item; +/// println!("array[{}] = {}", i, x); +/// } +/// ``` +/// +/// [slice]: prim@slice +/// [`Debug`]: fmt::Debug +/// [`Hash`]: hash::Hash +/// [`Borrow`]: borrow::Borrow +/// [`BorrowMut`]: borrow::BorrowMut +/// [slice pattern]: ../reference/patterns.html#slice-patterns +#[stable(feature = "rust1", since = "1.0.0")] +mod prim_array {} + +#[doc(primitive = "slice")] +#[doc(alias = "[")] +#[doc(alias = "]")] +#[doc(alias = "[]")] +/// A dynamically-sized view into a contiguous sequence, `[T]`. Contiguous here +/// means that elements are laid out so that every element is the same +/// distance from its neighbors. +/// +/// *[See also the `std::slice` module](crate::slice).* +/// +/// Slices are a view into a block of memory represented as a pointer and a +/// length. +/// +/// ``` +/// // slicing a Vec +/// let vec = vec![1, 2, 3]; +/// let int_slice = &vec[..]; +/// // coercing an array to a slice +/// let str_slice: &[&str] = &["one", "two", "three"]; +/// ``` +/// +/// Slices are either mutable or shared. The shared slice type is `&[T]`, +/// while the mutable slice type is `&mut [T]`, where `T` represents the element +/// type. For example, you can mutate the block of memory that a mutable slice +/// points to: +/// +/// ``` +/// let mut x = [1, 2, 3]; +/// let x = &mut x[..]; // Take a full slice of `x`. +/// x[1] = 7; +/// assert_eq!(x, &[1, 7, 3]); +/// ``` +/// +/// As slices store the length of the sequence they refer to, they have twice +/// the size of pointers to [`Sized`](marker/trait.Sized.html) types. +/// Also see the reference on +/// [dynamically sized types](../reference/dynamically-sized-types.html). +/// +/// ``` +/// # use std::rc::Rc; +/// let pointer_size = std::mem::size_of::<&u8>(); +/// assert_eq!(2 * pointer_size, std::mem::size_of::<&[u8]>()); +/// assert_eq!(2 * pointer_size, std::mem::size_of::<*const [u8]>()); +/// assert_eq!(2 * pointer_size, std::mem::size_of::>()); +/// assert_eq!(2 * pointer_size, std::mem::size_of::>()); +/// ``` +#[stable(feature = "rust1", since = "1.0.0")] +mod prim_slice {} + +#[doc(primitive = "str")] +// +/// String slices. +/// +/// *[See also the `std::str` module](crate::str).* +/// +/// The `str` type, also called a 'string slice', is the most primitive string +/// type. It is usually seen in its borrowed form, `&str`. It is also the type +/// of string literals, `&'static str`. +/// +/// String slices are always valid UTF-8. +/// +/// # Examples +/// +/// String literals are string slices: +/// +/// ``` +/// let hello = "Hello, world!"; +/// +/// // with an explicit type annotation +/// let hello: &'static str = "Hello, world!"; +/// ``` +/// +/// They are `'static` because they're stored directly in the final binary, and +/// so will be valid for the `'static` duration. +/// +/// # Representation +/// +/// A `&str` is made up of two components: a pointer to some bytes, and a +/// length. You can look at these with the [`as_ptr`] and [`len`] methods: +/// +/// ``` +/// use std::slice; +/// use std::str; +/// +/// let story = "Once upon a time..."; +/// +/// let ptr = story.as_ptr(); +/// let len = story.len(); +/// +/// // story has nineteen bytes +/// assert_eq!(19, len); +/// +/// // We can re-build a str out of ptr and len. This is all unsafe because +/// // we are responsible for making sure the two components are valid: +/// let s = unsafe { +/// // First, we build a &[u8]... +/// let slice = slice::from_raw_parts(ptr, len); +/// +/// // ... and then convert that slice into a string slice +/// str::from_utf8(slice) +/// }; +/// +/// assert_eq!(s, Ok(story)); +/// ``` +/// +/// [`as_ptr`]: str::as_ptr +/// [`len`]: str::len +/// +/// Note: This example shows the internals of `&str`. `unsafe` should not be +/// used to get a string slice under normal circumstances. Use `as_str` +/// instead. +#[stable(feature = "rust1", since = "1.0.0")] +mod prim_str {} + +#[doc(primitive = "tuple")] +#[doc(alias = "(")] +#[doc(alias = ")")] +#[doc(alias = "()")] +// +/// A finite heterogeneous sequence, `(T, U, ..)`. +/// +/// Let's cover each of those in turn: +/// +/// Tuples are *finite*. In other words, a tuple has a length. Here's a tuple +/// of length `3`: +/// +/// ``` +/// ("hello", 5, 'c'); +/// ``` +/// +/// 'Length' is also sometimes called 'arity' here; each tuple of a different +/// length is a different, distinct type. +/// +/// Tuples are *heterogeneous*. This means that each element of the tuple can +/// have a different type. In that tuple above, it has the type: +/// +/// ``` +/// # let _: +/// (&'static str, i32, char) +/// # = ("hello", 5, 'c'); +/// ``` +/// +/// Tuples are a *sequence*. This means that they can be accessed by position; +/// this is called 'tuple indexing', and it looks like this: +/// +/// ```rust +/// let tuple = ("hello", 5, 'c'); +/// +/// assert_eq!(tuple.0, "hello"); +/// assert_eq!(tuple.1, 5); +/// assert_eq!(tuple.2, 'c'); +/// ``` +/// +/// The sequential nature of the tuple applies to its implementations of various +/// traits. For example, in `PartialOrd` and `Ord`, the elements are compared +/// sequentially until the first non-equal set is found. +/// +/// For more about tuples, see [the book](../book/ch03-02-data-types.html#the-tuple-type). +/// +/// # Trait implementations +/// +/// If every type inside a tuple implements one of the following traits, then a +/// tuple itself also implements it. +/// +/// * [`Clone`] +/// * [`Copy`] +/// * [`PartialEq`] +/// * [`Eq`] +/// * [`PartialOrd`] +/// * [`Ord`] +/// * [`Debug`] +/// * [`Default`] +/// * [`Hash`] +/// +/// [`Debug`]: fmt::Debug +/// [`Hash`]: hash::Hash +/// +/// Due to a temporary restriction in Rust's type system, these traits are only +/// implemented on tuples of arity 12 or less. In the future, this may change. +/// +/// # Examples +/// +/// Basic usage: +/// +/// ``` +/// let tuple = ("hello", 5, 'c'); +/// +/// assert_eq!(tuple.0, "hello"); +/// ``` +/// +/// Tuples are often used as a return type when you want to return more than +/// one value: +/// +/// ``` +/// fn calculate_point() -> (i32, i32) { +/// // Don't do a calculation, that's not the point of the example +/// (4, 5) +/// } +/// +/// let point = calculate_point(); +/// +/// assert_eq!(point.0, 4); +/// assert_eq!(point.1, 5); +/// +/// // Combining this with patterns can be nicer. +/// +/// let (x, y) = calculate_point(); +/// +/// assert_eq!(x, 4); +/// assert_eq!(y, 5); +/// ``` +/// +#[stable(feature = "rust1", since = "1.0.0")] +mod prim_tuple {} + +#[doc(primitive = "f32")] +/// A 32-bit floating point type (specifically, the "binary32" type defined in IEEE 754-2008). +/// +/// This type can represent a wide range of decimal numbers, like `3.5`, `27`, +/// `-113.75`, `0.0078125`, `34359738368`, `0`, `-1`. So unlike integer types +/// (such as `i32`), floating point types can represent non-integer numbers, +/// too. +/// +/// However, being able to represent this wide range of numbers comes at the +/// cost of precision: floats can only represent some of the real numbers and +/// calculation with floats round to a nearby representable number. For example, +/// `5.0` and `1.0` can be exactly represented as `f32`, but `1.0 / 5.0` results +/// in `0.20000000298023223876953125` since `0.2` cannot be exactly represented +/// as `f32`. Note, however, that printing floats with `println` and friends will +/// often discard insignificant digits: `println!("{}", 1.0f32 / 5.0f32)` will +/// print `0.2`. +/// +/// Additionally, `f32` can represent some special values: +/// +/// - −0.0: IEEE 754 floating point numbers have a bit that indicates their sign, so −0.0 is a +/// possible value. For comparison −0.0 = +0.0, but floating point operations can carry +/// the sign bit through arithmetic operations. This means −0.0 × +0.0 produces −0.0 and +/// a negative number rounded to a value smaller than a float can represent also produces −0.0. +/// - [∞](#associatedconstant.INFINITY) and +/// [−∞](#associatedconstant.NEG_INFINITY): these result from calculations +/// like `1.0 / 0.0`. +/// - [NaN (not a number)](#associatedconstant.NAN): this value results from +/// calculations like `(-1.0).sqrt()`. NaN has some potentially unexpected +/// behavior: it is unequal to any float, including itself! It is also neither +/// smaller nor greater than any float, making it impossible to sort. Lastly, +/// it is considered infectious as almost all calculations where one of the +/// operands is NaN will also result in NaN. +/// +/// For more information on floating point numbers, see [Wikipedia][wikipedia]. +/// +/// *[See also the `std::f32::consts` module](crate::f32::consts).* +/// +/// [wikipedia]: https://en.wikipedia.org/wiki/Single-precision_floating-point_format +#[stable(feature = "rust1", since = "1.0.0")] +mod prim_f32 {} + +#[doc(primitive = "f64")] +/// A 64-bit floating point type (specifically, the "binary64" type defined in IEEE 754-2008). +/// +/// This type is very similar to [`f32`], but has increased +/// precision by using twice as many bits. Please see [the documentation for +/// `f32`][`f32`] or [Wikipedia on double precision +/// values][wikipedia] for more information. +/// +/// *[See also the `std::f64::consts` module](crate::f64::consts).* +/// +/// [`f32`]: prim@f32 +/// [wikipedia]: https://en.wikipedia.org/wiki/Double-precision_floating-point_format +#[stable(feature = "rust1", since = "1.0.0")] +mod prim_f64 {} + +#[doc(primitive = "i8")] +// +/// The 8-bit signed integer type. +#[stable(feature = "rust1", since = "1.0.0")] +mod prim_i8 {} + +#[doc(primitive = "i16")] +// +/// The 16-bit signed integer type. +#[stable(feature = "rust1", since = "1.0.0")] +mod prim_i16 {} + +#[doc(primitive = "i32")] +// +/// The 32-bit signed integer type. +#[stable(feature = "rust1", since = "1.0.0")] +mod prim_i32 {} + +#[doc(primitive = "i64")] +// +/// The 64-bit signed integer type. +#[stable(feature = "rust1", since = "1.0.0")] +mod prim_i64 {} + +#[doc(primitive = "i128")] +// +/// The 128-bit signed integer type. +#[stable(feature = "i128", since = "1.26.0")] +mod prim_i128 {} + +#[doc(primitive = "u8")] +// +/// The 8-bit unsigned integer type. +#[stable(feature = "rust1", since = "1.0.0")] +mod prim_u8 {} + +#[doc(primitive = "u16")] +// +/// The 16-bit unsigned integer type. +#[stable(feature = "rust1", since = "1.0.0")] +mod prim_u16 {} + +#[doc(primitive = "u32")] +// +/// The 32-bit unsigned integer type. +#[stable(feature = "rust1", since = "1.0.0")] +mod prim_u32 {} + +#[doc(primitive = "u64")] +// +/// The 64-bit unsigned integer type. +#[stable(feature = "rust1", since = "1.0.0")] +mod prim_u64 {} + +#[doc(primitive = "u128")] +// +/// The 128-bit unsigned integer type. +#[stable(feature = "i128", since = "1.26.0")] +mod prim_u128 {} + +#[doc(primitive = "isize")] +// +/// The pointer-sized signed integer type. +/// +/// The size of this primitive is how many bytes it takes to reference any +/// location in memory. For example, on a 32 bit target, this is 4 bytes +/// and on a 64 bit target, this is 8 bytes. +#[stable(feature = "rust1", since = "1.0.0")] +mod prim_isize {} + +#[doc(primitive = "usize")] +// +/// The pointer-sized unsigned integer type. +/// +/// The size of this primitive is how many bytes it takes to reference any +/// location in memory. For example, on a 32 bit target, this is 4 bytes +/// and on a 64 bit target, this is 8 bytes. +#[stable(feature = "rust1", since = "1.0.0")] +mod prim_usize {} + +#[doc(primitive = "reference")] +#[doc(alias = "&")] +#[doc(alias = "&mut")] +// +/// References, both shared and mutable. +/// +/// A reference represents a borrow of some owned value. You can get one by using the `&` or `&mut` +/// operators on a value, or by using a `ref` or `ref mut` pattern. +/// +/// For those familiar with pointers, a reference is just a pointer that is assumed to be +/// aligned, not null, and pointing to memory containing a valid value of `T` - for example, +/// `&bool` can only point to an allocation containing the integer values `1` (`true`) or `0` +/// (`false`), but creating a `&bool` that points to an allocation containing +/// the value `3` causes undefined behaviour. +/// In fact, `Option<&T>` has the same memory representation as a +/// nullable but aligned pointer, and can be passed across FFI boundaries as such. +/// +/// In most cases, references can be used much like the original value. Field access, method +/// calling, and indexing work the same (save for mutability rules, of course). In addition, the +/// comparison operators transparently defer to the referent's implementation, allowing references +/// to be compared the same as owned values. +/// +/// References have a lifetime attached to them, which represents the scope for which the borrow is +/// valid. A lifetime is said to "outlive" another one if its representative scope is as long or +/// longer than the other. The `'static` lifetime is the longest lifetime, which represents the +/// total life of the program. For example, string literals have a `'static` lifetime because the +/// text data is embedded into the binary of the program, rather than in an allocation that needs +/// to be dynamically managed. +/// +/// `&mut T` references can be freely coerced into `&T` references with the same referent type, and +/// references with longer lifetimes can be freely coerced into references with shorter ones. +/// +/// Reference equality by address, instead of comparing the values pointed to, is accomplished via +/// implicit reference-pointer coercion and raw pointer equality via [`ptr::eq`], while +/// [`PartialEq`] compares values. +/// +/// ``` +/// use std::ptr; +/// +/// let five = 5; +/// let other_five = 5; +/// let five_ref = &five; +/// let same_five_ref = &five; +/// let other_five_ref = &other_five; +/// +/// assert!(five_ref == same_five_ref); +/// assert!(five_ref == other_five_ref); +/// +/// assert!(ptr::eq(five_ref, same_five_ref)); +/// assert!(!ptr::eq(five_ref, other_five_ref)); +/// ``` +/// +/// For more information on how to use references, see [the book's section on "References and +/// Borrowing"][book-refs]. +/// +/// [book-refs]: ../book/ch04-02-references-and-borrowing.html +/// +/// # Trait implementations +/// +/// The following traits are implemented for all `&T`, regardless of the type of its referent: +/// +/// * [`Copy`] +/// * [`Clone`] \(Note that this will not defer to `T`'s `Clone` implementation if it exists!) +/// * [`Deref`] +/// * [`Borrow`] +/// * [`Pointer`] +/// +/// [`Deref`]: ops::Deref +/// [`Borrow`]: borrow::Borrow +/// [`Pointer`]: fmt::Pointer +/// +/// `&mut T` references get all of the above except `Copy` and `Clone` (to prevent creating +/// multiple simultaneous mutable borrows), plus the following, regardless of the type of its +/// referent: +/// +/// * [`DerefMut`] +/// * [`BorrowMut`] +/// +/// [`DerefMut`]: ops::DerefMut +/// [`BorrowMut`]: borrow::BorrowMut +/// +/// The following traits are implemented on `&T` references if the underlying `T` also implements +/// that trait: +/// +/// * All the traits in [`std::fmt`] except [`Pointer`] and [`fmt::Write`] +/// * [`PartialOrd`] +/// * [`Ord`] +/// * [`PartialEq`] +/// * [`Eq`] +/// * [`AsRef`] +/// * [`Fn`] \(in addition, `&T` references get [`FnMut`] and [`FnOnce`] if `T: Fn`) +/// * [`Hash`] +/// * [`ToSocketAddrs`] +/// +/// [`std::fmt`]: fmt +/// ['Pointer`]: fmt::Pointer +/// [`Hash`]: hash::Hash +#[doc = concat!("[`ToSocketAddrs`]: ", include_str!("../primitive_docs/net_tosocketaddrs.md"))] +/// +/// `&mut T` references get all of the above except `ToSocketAddrs`, plus the following, if `T` +/// implements that trait: +/// +/// * [`AsMut`] +/// * [`FnMut`] \(in addition, `&mut T` references get [`FnOnce`] if `T: FnMut`) +/// * [`fmt::Write`] +/// * [`Iterator`] +/// * [`DoubleEndedIterator`] +/// * [`ExactSizeIterator`] +/// * [`FusedIterator`] +/// * [`TrustedLen`] +/// * [`Send`] \(note that `&T` references only get `Send` if `T: Sync`) +/// * [`io::Write`] +/// * [`Read`] +/// * [`Seek`] +/// * [`BufRead`] +/// +/// [`FusedIterator`]: iter::FusedIterator +/// [`TrustedLen`]: iter::TrustedLen +#[doc = concat!("[`Seek`]: ", include_str!("../primitive_docs/io_seek.md"))] +#[doc = concat!("[`BufRead`]: ", include_str!("../primitive_docs/io_bufread.md"))] +#[doc = concat!("[`Read`]: ", include_str!("../primitive_docs/io_read.md"))] +#[doc = concat!("[`io::Write`]: ", include_str!("../primitive_docs/io_write.md"))] +/// +/// Note that due to method call deref coercion, simply calling a trait method will act like they +/// work on references as well as they do on owned values! The implementations described here are +/// meant for generic contexts, where the final type `T` is a type parameter or otherwise not +/// locally known. +#[stable(feature = "rust1", since = "1.0.0")] +mod prim_ref {} + +#[doc(primitive = "fn")] +// +/// Function pointers, like `fn(usize) -> bool`. +/// +/// *See also the traits [`Fn`], [`FnMut`], and [`FnOnce`].* +/// +/// [`Fn`]: ops::Fn +/// [`FnMut`]: ops::FnMut +/// [`FnOnce`]: ops::FnOnce +/// +/// Function pointers are pointers that point to *code*, not data. They can be called +/// just like functions. Like references, function pointers are, among other things, assumed to +/// not be null, so if you want to pass a function pointer over FFI and be able to accommodate null +/// pointers, make your type `Option` with your required signature. +/// +/// ### Safety +/// +/// Plain function pointers are obtained by casting either plain functions, or closures that don't +/// capture an environment: +/// +/// ``` +/// fn add_one(x: usize) -> usize { +/// x + 1 +/// } +/// +/// let ptr: fn(usize) -> usize = add_one; +/// assert_eq!(ptr(5), 6); +/// +/// let clos: fn(usize) -> usize = |x| x + 5; +/// assert_eq!(clos(5), 10); +/// ``` +/// +/// In addition to varying based on their signature, function pointers come in two flavors: safe +/// and unsafe. Plain `fn()` function pointers can only point to safe functions, +/// while `unsafe fn()` function pointers can point to safe or unsafe functions. +/// +/// ``` +/// fn add_one(x: usize) -> usize { +/// x + 1 +/// } +/// +/// unsafe fn add_one_unsafely(x: usize) -> usize { +/// x + 1 +/// } +/// +/// let safe_ptr: fn(usize) -> usize = add_one; +/// +/// //ERROR: mismatched types: expected normal fn, found unsafe fn +/// //let bad_ptr: fn(usize) -> usize = add_one_unsafely; +/// +/// let unsafe_ptr: unsafe fn(usize) -> usize = add_one_unsafely; +/// let really_safe_ptr: unsafe fn(usize) -> usize = add_one; +/// ``` +/// +/// ### ABI +/// +/// On top of that, function pointers can vary based on what ABI they use. This +/// is achieved by adding the `extern` keyword before the type, followed by the +/// ABI in question. The default ABI is "Rust", i.e., `fn()` is the exact same +/// type as `extern "Rust" fn()`. A pointer to a function with C ABI would have +/// type `extern "C" fn()`. +/// +/// `extern "ABI" { ... }` blocks declare functions with ABI "ABI". The default +/// here is "C", i.e., functions declared in an `extern {...}` block have "C" +/// ABI. +/// +/// For more information and a list of supported ABIs, see [the nomicon's +/// section on foreign calling conventions][nomicon-abi]. +/// +/// [nomicon-abi]: ../nomicon/ffi.html#foreign-calling-conventions +/// +/// ### Variadic functions +/// +/// Extern function declarations with the "C" or "cdecl" ABIs can also be *variadic*, allowing them +/// to be called with a variable number of arguments. Normal Rust functions, even those with an +/// `extern "ABI"`, cannot be variadic. For more information, see [the nomicon's section on +/// variadic functions][nomicon-variadic]. +/// +/// [nomicon-variadic]: ../nomicon/ffi.html#variadic-functions +/// +/// ### Creating function pointers +/// +/// When `bar` is the name of a function, then the expression `bar` is *not* a +/// function pointer. Rather, it denotes a value of an unnameable type that +/// uniquely identifies the function `bar`. The value is zero-sized because the +/// type already identifies the function. This has the advantage that "calling" +/// the value (it implements the `Fn*` traits) does not require dynamic +/// dispatch. +/// +/// This zero-sized type *coerces* to a regular function pointer. For example: +/// +/// ```rust +/// use std::mem; +/// +/// fn bar(x: i32) {} +/// +/// let not_bar_ptr = bar; // `not_bar_ptr` is zero-sized, uniquely identifying `bar` +/// assert_eq!(mem::size_of_val(¬_bar_ptr), 0); +/// +/// let bar_ptr: fn(i32) = not_bar_ptr; // force coercion to function pointer +/// assert_eq!(mem::size_of_val(&bar_ptr), mem::size_of::()); +/// +/// let footgun = &bar; // this is a shared reference to the zero-sized type identifying `bar` +/// ``` +/// +/// The last line shows that `&bar` is not a function pointer either. Rather, it +/// is a reference to the function-specific ZST. `&bar` is basically never what you +/// want when `bar` is a function. +/// +/// ### Traits +/// +/// Function pointers implement the following traits: +/// +/// * [`Clone`] +/// * [`PartialEq`] +/// * [`Eq`] +/// * [`PartialOrd`] +/// * [`Ord`] +/// * [`Hash`] +/// * [`Pointer`] +/// * [`Debug`] +/// +/// [`Hash`]: hash::Hash +/// [`Pointer`]: fmt::Pointer +/// +/// Due to a temporary restriction in Rust's type system, these traits are only implemented on +/// functions that take 12 arguments or less, with the `"Rust"` and `"C"` ABIs. In the future, this +/// may change. +/// +/// In addition, function pointers of *any* signature, ABI, or safety are [`Copy`], and all *safe* +/// function pointers implement [`Fn`], [`FnMut`], and [`FnOnce`]. This works because these traits +/// are specially known to the compiler. +#[stable(feature = "rust1", since = "1.0.0")] +mod prim_fn {} diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index 361a9b03aebc7..1d79f6ecd2761 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -2258,9 +2258,9 @@ impl [T] { /// assert!(match r { Ok(1..=4) => true, _ => false, }); /// ``` // Lint rustdoc::broken_intra_doc_links is allowed as `slice::sort_by_key` is - // in crate `alloc`, and as such doesn't exists yet when building `core`. - // links to downstream crate: #74481. Since primitives are only documented in - // libstd (#73423), this never leads to broken links in practice. + // in crate `alloc`, and as such doesn't exists yet when building `core`: #74481. + // This breaks links when slice is displayed in core, but changing it to use relative links + // would break when the item is re-exported. So allow the core links to be broken for now. #[allow(rustdoc::broken_intra_doc_links)] #[stable(feature = "slice_binary_search_by_key", since = "1.10.0")] #[inline] diff --git a/library/std/primitive_docs/box_into_raw.md b/library/std/primitive_docs/box_into_raw.md new file mode 100644 index 0000000000000..307b9c85bd67e --- /dev/null +++ b/library/std/primitive_docs/box_into_raw.md @@ -0,0 +1 @@ +Box::into_raw diff --git a/library/std/primitive_docs/fs_file.md b/library/std/primitive_docs/fs_file.md new file mode 100644 index 0000000000000..13e4540835e61 --- /dev/null +++ b/library/std/primitive_docs/fs_file.md @@ -0,0 +1 @@ +fs::File diff --git a/library/std/primitive_docs/io_bufread.md b/library/std/primitive_docs/io_bufread.md new file mode 100644 index 0000000000000..bb688e3a5cc87 --- /dev/null +++ b/library/std/primitive_docs/io_bufread.md @@ -0,0 +1 @@ +io::BufRead diff --git a/library/std/primitive_docs/io_read.md b/library/std/primitive_docs/io_read.md new file mode 100644 index 0000000000000..5118d7c4888ab --- /dev/null +++ b/library/std/primitive_docs/io_read.md @@ -0,0 +1 @@ +io::Read diff --git a/library/std/primitive_docs/io_seek.md b/library/std/primitive_docs/io_seek.md new file mode 100644 index 0000000000000..122e6df77b6d7 --- /dev/null +++ b/library/std/primitive_docs/io_seek.md @@ -0,0 +1 @@ +io::Seek diff --git a/library/std/primitive_docs/io_write.md b/library/std/primitive_docs/io_write.md new file mode 100644 index 0000000000000..15dfc907a6555 --- /dev/null +++ b/library/std/primitive_docs/io_write.md @@ -0,0 +1 @@ +io::Write diff --git a/library/std/primitive_docs/net_tosocketaddrs.md b/library/std/primitive_docs/net_tosocketaddrs.md new file mode 100644 index 0000000000000..a01f318e88771 --- /dev/null +++ b/library/std/primitive_docs/net_tosocketaddrs.md @@ -0,0 +1 @@ +net::ToSocketAddrs diff --git a/library/std/primitive_docs/process_exit.md b/library/std/primitive_docs/process_exit.md new file mode 100644 index 0000000000000..565a71375cd0e --- /dev/null +++ b/library/std/primitive_docs/process_exit.md @@ -0,0 +1 @@ +process::exit diff --git a/library/std/primitive_docs/string_string.md b/library/std/primitive_docs/string_string.md new file mode 100644 index 0000000000000..ce7815ff91b9e --- /dev/null +++ b/library/std/primitive_docs/string_string.md @@ -0,0 +1 @@ +string::String diff --git a/library/std/src/os/raw/mod.rs b/library/std/src/os/raw/mod.rs index 50464a050c706..1e220ea30ab95 100644 --- a/library/std/src/os/raw/mod.rs +++ b/library/std/src/os/raw/mod.rs @@ -151,3 +151,17 @@ type_alias_no_nz! { "double.md", c_double = f64; } #[stable(feature = "raw_os", since = "1.1.0")] #[doc(no_inline)] pub use core::ffi::c_void; + +/// Equivalent to C's `size_t` type, from `stddef.h` (or `cstddef` for C++). +/// +/// This type is currently always [`usize`], however in the future there may be +/// platforms where this is not the case. +#[unstable(feature = "c_size_t", issue = "88345")] +pub type c_size_t = usize; + +/// Equivalent to C's `ssize_t` type, from `stddef.h` (or `cstddef` for C++). +/// +/// This type is currently always [`isize`], however in the future there may be +/// platforms where this is not the case. +#[unstable(feature = "c_size_t", issue = "88345")] +pub type c_ssize_t = isize; diff --git a/library/std/src/primitive_docs.rs b/library/std/src/primitive_docs.rs index dc4572cd9363b..edd19ccdeb2ef 100644 --- a/library/std/src/primitive_docs.rs +++ b/library/std/src/primitive_docs.rs @@ -1,3 +1,6 @@ +// `library/{std,core}/src/primitive_docs.rs` should have the same contents. +// These are different files so that relative links work properly without +// having to have `CARGO_PKG_NAME` set, but conceptually they should always be the same. #[doc(primitive = "bool")] #[doc(alias = "true")] #[doc(alias = "false")] @@ -100,7 +103,7 @@ mod prim_bool {} /// behaviour of the `!` type - expressions with type `!` will coerce into any other type. /// /// [`u32`]: prim@u32 -/// [`exit`]: process::exit +#[doc = concat!("[`exit`]: ", include_str!("../primitive_docs/process_exit.md"))] /// /// # `!` and generics /// @@ -185,7 +188,7 @@ mod prim_bool {} /// because `!` coerces to `Result` automatically. /// /// [`String::from_str`]: str::FromStr::from_str -/// [`String`]: string::String +#[doc = concat!("[`String`]: ", include_str!("../primitive_docs/string_string.md"))] /// [`FromStr`]: str::FromStr /// /// # `!` and traits @@ -261,7 +264,7 @@ mod prim_bool {} /// `impl` for this which simply panics, but the same is true for any type (we could `impl /// Default` for (eg.) [`File`] by just making [`default()`] panic.) /// -/// [`File`]: fs::File +#[doc = concat!("[`File`]: ", include_str!("../primitive_docs/fs_file.md"))] /// [`Debug`]: fmt::Debug /// [`default()`]: Default::default /// @@ -269,7 +272,6 @@ mod prim_bool {} mod prim_never {} #[doc(primitive = "char")] -// /// A character type. /// /// The `char` type represents a single character. More specifically, since @@ -301,7 +303,7 @@ mod prim_never {} /// assert_eq!(5, s.len() * std::mem::size_of::()); /// ``` /// -/// [`String`]: string/struct.String.html +#[doc = concat!("[`String`]: ", include_str!("../primitive_docs/string_string.md"))] /// /// As always, remember that a human intuition for 'character' might not map to /// Unicode's definitions. For example, despite looking similar, the 'é' @@ -493,7 +495,7 @@ mod prim_unit {} /// [`null_mut`]: ptr::null_mut /// [`is_null`]: pointer::is_null /// [`offset`]: pointer::offset -/// [`into_raw`]: Box::into_raw +#[doc = concat!("[`into_raw`]: ", include_str!("../primitive_docs/box_into_raw.md"))] /// [`drop`]: mem::drop /// [`write`]: ptr::write #[stable(feature = "rust1", since = "1.0.0")] @@ -1123,7 +1125,7 @@ mod prim_usize {} /// [`std::fmt`]: fmt /// ['Pointer`]: fmt::Pointer /// [`Hash`]: hash::Hash -/// [`ToSocketAddrs`]: net::ToSocketAddrs +#[doc = concat!("[`ToSocketAddrs`]: ", include_str!("../primitive_docs/net_tosocketaddrs.md"))] /// /// `&mut T` references get all of the above except `ToSocketAddrs`, plus the following, if `T` /// implements that trait: @@ -1144,9 +1146,10 @@ mod prim_usize {} /// /// [`FusedIterator`]: iter::FusedIterator /// [`TrustedLen`]: iter::TrustedLen -/// [`Seek`]: io::Seek -/// [`BufRead`]: io::BufRead -/// [`Read`]: io::Read +#[doc = concat!("[`Seek`]: ", include_str!("../primitive_docs/io_seek.md"))] +#[doc = concat!("[`BufRead`]: ", include_str!("../primitive_docs/io_bufread.md"))] +#[doc = concat!("[`Read`]: ", include_str!("../primitive_docs/io_read.md"))] +#[doc = concat!("[`io::Write`]: ", include_str!("../primitive_docs/io_write.md"))] /// /// Note that due to method call deref coercion, simply calling a trait method will act like they /// work on references as well as they do on owned values! The implementations described here are diff --git a/src/librustdoc/clean/blanket_impl.rs b/src/librustdoc/clean/blanket_impl.rs index ff3641d6c9ab0..15d4563ad7461 100644 --- a/src/librustdoc/clean/blanket_impl.rs +++ b/src/librustdoc/clean/blanket_impl.rs @@ -17,7 +17,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> { let param_env = self.cx.tcx.param_env(item_def_id); let ty = self.cx.tcx.type_of(item_def_id); - debug!("get_blanket_impls({:?})", ty); + trace!("get_blanket_impls({:?})", ty); let mut impls = Vec::new(); for &trait_def_id in self.cx.tcx.all_traits(()).iter() { if !self.cx.cache.access_levels.is_public(trait_def_id) @@ -28,9 +28,10 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> { // NOTE: doesn't use `for_each_relevant_impl` to avoid looking at anything besides blanket impls let trait_impls = self.cx.tcx.trait_impls_of(trait_def_id); for &impl_def_id in trait_impls.blanket_impls() { - debug!( + trace!( "get_blanket_impls: Considering impl for trait '{:?}' {:?}", - trait_def_id, impl_def_id + trait_def_id, + impl_def_id ); let trait_ref = self.cx.tcx.impl_trait_ref(impl_def_id).unwrap(); let is_param = matches!(trait_ref.self_ty().kind(), ty::Param(_)); @@ -50,9 +51,11 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> { // FIXME(eddyb) ignoring `obligations` might cause false positives. drop(obligations); - debug!( + trace!( "invoking predicate_may_hold: param_env={:?}, trait_ref={:?}, ty={:?}", - param_env, trait_ref, ty + param_env, + trait_ref, + ty ); let predicates = self .cx diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index fa29b54264948..775fc6fe41f16 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -447,9 +447,9 @@ crate fn build_impl( } let (merged_attrs, cfg) = merge_attrs(cx, parent_module.into(), load_attrs(cx, did), attrs); - debug!("merged_attrs={:?}", merged_attrs); + trace!("merged_attrs={:?}", merged_attrs); - debug!("build_impl: impl {:?} for {:?}", trait_.def_id(), for_.def_id()); + trace!("build_impl: impl {:?} for {:?}", trait_.def_id(), for_.def_id()); ret.push(clean::Item::from_def_id_and_attrs_and_parts( did, None, diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index b6ff3890c584e..f2cf8351e7adf 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -1419,7 +1419,7 @@ fn normalize(cx: &mut DocContext<'tcx>, ty: Ty<'_>) -> Option> { impl<'tcx> Clean for Ty<'tcx> { fn clean(&self, cx: &mut DocContext<'_>) -> Type { - debug!("cleaning type: {:?}", self); + trace!("cleaning type: {:?}", self); let ty = normalize(cx, self).unwrap_or(self); match *ty.kind() { ty::Never => Never, diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 7025574914331..8107bd9de2250 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -461,60 +461,20 @@ impl Item { .map_or(&[][..], |v| v.as_slice()) .iter() .filter_map(|ItemLink { link: s, link_text, did, ref fragment }| { - match did { - Some(did) => { - if let Ok((mut href, ..)) = href(did.clone(), cx) { - if let Some(ref fragment) = *fragment { - href.push('#'); - href.push_str(fragment); - } - Some(RenderedLink { - original_text: s.clone(), - new_text: link_text.clone(), - href, - }) - } else { - None - } - } - // FIXME(83083): using fragments as a side-channel for - // primitive names is very unfortunate - None => { - let relative_to = &cx.current; - if let Some(ref fragment) = *fragment { - let url = match cx.cache().extern_locations.get(&self.def_id.krate()) { - Some(&ExternalLocation::Local) => { - if relative_to[0] == "std" { - let depth = relative_to.len() - 1; - "../".repeat(depth) - } else { - let depth = relative_to.len(); - format!("{}std/", "../".repeat(depth)) - } - } - Some(ExternalLocation::Remote(ref s)) => { - format!("{}/std/", s.trim_end_matches('/')) - } - Some(ExternalLocation::Unknown) | None => { - format!("{}/std/", crate::DOC_RUST_LANG_ORG_CHANNEL) - } - }; - // This is a primitive so the url is done "by hand". - let tail = fragment.find('#').unwrap_or_else(|| fragment.len()); - Some(RenderedLink { - original_text: s.clone(), - new_text: link_text.clone(), - href: format!( - "{}primitive.{}.html{}", - url, - &fragment[..tail], - &fragment[tail..] - ), - }) - } else { - panic!("This isn't a primitive?!"); - } + debug!(?did); + if let Ok((mut href, ..)) = href(did.clone(), cx) { + debug!(?href); + if let Some(ref fragment) = *fragment { + href.push('#'); + href.push_str(fragment); } + Some(RenderedLink { + original_text: s.clone(), + new_text: link_text.clone(), + href, + }) + } else { + None } }) .collect() @@ -531,18 +491,10 @@ impl Item { .get(&self.def_id) .map_or(&[][..], |v| v.as_slice()) .iter() - .filter_map(|ItemLink { link: s, link_text, did, fragment }| { - // FIXME(83083): using fragments as a side-channel for - // primitive names is very unfortunate - if did.is_some() || fragment.is_some() { - Some(RenderedLink { - original_text: s.clone(), - new_text: link_text.clone(), - href: String::new(), - }) - } else { - None - } + .map(|ItemLink { link: s, link_text, .. }| RenderedLink { + original_text: s.clone(), + new_text: link_text.clone(), + href: String::new(), }) .collect() } @@ -962,7 +914,7 @@ crate struct Attributes { crate other_attrs: Vec, } -#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)] +#[derive(Clone, Debug, PartialEq, Eq, Hash)] /// A link that has not yet been rendered. /// /// This link will be turned into a rendered link by [`Item::links`]. @@ -974,7 +926,7 @@ crate struct ItemLink { /// This may not be the same as `link` if there was a disambiguator /// in an intra-doc link (e.g. \[`fn@f`\]) pub(crate) link_text: String, - pub(crate) did: Option, + pub(crate) did: DefId, /// The url fragment to append to the link pub(crate) fragment: Option, } @@ -1799,6 +1751,39 @@ impl PrimitiveType { Never => sym::never, } } + + /// Returns the DefId of the module with `doc(primitive)` for this primitive type. + /// Panics if there is no such module. + /// + /// This gives precedence to primitives defined in the current crate, and deprioritizes primitives defined in `core`, + /// but otherwise, if multiple crates define the same primitive, there is no guarantee of which will be picked. + /// In particular, if a crate depends on both `std` and another crate that also defines `doc(primitive)`, then + /// it's entirely random whether `std` or the other crate is picked. (no_std crates are usually fine unless multiple dependencies define a primitive.) + crate fn primitive_locations(tcx: TyCtxt<'_>) -> &FxHashMap { + static PRIMITIVE_LOCATIONS: OnceCell> = OnceCell::new(); + PRIMITIVE_LOCATIONS.get_or_init(|| { + let mut primitive_locations = FxHashMap::default(); + // NOTE: technically this misses crates that are only passed with `--extern` and not loaded when checking the crate. + // This is a degenerate case that I don't plan to support. + for &crate_num in tcx.crates(()) { + let e = ExternalCrate { crate_num }; + let crate_name = e.name(tcx); + debug!(?crate_num, ?crate_name); + for &(def_id, prim) in &e.primitives(tcx) { + // HACK: try to link to std instead where possible + if crate_name == sym::core && primitive_locations.contains_key(&prim) { + continue; + } + primitive_locations.insert(prim, def_id); + } + } + let local_primitives = ExternalCrate { crate_num: LOCAL_CRATE }.primitives(tcx); + for (def_id, prim) in local_primitives { + primitive_locations.insert(prim, def_id); + } + primitive_locations + }) + } } impl From for PrimitiveType { diff --git a/src/librustdoc/formats/cache.rs b/src/librustdoc/formats/cache.rs index 1830909d94460..66fd0d9262d6b 100644 --- a/src/librustdoc/formats/cache.rs +++ b/src/librustdoc/formats/cache.rs @@ -6,7 +6,7 @@ use rustc_middle::middle::privacy::AccessLevels; use rustc_middle::ty::TyCtxt; use rustc_span::symbol::sym; -use crate::clean::{self, GetDefId, ItemId}; +use crate::clean::{self, GetDefId, ItemId, PrimitiveType}; use crate::config::RenderOptions; use crate::fold::DocFolder; use crate::formats::item_type::ItemType; @@ -159,17 +159,16 @@ impl Cache { self.external_paths.insert(e.def_id(), (vec![name.to_string()], ItemType::Module)); } - // Cache where all known primitives have their documentation located. - // - // Favor linking to as local extern as possible, so iterate all crates in - // reverse topological order. - for &e in krate.externs.iter().rev() { - for &(def_id, prim) in &e.primitives(tcx) { - self.primitive_locations.insert(prim, def_id); - } - } - for &(def_id, prim) in &krate.primitives { - self.primitive_locations.insert(prim, def_id); + // FIXME: avoid this clone (requires implementing Default manually) + self.primitive_locations = PrimitiveType::primitive_locations(tcx).clone(); + for (prim, &def_id) in &self.primitive_locations { + let crate_name = tcx.crate_name(def_id.krate); + // Recall that we only allow primitive modules to be at the root-level of the crate. + // If that restriction is ever lifted, this will have to include the relative paths instead. + self.external_paths.insert( + def_id, + (vec![crate_name.to_string(), prim.as_sym().to_string()], ItemType::Primitive), + ); } krate = CacheBuilder { tcx, cache: self }.fold_crate(krate); diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index eb7c12d13c339..768dab66e5c78 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -495,7 +495,11 @@ crate fn href_with_root_path( if shortty == ItemType::Module { fqp } else { &fqp[..fqp.len() - 1] } } - if !did.is_local() && !cache.access_levels.is_public(did) && !cache.document_private { + if !did.is_local() + && !cache.access_levels.is_public(did) + && !cache.document_private + && !cache.primitive_locations.values().any(|&id| id == did) + { return Err(HrefError::Private); } @@ -503,6 +507,7 @@ crate fn href_with_root_path( let (fqp, shortty, mut url_parts) = match cache.paths.get(&did) { Some(&(ref fqp, shortty)) => (fqp, shortty, { let module_fqp = to_module_fqp(shortty, fqp); + debug!(?fqp, ?shortty, ?module_fqp); href_relative_parts(module_fqp, relative_to) }), None => { @@ -534,6 +539,7 @@ crate fn href_with_root_path( url_parts.insert(0, root); } } + debug!(?url_parts); let last = &fqp.last().unwrap()[..]; let filename; match shortty { @@ -728,7 +734,7 @@ fn fmt_type<'cx>( use_absolute: bool, cx: &'cx Context<'_>, ) -> fmt::Result { - debug!("fmt_type(t = {:?})", t); + trace!("fmt_type(t = {:?})", t); match *t { clean::Generic(name) => write!(f, "{}", name), diff --git a/src/librustdoc/json/conversions.rs b/src/librustdoc/json/conversions.rs index f3eeea6c6ae0b..d4eac8bc9d333 100644 --- a/src/librustdoc/json/conversions.rs +++ b/src/librustdoc/json/conversions.rs @@ -30,9 +30,7 @@ impl JsonRenderer<'_> { .get(&item.def_id) .into_iter() .flatten() - .filter_map(|clean::ItemLink { link, did, .. }| { - did.map(|did| (link.clone(), from_item_id(did.into()))) - }) + .map(|clean::ItemLink { link, did, .. }| (link.clone(), from_item_id((*did).into()))) .collect(); let docs = item.attrs.collapsed_doc_value(); let attrs = item diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index 807872ae4fde3..3ade1526d05a9 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -14,7 +14,7 @@ use rustc_hir::def::{ }; use rustc_hir::def_id::{CrateNum, DefId}; use rustc_middle::ty::TyCtxt; -use rustc_middle::{bug, ty}; +use rustc_middle::{bug, span_bug, ty}; use rustc_resolve::ParentScope; use rustc_session::lint::Lint; use rustc_span::hygiene::{MacroKind, SyntaxContext}; @@ -95,14 +95,10 @@ impl Res { } } - fn def_id(self) -> DefId { - self.opt_def_id().expect("called def_id() on a primitive") - } - - fn opt_def_id(self) -> Option { + fn def_id(self, tcx: TyCtxt<'_>) -> DefId { match self { - Res::Def(_, id) => Some(id), - Res::Primitive(_) => None, + Res::Def(_, id) => id, + Res::Primitive(prim) => *PrimitiveType::primitive_locations(tcx).get(&prim).unwrap(), } } @@ -385,7 +381,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> { ty::AssocKind::Const => "associatedconstant", ty::AssocKind::Type => "associatedtype", }; - let fragment = format!("{}#{}.{}", prim_ty.as_sym(), out, item_name); + let fragment = format!("{}.{}", out, item_name); (Res::Primitive(prim_ty), fragment, Some((kind.as_def_kind(), item.def_id))) }) }) @@ -472,14 +468,6 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> { return handle_variant(self.cx, res, extra_fragment); } // Not a trait item; just return what we found. - Res::Primitive(ty) => { - if extra_fragment.is_some() { - return Err(ErrorKind::AnchorFailure( - AnchorFailure::RustdocAnchorConflict(res), - )); - } - return Ok((res, Some(ty.as_sym().to_string()))); - } _ => return Ok((res, extra_fragment.clone())), } } @@ -1149,7 +1137,7 @@ impl LinkCollector<'_, '_> { module_id = DefId { krate, index: CRATE_DEF_INDEX }; } - let (mut res, mut fragment) = self.resolve_with_disambiguator_cached( + let (mut res, fragment) = self.resolve_with_disambiguator_cached( ResolutionInfo { module_id, dis: disambiguator, @@ -1171,16 +1159,7 @@ impl LinkCollector<'_, '_> { if let Some(prim) = resolve_primitive(path_str, TypeNS) { // `prim@char` if matches!(disambiguator, Some(Disambiguator::Primitive)) { - if fragment.is_some() { - anchor_failure( - self.cx, - diag_info, - AnchorFailure::RustdocAnchorConflict(prim), - ); - return None; - } res = prim; - fragment = Some(prim.name(self.cx.tcx).to_string()); } else { // `[char]` when a `char` module is in scope let candidates = vec![res, prim]; @@ -1300,12 +1279,17 @@ impl LinkCollector<'_, '_> { } } - Some(ItemLink { link: ori_link.link, link_text, did: None, fragment }) + Some(ItemLink { + link: ori_link.link, + link_text, + did: res.def_id(self.cx.tcx), + fragment, + }) } Res::Def(kind, id) => { verify(kind, id)?; let id = clean::register_res(self.cx, rustc_hir::def::Res::Def(kind, id)); - Some(ItemLink { link: ori_link.link, link_text, did: Some(id), fragment }) + Some(ItemLink { link: ori_link.link, link_text, did: id, fragment }) } } } @@ -2066,8 +2050,11 @@ fn anchor_failure(cx: &DocContext<'_>, diag_info: DiagnosticInfo<'_>, failure: A diag.span_label(sp, "invalid anchor"); } if let AnchorFailure::RustdocAnchorConflict(Res::Primitive(_)) = failure { - diag.note("this restriction may be lifted in a future release"); - diag.note("see https://github.com/rust-lang/rust/issues/83083 for more information"); + if let Some(sp) = sp { + span_bug!(sp, "anchors should be allowed now"); + } else { + bug!("anchors should be allowed now"); + } } }); } @@ -2198,7 +2185,7 @@ fn handle_variant( return Err(ErrorKind::AnchorFailure(AnchorFailure::RustdocAnchorConflict(res))); } cx.tcx - .parent(res.def_id()) + .parent(res.def_id(cx.tcx)) .map(|parent| { let parent_def = Res::Def(DefKind::Enum, parent); let variant = cx.tcx.expect_variant_res(res.as_hir_res().unwrap()); diff --git a/src/test/rustdoc-js-std/multi-query.js b/src/test/rustdoc-js-std/multi-query.js index 01e54065189c5..1c92d019606b6 100644 --- a/src/test/rustdoc-js-std/multi-query.js +++ b/src/test/rustdoc-js-std/multi-query.js @@ -2,8 +2,9 @@ const QUERY = 'str,u8'; const EXPECTED = { 'others': [ - { 'path': 'std', 'name': 'str' }, - { 'path': 'std', 'name': 'u8' }, - { 'path': 'std::ffi', 'name': 'CStr' }, + { 'path': 'std', 'name': 'str', 'href': '../std/primitive.str.html' }, + { 'path': 'std', 'name': 'u8', 'href': '../std/primitive.u8.html' }, + { 'path': 'std', 'name': 'str', 'href': '../std/str/index.html' }, + { 'path': 'std', 'name': 'u8', 'href': '../std/u8/index.html' }, ], }; diff --git a/src/test/rustdoc-ui/intra-doc/anchors.rs b/src/test/rustdoc-ui/intra-doc/anchors.rs index 6785cb7abeaee..34e11c7c7b7cf 100644 --- a/src/test/rustdoc-ui/intra-doc/anchors.rs +++ b/src/test/rustdoc-ui/intra-doc/anchors.rs @@ -37,13 +37,3 @@ pub fn bar() {} /// Damn enum's variants: [Enum::A#whatever]. //~^ ERROR `Enum::A#whatever` contains an anchor pub fn enum_link() {} - -/// Primitives? -/// -/// [u32#hello] -//~^ ERROR `u32#hello` contains an anchor -pub fn x() {} - -/// [prim@usize#x] -//~^ ERROR `prim@usize#x` contains an anchor -pub mod usize {} diff --git a/src/test/rustdoc-ui/intra-doc/anchors.stderr b/src/test/rustdoc-ui/intra-doc/anchors.stderr index d63e1ee60b3c5..0d226b277535c 100644 --- a/src/test/rustdoc-ui/intra-doc/anchors.stderr +++ b/src/test/rustdoc-ui/intra-doc/anchors.stderr @@ -1,19 +1,3 @@ -error: `prim@usize#x` contains an anchor, but links to builtin types are already anchored - --> $DIR/anchors.rs:47:6 - | -LL | /// [prim@usize#x] - | ^^^^^^^^^^-- - | | - | invalid anchor - | -note: the lint level is defined here - --> $DIR/anchors.rs:1:9 - | -LL | #![deny(rustdoc::broken_intra_doc_links)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - = note: this restriction may be lifted in a future release - = note: see https://github.com/rust-lang/rust/issues/83083 for more information - error: `Foo::f#hola` contains an anchor, but links to fields are already anchored --> $DIR/anchors.rs:25:15 | @@ -21,6 +5,12 @@ LL | /// Or maybe [Foo::f#hola]. | ^^^^^^----- | | | invalid anchor + | +note: the lint level is defined here + --> $DIR/anchors.rs:1:9 + | +LL | #![deny(rustdoc::broken_intra_doc_links)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: `hello#people#!` contains multiple anchors --> $DIR/anchors.rs:31:28 @@ -38,16 +28,5 @@ LL | /// Damn enum's variants: [Enum::A#whatever]. | | | invalid anchor -error: `u32#hello` contains an anchor, but links to builtin types are already anchored - --> $DIR/anchors.rs:43:6 - | -LL | /// [u32#hello] - | ^^^------ - | | - | invalid anchor - | - = note: this restriction may be lifted in a future release - = note: see https://github.com/rust-lang/rust/issues/83083 for more information - -error: aborting due to 5 previous errors +error: aborting due to 3 previous errors diff --git a/src/test/rustdoc/auxiliary/issue-15318.rs b/src/test/rustdoc/auxiliary/issue-15318.rs index 83cc31b587c29..96e2927a43ea0 100644 --- a/src/test/rustdoc/auxiliary/issue-15318.rs +++ b/src/test/rustdoc/auxiliary/issue-15318.rs @@ -1,6 +1,39 @@ // compile-flags: -Cmetadata=aux #![doc(html_root_url = "http://example.com/")] +#![feature(lang_items)] +#![no_std] + +#[cfg(windows)] +#[link(name = "vcruntime")] +extern {} + +#[cfg(windows)] +#[link(name = "ucrt")] +extern {} + +#[cfg(windows)] +#[no_mangle] +#[used] +static _fltused: i32 = 0; +#[cfg(windows)] +#[no_mangle] +#[used] +static __aullrem: i32 = 0; +#[cfg(windows)] +#[no_mangle] +#[used] +static __aulldiv: i32 = 0; + +#[cfg(windows)] +#[no_mangle] +extern "system" fn _DllMainCRTStartup(_: *const u8, _: u32, _: *const u8) -> u32 { 1 } + +#[lang = "eh_personality"] +fn foo() {} + +#[panic_handler] +fn bar(_: &core::panic::PanicInfo) -> ! { loop {} } /// dox #[doc(primitive = "pointer")] diff --git a/src/test/rustdoc/auxiliary/primitive-doc.rs b/src/test/rustdoc/auxiliary/primitive-doc.rs index a5b69740dd447..811bda11d4637 100644 --- a/src/test/rustdoc/auxiliary/primitive-doc.rs +++ b/src/test/rustdoc/auxiliary/primitive-doc.rs @@ -1,4 +1,15 @@ // compile-flags: --crate-type lib --edition 2018 +#![no_core] +#![feature(no_core)] +#![feature(lang_items)] + +#[cfg(windows)] +#[no_mangle] +extern "system" fn _DllMainCRTStartup(_: *const u8, _: u32, _: *const u8) -> u32 { 1 } + +#[cfg(windows)] +#[lang = "sized"] +trait Sized {} #[doc(primitive = "usize")] /// This is the built-in type `usize`. diff --git a/src/test/rustdoc/cross-crate-primitive-doc.rs b/src/test/rustdoc/cross-crate-primitive-doc.rs index 05376e4680ec4..709ffa1818066 100644 --- a/src/test/rustdoc/cross-crate-primitive-doc.rs +++ b/src/test/rustdoc/cross-crate-primitive-doc.rs @@ -1,9 +1,11 @@ // aux-build:primitive-doc.rs // compile-flags: --extern-html-root-url=primitive_doc=../ -Z unstable-options - -#![no_std] +#![feature(no_core)] +#![no_core] extern crate primitive_doc; // @has 'cross_crate_primitive_doc/fn.foo.html' '//a[@href="../primitive_doc/primitive.usize.html"]' 'usize' +// @has 'cross_crate_primitive_doc/fn.foo.html' '//a[@href="../primitive_doc/primitive.usize.html"]' 'link' +/// [link](usize) pub fn foo() -> usize { 0 } diff --git a/src/test/rustdoc/intra-doc/anchors.rs b/src/test/rustdoc/intra-doc/anchors.rs index 8ec1a7b4f9056..3d4c464960bbc 100644 --- a/src/test/rustdoc/intra-doc/anchors.rs +++ b/src/test/rustdoc/intra-doc/anchors.rs @@ -10,3 +10,15 @@ pub struct Something; /// /// To link to [Something#Anchor!] pub struct SomeOtherType; + +/// Primitives? +/// +/// [u32#hello] +// @has anchors/fn.x.html +// @has - '//a/@href' '{{channel}}/std/primitive.u32.html#hello' +pub fn x() {} + +/// [prim@usize#x] +// @has anchors/usize/index.html +// @has - '//a/@href' '{{channel}}/std/primitive.usize.html#x' +pub mod usize {} diff --git a/src/test/rustdoc/intra-doc/auxiliary/my-core.rs b/src/test/rustdoc/intra-doc/auxiliary/my-core.rs index 54e986be9eccf..92cfd46188b01 100644 --- a/src/test/rustdoc/intra-doc/auxiliary/my-core.rs +++ b/src/test/rustdoc/intra-doc/auxiliary/my-core.rs @@ -2,6 +2,10 @@ #![no_core] #![crate_type="rlib"] +#[doc(primitive = "char")] +/// Some char docs +mod char {} + #[lang = "char"] impl char { pub fn len_utf8(self) -> usize { diff --git a/src/test/rustdoc/intra-doc/prim-methods-external-core.rs b/src/test/rustdoc/intra-doc/prim-methods-external-core.rs index 9347d7bb42819..5a92a28556ede 100644 --- a/src/test/rustdoc/intra-doc/prim-methods-external-core.rs +++ b/src/test/rustdoc/intra-doc/prim-methods-external-core.rs @@ -9,8 +9,8 @@ #![crate_type = "rlib"] // @has prim_methods_external_core/index.html -// @has - '//*[@id="main"]//a[@href="{{channel}}/std/primitive.char.html"]' 'char' -// @has - '//*[@id="main"]//a[@href="{{channel}}/std/primitive.char.html#method.len_utf8"]' 'char::len_utf8' +// @has - '//*[@id="main"]//a[@href="../my_core/primitive.char.html"]' 'char' +// @has - '//*[@id="main"]//a[@href="../my_core/primitive.char.html#method.len_utf8"]' 'char::len_utf8' //! A [`char`] and its [`char::len_utf8`]. diff --git a/src/test/rustdoc/intra-doc/prim-methods-local.rs b/src/test/rustdoc/intra-doc/prim-methods-local.rs index 124faa9a636ff..cfb3c3842ab16 100644 --- a/src/test/rustdoc/intra-doc/prim-methods-local.rs +++ b/src/test/rustdoc/intra-doc/prim-methods-local.rs @@ -5,10 +5,13 @@ // @has prim_methods_local/index.html -// @has - '//*[@id="main"]//a[@href="{{channel}}/std/primitive.char.html"]' 'char' -// @has - '//*[@id="main"]//a[@href="{{channel}}/std/primitive.char.html#method.len_utf8"]' 'char::len_utf8' +// @has - '//*[@id="main"]//a[@href="primitive.char.html"]' 'char' +// @has - '//*[@id="main"]//a[@href="primitive.char.html#method.len_utf8"]' 'char::len_utf8' -//! A [`char`] and its [`char::len_utf8`]. +//! A [prim@`char`] and its [`char::len_utf8`]. + +#[doc(primitive = "char")] +mod char {} #[lang = "char"] impl char { diff --git a/src/test/rustdoc/intra-link-prim-self.rs b/src/test/rustdoc/intra-link-prim-self.rs index 4744c84b6226d..8c47f7ef77e51 100644 --- a/src/test/rustdoc/intra-link-prim-self.rs +++ b/src/test/rustdoc/intra-link-prim-self.rs @@ -7,8 +7,8 @@ /// [Self::f] /// [Self::MAX] // @has intra_link_prim_self/primitive.usize.html -// @has - '//a[@href="{{channel}}/std/primitive.usize.html#method.f"]' 'Self::f' -// @has - '//a[@href="{{channel}}/std/primitive.usize.html#associatedconstant.MAX"]' 'Self::MAX' +// @has - '//a[@href="primitive.usize.html#method.f"]' 'Self::f' +// @has - '//a[@href="primitive.usize.html#associatedconstant.MAX"]' 'Self::MAX' impl usize { /// Some docs pub fn f() {} diff --git a/src/test/rustdoc/issue-15318-2.rs b/src/test/rustdoc/issue-15318-2.rs index 2af811ad5bbe8..f7f5052a36dd3 100644 --- a/src/test/rustdoc/issue-15318-2.rs +++ b/src/test/rustdoc/issue-15318-2.rs @@ -1,5 +1,6 @@ // aux-build:issue-15318.rs // ignore-cross-compile +#![no_std] extern crate issue_15318; diff --git a/src/test/rustdoc/no_std-primitive.rs b/src/test/rustdoc/no_std-primitive.rs new file mode 100644 index 0000000000000..22fd392dd36c3 --- /dev/null +++ b/src/test/rustdoc/no_std-primitive.rs @@ -0,0 +1,6 @@ +#![no_std] + +/// Link to [intra-doc link][u8] +// @has 'no_std_primitive/fn.foo.html' '//a[@href="{{channel}}/core/primitive.u8.html"]' 'intra-doc link' +// @has - '//a[@href="{{channel}}/core/primitive.u8.html"]' 'u8' +pub fn foo() -> u8 {} diff --git a/src/test/rustdoc/primitive/no_std.rs b/src/test/rustdoc/primitive/no_std.rs new file mode 100644 index 0000000000000..f0f70cb6c1881 --- /dev/null +++ b/src/test/rustdoc/primitive/no_std.rs @@ -0,0 +1,16 @@ +#![no_std] +#![deny(warnings)] +#![deny(rustdoc::broken_intra_doc_links)] + +// @has no_std/fn.foo.html '//a/[@href="{{channel}}/core/primitive.u8.html"]' 'u8' +// @has no_std/fn.foo.html '//a/[@href="{{channel}}/core/primitive.u8.html"]' 'primitive link' +/// Link to [primitive link][u8] +pub fn foo() -> u8 {} + +// Test that all primitives can be linked to. +/// [isize] [i8] [i16] [i32] [i64] [i128] +/// [usize] [u8] [u16] [u32] [u64] [u128] +/// [f32] [f64] +/// [char] [bool] [str] [slice] [array] [tuple] [unit] +/// [pointer] [reference] [fn] [never] +pub fn bar() {} diff --git a/src/test/ui/destructuring-assignment/tuple_struct_destructure_fail.stderr b/src/test/ui/destructuring-assignment/tuple_struct_destructure_fail.stderr index 0e92cc5c9f282..9aae4b0a3faed 100644 --- a/src/test/ui/destructuring-assignment/tuple_struct_destructure_fail.stderr +++ b/src/test/ui/destructuring-assignment/tuple_struct_destructure_fail.stderr @@ -15,22 +15,22 @@ LL | Enum::SingleVariant(a, .., b, ..) = Enum::SingleVariant(0, 1); | previously used here error[E0023]: this pattern has 3 fields, but the corresponding tuple struct has 2 fields - --> $DIR/tuple_struct_destructure_fail.rs:30:5 + --> $DIR/tuple_struct_destructure_fail.rs:30:17 | LL | struct TupleStruct(S, T); - | ------------------------------- tuple struct defined here + | - - tuple struct has 2 fields ... LL | TupleStruct(a, a, b) = TupleStruct(1, 2); - | ^^^^^^^^^^^^^^^^^^^^ expected 2 fields, found 3 + | ^ ^ ^ expected 2 fields, found 3 error[E0023]: this pattern has 1 field, but the corresponding tuple struct has 2 fields - --> $DIR/tuple_struct_destructure_fail.rs:32:5 + --> $DIR/tuple_struct_destructure_fail.rs:32:17 | LL | struct TupleStruct(S, T); - | ------------------------------- tuple struct defined here + | - - tuple struct has 2 fields ... LL | TupleStruct(_) = TupleStruct(1, 2); - | ^^^^^^^^^^^^^^ expected 2 fields, found 1 + | ^ expected 2 fields, found 1 | help: use `_` to explicitly ignore each field | @@ -42,22 +42,22 @@ LL | TupleStruct(..) = TupleStruct(1, 2); | ~~ error[E0023]: this pattern has 3 fields, but the corresponding tuple variant has 2 fields - --> $DIR/tuple_struct_destructure_fail.rs:34:5 + --> $DIR/tuple_struct_destructure_fail.rs:34:25 | LL | SingleVariant(S, T) - | ------------------- tuple variant defined here + | - - tuple variant has 2 fields ... LL | Enum::SingleVariant(a, a, b) = Enum::SingleVariant(1, 2); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected 2 fields, found 3 + | ^ ^ ^ expected 2 fields, found 3 error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 2 fields - --> $DIR/tuple_struct_destructure_fail.rs:36:5 + --> $DIR/tuple_struct_destructure_fail.rs:36:25 | LL | SingleVariant(S, T) - | ------------------- tuple variant defined here + | - - tuple variant has 2 fields ... LL | Enum::SingleVariant(_) = Enum::SingleVariant(1, 2); - | ^^^^^^^^^^^^^^^^^^^^^^ expected 2 fields, found 1 + | ^ expected 2 fields, found 1 | help: use `_` to explicitly ignore each field | diff --git a/src/test/ui/error-codes/E0023.stderr b/src/test/ui/error-codes/E0023.stderr index ec3aae2971410..3e321b037b2b2 100644 --- a/src/test/ui/error-codes/E0023.stderr +++ b/src/test/ui/error-codes/E0023.stderr @@ -1,11 +1,11 @@ error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 2 fields - --> $DIR/E0023.rs:11:9 + --> $DIR/E0023.rs:11:22 | LL | Apple(String, String), - | --------------------- tuple variant defined here + | ------ ------ tuple variant has 2 fields ... LL | Fruit::Apple(a) => {}, - | ^^^^^^^^^^^^^^^ expected 2 fields, found 1 + | ^ expected 2 fields, found 1 | help: use `_` to explicitly ignore each field | @@ -13,31 +13,31 @@ LL | Fruit::Apple(a, _) => {}, | +++ error[E0023]: this pattern has 3 fields, but the corresponding tuple variant has 2 fields - --> $DIR/E0023.rs:12:9 + --> $DIR/E0023.rs:12:22 | LL | Apple(String, String), - | --------------------- tuple variant defined here + | ------ ------ tuple variant has 2 fields ... LL | Fruit::Apple(a, b, c) => {}, - | ^^^^^^^^^^^^^^^^^^^^^ expected 2 fields, found 3 + | ^ ^ ^ expected 2 fields, found 3 error[E0023]: this pattern has 2 fields, but the corresponding tuple variant has 1 field - --> $DIR/E0023.rs:13:9 + --> $DIR/E0023.rs:13:21 | LL | Pear(u32), - | --------- tuple variant defined here + | --- tuple variant has 1 field ... LL | Fruit::Pear(1, 2) => {}, - | ^^^^^^^^^^^^^^^^^ expected 1 field, found 2 + | ^ ^ expected 1 field, found 2 error[E0023]: this pattern has 2 fields, but the corresponding tuple variant has 1 field - --> $DIR/E0023.rs:14:9 + --> $DIR/E0023.rs:14:23 | LL | Orange((String, String)), - | ------------------------ tuple variant defined here + | ---------------- tuple variant has 1 field ... LL | Fruit::Orange(a, b) => {}, - | ^^^^^^^^^^^^^^^^^^^ expected 1 field, found 2 + | ^ ^ expected 1 field, found 2 | help: missing parentheses | @@ -48,7 +48,7 @@ error[E0023]: this pattern has 0 fields, but the corresponding tuple variant has --> $DIR/E0023.rs:15:9 | LL | Banana(()), - | ---------- tuple variant defined here + | -- tuple variant has 1 field ... LL | Fruit::Banana() => {}, | ^^^^^^^^^^^^^^^ expected 1 field, found 0 diff --git a/src/test/ui/hrtb/due-to-where-clause.nll.stderr b/src/test/ui/hrtb/due-to-where-clause.nll.stderr deleted file mode 100644 index 90803a0adb01b..0000000000000 --- a/src/test/ui/hrtb/due-to-where-clause.nll.stderr +++ /dev/null @@ -1,8 +0,0 @@ -error: higher-ranked subtype error - --> $DIR/due-to-where-clause.rs:2:5 - | -LL | test::(&mut 42); - | ^^^^^^^^^^^^ - -error: aborting due to previous error - diff --git a/src/test/ui/hrtb/hrtb-cache-issue-54302.nll.stderr b/src/test/ui/hrtb/hrtb-cache-issue-54302.nll.stderr deleted file mode 100644 index 4de35d70c30a3..0000000000000 --- a/src/test/ui/hrtb/hrtb-cache-issue-54302.nll.stderr +++ /dev/null @@ -1,8 +0,0 @@ -error: higher-ranked subtype error - --> $DIR/hrtb-cache-issue-54302.rs:19:5 - | -LL | assert_deserialize_owned::<&'static str>(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -error: aborting due to previous error - diff --git a/src/test/ui/hrtb/hrtb-just-for-static.nll.stderr b/src/test/ui/hrtb/hrtb-just-for-static.nll.stderr index a812282def9a8..17d59bb321a45 100644 --- a/src/test/ui/hrtb/hrtb-just-for-static.nll.stderr +++ b/src/test/ui/hrtb/hrtb-just-for-static.nll.stderr @@ -17,11 +17,14 @@ LL | want_hrtb::<&'a u32>() | = help: consider replacing `'a` with `'static` -error: higher-ranked subtype error +error: implementation of `Foo` is not general enough --> $DIR/hrtb-just-for-static.rs:30:5 | LL | want_hrtb::<&'a u32>() - | ^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^^^^^^ implementation of `Foo` is not general enough + | + = note: `Foo<&'0 isize>` would have to be implemented for the type `&u32`, for any lifetime `'0`... + = note: ...but `Foo<&'1 isize>` is actually implemented for the type `&'1 u32`, for some specific lifetime `'1` error: aborting due to 3 previous errors diff --git a/src/test/ui/issues/issue-54302.nll.stderr b/src/test/ui/issues/issue-54302.nll.stderr deleted file mode 100644 index e68de0312824d..0000000000000 --- a/src/test/ui/issues/issue-54302.nll.stderr +++ /dev/null @@ -1,8 +0,0 @@ -error: higher-ranked subtype error - --> $DIR/issue-54302.rs:13:5 - | -LL | assert_deserialize_owned::<&'static str>(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -error: aborting due to previous error - diff --git a/src/test/ui/issues/issue-72574-2.stderr b/src/test/ui/issues/issue-72574-2.stderr index 928fa58b17556..05650f05cbf5b 100644 --- a/src/test/ui/issues/issue-72574-2.stderr +++ b/src/test/ui/issues/issue-72574-2.stderr @@ -19,13 +19,13 @@ LL | Binder(_a, _x @ ..) => {} = note: only allowed in tuple, tuple struct, and slice patterns error[E0023]: this pattern has 2 fields, but the corresponding tuple struct has 3 fields - --> $DIR/issue-72574-2.rs:6:9 + --> $DIR/issue-72574-2.rs:6:16 | LL | struct Binder(i32, i32, i32); - | ----------------------------- tuple struct defined here + | --- --- --- tuple struct has 3 fields ... LL | Binder(_a, _x @ ..) => {} - | ^^^^^^^^^^^^^^^^^^^ expected 3 fields, found 2 + | ^^ ^^^^^^^ expected 3 fields, found 2 | help: use `_` to explicitly ignore each field | diff --git a/src/test/ui/match/match-pattern-field-mismatch.stderr b/src/test/ui/match/match-pattern-field-mismatch.stderr index e34164ec0db24..c994ee4f6d4ff 100644 --- a/src/test/ui/match/match-pattern-field-mismatch.stderr +++ b/src/test/ui/match/match-pattern-field-mismatch.stderr @@ -1,11 +1,11 @@ error[E0023]: this pattern has 2 fields, but the corresponding tuple variant has 3 fields - --> $DIR/match-pattern-field-mismatch.rs:10:11 + --> $DIR/match-pattern-field-mismatch.rs:10:22 | LL | Rgb(usize, usize, usize), - | ------------------------ tuple variant defined here + | ----- ----- ----- tuple variant has 3 fields ... LL | Color::Rgb(_, _) => { } - | ^^^^^^^^^^^^^^^^ expected 3 fields, found 2 + | ^ ^ expected 3 fields, found 2 | help: use `_` to explicitly ignore each field | diff --git a/src/test/ui/pattern/auxiliary/declarations-for-tuple-field-count-errors.rs b/src/test/ui/pattern/auxiliary/declarations-for-tuple-field-count-errors.rs new file mode 100644 index 0000000000000..f7373c453966c --- /dev/null +++ b/src/test/ui/pattern/auxiliary/declarations-for-tuple-field-count-errors.rs @@ -0,0 +1,20 @@ +pub struct Z0; +pub struct Z1(); + +pub struct S(pub u8, pub u8, pub u8); +pub struct M( + pub u8, + pub u8, + pub u8, +); + +pub enum E1 { Z0, Z1(), S(u8, u8, u8) } + +pub enum E2 { + S(u8, u8, u8), + M( + u8, + u8, + u8, + ), +} diff --git a/src/test/ui/pattern/issue-67037-pat-tup-scrut-ty-diff-less-fields.stderr b/src/test/ui/pattern/issue-67037-pat-tup-scrut-ty-diff-less-fields.stderr index 7f5da8f3c2dcc..75a231f6b4ba3 100644 --- a/src/test/ui/pattern/issue-67037-pat-tup-scrut-ty-diff-less-fields.stderr +++ b/src/test/ui/pattern/issue-67037-pat-tup-scrut-ty-diff-less-fields.stderr @@ -13,7 +13,7 @@ error[E0023]: this pattern has 0 fields, but the corresponding tuple struct has --> $DIR/issue-67037-pat-tup-scrut-ty-diff-less-fields.rs:19:9 | LL | struct P(T); // 1 type parameter wanted - | --------------- tuple struct defined here + | - tuple struct has 1 field ... LL | let P() = U {}; | ^^^ expected 1 field, found 0 diff --git a/src/test/ui/pattern/issue-74539.stderr b/src/test/ui/pattern/issue-74539.stderr index 7d998af7fb388..7443946c013f7 100644 --- a/src/test/ui/pattern/issue-74539.stderr +++ b/src/test/ui/pattern/issue-74539.stderr @@ -19,13 +19,13 @@ LL | E::A(x @ ..) => { = note: only allowed in tuple, tuple struct, and slice patterns error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 2 fields - --> $DIR/issue-74539.rs:8:9 + --> $DIR/issue-74539.rs:8:14 | LL | A(u8, u8), - | --------- tuple variant defined here + | -- -- tuple variant has 2 fields ... LL | E::A(x @ ..) => { - | ^^^^^^^^^^^^ expected 2 fields, found 1 + | ^^^^^^ expected 2 fields, found 1 | help: use `_` to explicitly ignore each field | diff --git a/src/test/ui/pattern/pat-tuple-field-count-cross.rs b/src/test/ui/pattern/pat-tuple-field-count-cross.rs new file mode 100644 index 0000000000000..b63da4e154f73 --- /dev/null +++ b/src/test/ui/pattern/pat-tuple-field-count-cross.rs @@ -0,0 +1,57 @@ +// aux-build:declarations-for-tuple-field-count-errors.rs + +extern crate declarations_for_tuple_field_count_errors; + +use declarations_for_tuple_field_count_errors::*; + +fn main() { + match Z0 { + Z0() => {} //~ ERROR expected tuple struct or tuple variant, found unit struct `Z0` + Z0(x) => {} //~ ERROR expected tuple struct or tuple variant, found unit struct `Z0` + } + match Z1() { + Z1 => {} //~ ERROR match bindings cannot shadow tuple structs + Z1(x) => {} //~ ERROR this pattern has 1 field, but the corresponding tuple struct has 0 fields + } + + match S(1, 2, 3) { + S() => {} //~ ERROR this pattern has 0 fields, but the corresponding tuple struct has 3 fields + S(1) => {} //~ ERROR this pattern has 1 field, but the corresponding tuple struct has 3 fields + S(xyz, abc) => {} //~ ERROR this pattern has 2 fields, but the corresponding tuple struct has 3 fields + S(1, 2, 3, 4) => {} //~ ERROR this pattern has 4 fields, but the corresponding tuple struct has 3 fields + } + match M(1, 2, 3) { + M() => {} //~ ERROR this pattern has 0 fields, but the corresponding tuple struct has 3 fields + M(1) => {} //~ ERROR this pattern has 1 field, but the corresponding tuple struct has 3 fields + M(xyz, abc) => {} //~ ERROR this pattern has 2 fields, but the corresponding tuple struct has 3 fields + M(1, 2, 3, 4) => {} //~ ERROR this pattern has 4 fields, but the corresponding tuple struct has 3 fields + } + + match E1::Z0 { + E1::Z0() => {} //~ ERROR expected tuple struct or tuple variant, found unit variant `E1::Z0` + E1::Z0(x) => {} //~ ERROR expected tuple struct or tuple variant, found unit variant `E1::Z0` + } + match E1::Z1() { + E1::Z1 => {} //~ ERROR expected unit struct, unit variant or constant, found tuple variant `E1::Z1` + E1::Z1(x) => {} //~ ERROR this pattern has 1 field, but the corresponding tuple variant has 0 fields + } + match E1::S(1, 2, 3) { + E1::S() => {} //~ ERROR this pattern has 0 fields, but the corresponding tuple variant has 3 fields + E1::S(1) => {} //~ ERROR this pattern has 1 field, but the corresponding tuple variant has 3 fields + E1::S(xyz, abc) => {} //~ ERROR this pattern has 2 fields, but the corresponding tuple variant has 3 fields + E1::S(1, 2, 3, 4) => {} //~ ERROR this pattern has 4 fields, but the corresponding tuple variant has 3 fields + } + + match E2::S(1, 2, 3) { + E2::S() => {} //~ ERROR this pattern has 0 fields, but the corresponding tuple variant has 3 fields + E2::S(1) => {} //~ ERROR this pattern has 1 field, but the corresponding tuple variant has 3 fields + E2::S(xyz, abc) => {} //~ ERROR this pattern has 2 fields, but the corresponding tuple variant has 3 fields + E2::S(1, 2, 3, 4) => {} //~ ERROR this pattern has 4 fields, but the corresponding tuple variant has 3 fields + } + match E2::M(1, 2, 3) { + E2::M() => {} //~ ERROR this pattern has 0 fields, but the corresponding tuple variant has 3 fields + E2::M(1) => {} //~ ERROR this pattern has 1 field, but the corresponding tuple variant has 3 fields + E2::M(xyz, abc) => {} //~ ERROR this pattern has 2 fields, but the corresponding tuple variant has 3 fields + E2::M(1, 2, 3, 4) => {} //~ ERROR this pattern has 4 fields, but the corresponding tuple variant has 3 fields + } +} diff --git a/src/test/ui/pattern/pat-tuple-field-count-cross.stderr b/src/test/ui/pattern/pat-tuple-field-count-cross.stderr new file mode 100644 index 0000000000000..cab8d4759df64 --- /dev/null +++ b/src/test/ui/pattern/pat-tuple-field-count-cross.stderr @@ -0,0 +1,536 @@ +error[E0530]: match bindings cannot shadow tuple structs + --> $DIR/pat-tuple-field-count-cross.rs:13:9 + | +LL | use declarations_for_tuple_field_count_errors::*; + | -------------------------------------------- the tuple struct `Z1` is imported here +... +LL | Z1 => {} + | ^^ cannot be named the same as a tuple struct + +error[E0532]: expected tuple struct or tuple variant, found unit struct `Z0` + --> $DIR/pat-tuple-field-count-cross.rs:9:9 + | +LL | Z0() => {} + | ^^^^ + | + ::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:1:1 + | +LL | pub struct Z0; + | -------------- `Z0` defined here +LL | pub struct Z1(); + | ---------------- similarly named tuple struct `Z1` defined here + | +help: use this syntax instead + | +LL | Z0 => {} + | ~~ +help: a tuple struct with a similar name exists + | +LL | Z1() => {} + | ~~ + +error[E0532]: expected tuple struct or tuple variant, found unit struct `Z0` + --> $DIR/pat-tuple-field-count-cross.rs:10:9 + | +LL | Z0(x) => {} + | ^^^^^ + | + ::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:1:1 + | +LL | pub struct Z0; + | -------------- `Z0` defined here +LL | pub struct Z1(); + | ---------------- similarly named tuple struct `Z1` defined here + | +help: use this syntax instead + | +LL | Z0 => {} + | ~~ +help: a tuple struct with a similar name exists + | +LL | Z1(x) => {} + | ~~ + +error[E0532]: expected tuple struct or tuple variant, found unit variant `E1::Z0` + --> $DIR/pat-tuple-field-count-cross.rs:31:9 + | +LL | E1::Z0() => {} + | ^^^^^^^^ + | + ::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:11:15 + | +LL | pub enum E1 { Z0, Z1(), S(u8, u8, u8) } + | -- ---- similarly named tuple variant `Z1` defined here + | | + | `E1::Z0` defined here + | +help: use this syntax instead + | +LL | E1::Z0 => {} + | ~~~~~~ +help: a tuple variant with a similar name exists + | +LL | E1::Z1() => {} + | ~~ + +error[E0532]: expected tuple struct or tuple variant, found unit variant `E1::Z0` + --> $DIR/pat-tuple-field-count-cross.rs:32:9 + | +LL | E1::Z0(x) => {} + | ^^^^^^^^^ + | + ::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:11:15 + | +LL | pub enum E1 { Z0, Z1(), S(u8, u8, u8) } + | -- ---- similarly named tuple variant `Z1` defined here + | | + | `E1::Z0` defined here + | +help: use this syntax instead + | +LL | E1::Z0 => {} + | ~~~~~~ +help: a tuple variant with a similar name exists + | +LL | E1::Z1(x) => {} + | ~~ + +error[E0532]: expected unit struct, unit variant or constant, found tuple variant `E1::Z1` + --> $DIR/pat-tuple-field-count-cross.rs:35:9 + | +LL | E1::Z1 => {} + | ^^^^^^ + | + ::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:11:19 + | +LL | pub enum E1 { Z0, Z1(), S(u8, u8, u8) } + | -- ---- `E1::Z1` defined here + | | + | similarly named unit variant `Z0` defined here + | +help: use the tuple variant pattern syntax instead + | +LL | E1::Z1(/* fields */) => {} + | ~~~~~~~~~~~~~~~~~~~~ +help: a unit variant with a similar name exists + | +LL | E1::Z0 => {} + | ~~ + +error[E0023]: this pattern has 1 field, but the corresponding tuple struct has 0 fields + --> $DIR/pat-tuple-field-count-cross.rs:14:12 + | +LL | Z1(x) => {} + | ^ expected 0 fields, found 1 + | + ::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:2:1 + | +LL | pub struct Z1(); + | ---------------- tuple struct has 0 fields + +error[E0023]: this pattern has 0 fields, but the corresponding tuple struct has 3 fields + --> $DIR/pat-tuple-field-count-cross.rs:18:9 + | +LL | S() => {} + | ^^^ expected 3 fields, found 0 + | + ::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:4:14 + | +LL | pub struct S(pub u8, pub u8, pub u8); + | ------ ------ ------ tuple struct has 3 fields + | +help: use `_` to explicitly ignore each field + | +LL | S(_, _, _) => {} + | +++++++ +help: use `..` to ignore all fields + | +LL | S(..) => {} + | ++ + +error[E0023]: this pattern has 1 field, but the corresponding tuple struct has 3 fields + --> $DIR/pat-tuple-field-count-cross.rs:19:11 + | +LL | S(1) => {} + | ^ expected 3 fields, found 1 + | + ::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:4:14 + | +LL | pub struct S(pub u8, pub u8, pub u8); + | ------ ------ ------ tuple struct has 3 fields + | +help: use `_` to explicitly ignore each field + | +LL | S(1, _, _) => {} + | ++++++ +help: use `..` to ignore the rest of the fields + | +LL | S(1, ..) => {} + | ++++ + +error[E0023]: this pattern has 2 fields, but the corresponding tuple struct has 3 fields + --> $DIR/pat-tuple-field-count-cross.rs:20:11 + | +LL | S(xyz, abc) => {} + | ^^^ ^^^ expected 3 fields, found 2 + | + ::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:4:14 + | +LL | pub struct S(pub u8, pub u8, pub u8); + | ------ ------ ------ tuple struct has 3 fields + | +help: use `_` to explicitly ignore each field + | +LL | S(xyz, abc, _) => {} + | +++ + +error[E0023]: this pattern has 4 fields, but the corresponding tuple struct has 3 fields + --> $DIR/pat-tuple-field-count-cross.rs:21:11 + | +LL | S(1, 2, 3, 4) => {} + | ^ ^ ^ ^ expected 3 fields, found 4 + | + ::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:4:14 + | +LL | pub struct S(pub u8, pub u8, pub u8); + | ------ ------ ------ tuple struct has 3 fields + +error[E0023]: this pattern has 0 fields, but the corresponding tuple struct has 3 fields + --> $DIR/pat-tuple-field-count-cross.rs:24:9 + | +LL | M() => {} + | ^^^ expected 3 fields, found 0 + | + ::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:5:1 + | +LL | / pub struct M( +LL | | pub u8, + | | ------ +LL | | pub u8, + | | ------ +LL | | pub u8, + | | ------ tuple struct has 3 fields +LL | | ); + | |__- tuple struct defined here + | +help: use `_` to explicitly ignore each field + | +LL | M(_, _, _) => {} + | +++++++ +help: use `..` to ignore all fields + | +LL | M(..) => {} + | ++ + +error[E0023]: this pattern has 1 field, but the corresponding tuple struct has 3 fields + --> $DIR/pat-tuple-field-count-cross.rs:25:11 + | +LL | M(1) => {} + | ^ expected 3 fields, found 1 + | + ::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:5:1 + | +LL | / pub struct M( +LL | | pub u8, + | | ------ +LL | | pub u8, + | | ------ +LL | | pub u8, + | | ------ tuple struct has 3 fields +LL | | ); + | |__- tuple struct defined here + | +help: use `_` to explicitly ignore each field + | +LL | M(1, _, _) => {} + | ++++++ +help: use `..` to ignore the rest of the fields + | +LL | M(1, ..) => {} + | ++++ + +error[E0023]: this pattern has 2 fields, but the corresponding tuple struct has 3 fields + --> $DIR/pat-tuple-field-count-cross.rs:26:11 + | +LL | M(xyz, abc) => {} + | ^^^ ^^^ expected 3 fields, found 2 + | + ::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:5:1 + | +LL | / pub struct M( +LL | | pub u8, + | | ------ +LL | | pub u8, + | | ------ +LL | | pub u8, + | | ------ tuple struct has 3 fields +LL | | ); + | |__- tuple struct defined here + | +help: use `_` to explicitly ignore each field + | +LL | M(xyz, abc, _) => {} + | +++ + +error[E0023]: this pattern has 4 fields, but the corresponding tuple struct has 3 fields + --> $DIR/pat-tuple-field-count-cross.rs:27:11 + | +LL | M(1, 2, 3, 4) => {} + | ^ ^ ^ ^ expected 3 fields, found 4 + | + ::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:5:1 + | +LL | / pub struct M( +LL | | pub u8, + | | ------ +LL | | pub u8, + | | ------ +LL | | pub u8, + | | ------ tuple struct has 3 fields +LL | | ); + | |__- tuple struct defined here + +error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 0 fields + --> $DIR/pat-tuple-field-count-cross.rs:36:16 + | +LL | E1::Z1(x) => {} + | ^ expected 0 fields, found 1 + | + ::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:11:19 + | +LL | pub enum E1 { Z0, Z1(), S(u8, u8, u8) } + | ---- tuple variant has 0 fields + +error[E0023]: this pattern has 0 fields, but the corresponding tuple variant has 3 fields + --> $DIR/pat-tuple-field-count-cross.rs:39:9 + | +LL | E1::S() => {} + | ^^^^^^^ expected 3 fields, found 0 + | + ::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:11:27 + | +LL | pub enum E1 { Z0, Z1(), S(u8, u8, u8) } + | -- -- -- tuple variant has 3 fields + | +help: use `_` to explicitly ignore each field + | +LL | E1::S(_, _, _) => {} + | +++++++ +help: use `..` to ignore all fields + | +LL | E1::S(..) => {} + | ++ + +error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 3 fields + --> $DIR/pat-tuple-field-count-cross.rs:40:15 + | +LL | E1::S(1) => {} + | ^ expected 3 fields, found 1 + | + ::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:11:27 + | +LL | pub enum E1 { Z0, Z1(), S(u8, u8, u8) } + | -- -- -- tuple variant has 3 fields + | +help: use `_` to explicitly ignore each field + | +LL | E1::S(1, _, _) => {} + | ++++++ +help: use `..` to ignore the rest of the fields + | +LL | E1::S(1, ..) => {} + | ++++ + +error[E0023]: this pattern has 2 fields, but the corresponding tuple variant has 3 fields + --> $DIR/pat-tuple-field-count-cross.rs:41:15 + | +LL | E1::S(xyz, abc) => {} + | ^^^ ^^^ expected 3 fields, found 2 + | + ::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:11:27 + | +LL | pub enum E1 { Z0, Z1(), S(u8, u8, u8) } + | -- -- -- tuple variant has 3 fields + | +help: use `_` to explicitly ignore each field + | +LL | E1::S(xyz, abc, _) => {} + | +++ + +error[E0023]: this pattern has 4 fields, but the corresponding tuple variant has 3 fields + --> $DIR/pat-tuple-field-count-cross.rs:42:15 + | +LL | E1::S(1, 2, 3, 4) => {} + | ^ ^ ^ ^ expected 3 fields, found 4 + | + ::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:11:27 + | +LL | pub enum E1 { Z0, Z1(), S(u8, u8, u8) } + | -- -- -- tuple variant has 3 fields + +error[E0023]: this pattern has 0 fields, but the corresponding tuple variant has 3 fields + --> $DIR/pat-tuple-field-count-cross.rs:46:9 + | +LL | E2::S() => {} + | ^^^^^^^ expected 3 fields, found 0 + | + ::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:14:7 + | +LL | S(u8, u8, u8), + | -- -- -- tuple variant has 3 fields + | +help: use `_` to explicitly ignore each field + | +LL | E2::S(_, _, _) => {} + | +++++++ +help: use `..` to ignore all fields + | +LL | E2::S(..) => {} + | ++ + +error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 3 fields + --> $DIR/pat-tuple-field-count-cross.rs:47:15 + | +LL | E2::S(1) => {} + | ^ expected 3 fields, found 1 + | + ::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:14:7 + | +LL | S(u8, u8, u8), + | -- -- -- tuple variant has 3 fields + | +help: use `_` to explicitly ignore each field + | +LL | E2::S(1, _, _) => {} + | ++++++ +help: use `..` to ignore the rest of the fields + | +LL | E2::S(1, ..) => {} + | ++++ + +error[E0023]: this pattern has 2 fields, but the corresponding tuple variant has 3 fields + --> $DIR/pat-tuple-field-count-cross.rs:48:15 + | +LL | E2::S(xyz, abc) => {} + | ^^^ ^^^ expected 3 fields, found 2 + | + ::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:14:7 + | +LL | S(u8, u8, u8), + | -- -- -- tuple variant has 3 fields + | +help: use `_` to explicitly ignore each field + | +LL | E2::S(xyz, abc, _) => {} + | +++ + +error[E0023]: this pattern has 4 fields, but the corresponding tuple variant has 3 fields + --> $DIR/pat-tuple-field-count-cross.rs:49:15 + | +LL | E2::S(1, 2, 3, 4) => {} + | ^ ^ ^ ^ expected 3 fields, found 4 + | + ::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:14:7 + | +LL | S(u8, u8, u8), + | -- -- -- tuple variant has 3 fields + +error[E0023]: this pattern has 0 fields, but the corresponding tuple variant has 3 fields + --> $DIR/pat-tuple-field-count-cross.rs:52:9 + | +LL | E2::M() => {} + | ^^^^^^^ expected 3 fields, found 0 + | + ::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:15:5 + | +LL | / M( +LL | | u8, + | | -- +LL | | u8, + | | -- +LL | | u8, + | | -- tuple variant has 3 fields +LL | | ), + | |_____- tuple variant defined here + | +help: use `_` to explicitly ignore each field + | +LL | E2::M(_, _, _) => {} + | +++++++ +help: use `..` to ignore all fields + | +LL | E2::M(..) => {} + | ++ + +error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 3 fields + --> $DIR/pat-tuple-field-count-cross.rs:53:15 + | +LL | E2::M(1) => {} + | ^ expected 3 fields, found 1 + | + ::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:15:5 + | +LL | / M( +LL | | u8, + | | -- +LL | | u8, + | | -- +LL | | u8, + | | -- tuple variant has 3 fields +LL | | ), + | |_____- tuple variant defined here + | +help: use `_` to explicitly ignore each field + | +LL | E2::M(1, _, _) => {} + | ++++++ +help: use `..` to ignore the rest of the fields + | +LL | E2::M(1, ..) => {} + | ++++ + +error[E0023]: this pattern has 2 fields, but the corresponding tuple variant has 3 fields + --> $DIR/pat-tuple-field-count-cross.rs:54:15 + | +LL | E2::M(xyz, abc) => {} + | ^^^ ^^^ expected 3 fields, found 2 + | + ::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:15:5 + | +LL | / M( +LL | | u8, + | | -- +LL | | u8, + | | -- +LL | | u8, + | | -- tuple variant has 3 fields +LL | | ), + | |_____- tuple variant defined here + | +help: use `_` to explicitly ignore each field + | +LL | E2::M(xyz, abc, _) => {} + | +++ + +error[E0023]: this pattern has 4 fields, but the corresponding tuple variant has 3 fields + --> $DIR/pat-tuple-field-count-cross.rs:55:15 + | +LL | E2::M(1, 2, 3, 4) => {} + | ^ ^ ^ ^ expected 3 fields, found 4 + | + ::: $DIR/auxiliary/declarations-for-tuple-field-count-errors.rs:15:5 + | +LL | / M( +LL | | u8, + | | -- +LL | | u8, + | | -- +LL | | u8, + | | -- tuple variant has 3 fields +LL | | ), + | |_____- tuple variant defined here + +error: aborting due to 28 previous errors + +Some errors have detailed explanations: E0023, E0530, E0532. +For more information about an error, try `rustc --explain E0023`. diff --git a/src/test/ui/pattern/pat-tuple-overfield.rs b/src/test/ui/pattern/pat-tuple-overfield.rs index 46a5e15ffa52c..c863c657514f3 100644 --- a/src/test/ui/pattern/pat-tuple-overfield.rs +++ b/src/test/ui/pattern/pat-tuple-overfield.rs @@ -1,4 +1,18 @@ struct S(u8, u8, u8); +struct M( + u8, + u8, + u8, + u8, + u8, +); + +struct Z0; +struct Z1(); +enum E1 { + Z0, + Z1(), +} fn main() { match (1, 2, 3) { @@ -13,4 +27,48 @@ fn main() { //~^ ERROR this pattern has 4 fields, but the corresponding tuple struct has 3 fields _ => {} } + match M(1, 2, 3, 4, 5) { + M(1, 2, 3, 4, 5, 6) => {} + //~^ ERROR this pattern has 6 fields, but the corresponding tuple struct has 5 fields + M(1, + 2, + 3, + 4, + 5, + 6) => {} + //~^ ERROR this pattern has 6 fields, but the corresponding tuple struct has 5 fields + M( + 1, + 2, + 3, + 4, + 5, + 6, + ) => {} + //~^^ ERROR this pattern has 6 fields, but the corresponding tuple struct has 5 fields + } + match Z0 { + Z0 => {} + Z0() => {} //~ ERROR expected tuple struct or tuple variant, found unit struct `Z0` + Z0(_) => {} //~ ERROR expected tuple struct or tuple variant, found unit struct `Z0` + Z0(_, _) => {} //~ ERROR expected tuple struct or tuple variant, found unit struct `Z0` + } + match Z1() { + Z1 => {} //~ ERROR match bindings cannot shadow tuple structs + Z1() => {} + Z1(_) => {} //~ ERROR this pattern has 1 field, but the corresponding tuple struct has 0 fields + Z1(_, _) => {} //~ ERROR this pattern has 2 fields, but the corresponding tuple struct has 0 fields + } + match E1::Z0 { + E1::Z0 => {} + E1::Z0() => {} //~ ERROR expected tuple struct or tuple variant, found unit variant `E1::Z0` + E1::Z0(_) => {} //~ ERROR expected tuple struct or tuple variant, found unit variant `E1::Z0` + E1::Z0(_, _) => {} //~ ERROR expected tuple struct or tuple variant, found unit variant `E1::Z0` + } + match E1::Z1() { + E1::Z1 => {} //~ ERROR expected unit struct, unit variant or constant, found tuple variant `E1::Z1` + E1::Z1() => {} + E1::Z1(_) => {} //~ ERROR this pattern has 1 field, but the corresponding tuple variant has 0 fields + E1::Z1(_, _) => {} //~ ERROR this pattern has 2 fields, but the corresponding tuple variant has 0 fields + } } diff --git a/src/test/ui/pattern/pat-tuple-overfield.stderr b/src/test/ui/pattern/pat-tuple-overfield.stderr index 45b6fd1b4d445..1c44f7e5f6f1f 100644 --- a/src/test/ui/pattern/pat-tuple-overfield.stderr +++ b/src/test/ui/pattern/pat-tuple-overfield.stderr @@ -1,5 +1,154 @@ +error[E0530]: match bindings cannot shadow tuple structs + --> $DIR/pat-tuple-overfield.rs:57:9 + | +LL | struct Z1(); + | ------------ the tuple struct `Z1` is defined here +... +LL | Z1 => {} + | ^^ cannot be named the same as a tuple struct + +error[E0532]: expected tuple struct or tuple variant, found unit struct `Z0` + --> $DIR/pat-tuple-overfield.rs:52:9 + | +LL | struct Z0; + | ---------- `Z0` defined here +LL | struct Z1(); + | ------------ similarly named tuple struct `Z1` defined here +... +LL | Z0() => {} + | ^^^^ + | +help: use this syntax instead + | +LL | Z0 => {} + | ~~ +help: a tuple struct with a similar name exists + | +LL | Z1() => {} + | ~~ + +error[E0532]: expected tuple struct or tuple variant, found unit struct `Z0` + --> $DIR/pat-tuple-overfield.rs:53:9 + | +LL | struct Z0; + | ---------- `Z0` defined here +LL | struct Z1(); + | ------------ similarly named tuple struct `Z1` defined here +... +LL | Z0(_) => {} + | ^^^^^ + | +help: use this syntax instead + | +LL | Z0 => {} + | ~~ +help: a tuple struct with a similar name exists + | +LL | Z1(_) => {} + | ~~ + +error[E0532]: expected tuple struct or tuple variant, found unit struct `Z0` + --> $DIR/pat-tuple-overfield.rs:54:9 + | +LL | struct Z0; + | ---------- `Z0` defined here +LL | struct Z1(); + | ------------ similarly named tuple struct `Z1` defined here +... +LL | Z0(_, _) => {} + | ^^^^^^^^ + | +help: use this syntax instead + | +LL | Z0 => {} + | ~~ +help: a tuple struct with a similar name exists + | +LL | Z1(_, _) => {} + | ~~ + +error[E0532]: expected tuple struct or tuple variant, found unit variant `E1::Z0` + --> $DIR/pat-tuple-overfield.rs:64:9 + | +LL | Z0, + | -- `E1::Z0` defined here +LL | Z1(), + | ---- similarly named tuple variant `Z1` defined here +... +LL | E1::Z0() => {} + | ^^^^^^^^ + | +help: use this syntax instead + | +LL | E1::Z0 => {} + | ~~~~~~ +help: a tuple variant with a similar name exists + | +LL | E1::Z1() => {} + | ~~ + +error[E0532]: expected tuple struct or tuple variant, found unit variant `E1::Z0` + --> $DIR/pat-tuple-overfield.rs:65:9 + | +LL | Z0, + | -- `E1::Z0` defined here +LL | Z1(), + | ---- similarly named tuple variant `Z1` defined here +... +LL | E1::Z0(_) => {} + | ^^^^^^^^^ + | +help: use this syntax instead + | +LL | E1::Z0 => {} + | ~~~~~~ +help: a tuple variant with a similar name exists + | +LL | E1::Z1(_) => {} + | ~~ + +error[E0532]: expected tuple struct or tuple variant, found unit variant `E1::Z0` + --> $DIR/pat-tuple-overfield.rs:66:9 + | +LL | Z0, + | -- `E1::Z0` defined here +LL | Z1(), + | ---- similarly named tuple variant `Z1` defined here +... +LL | E1::Z0(_, _) => {} + | ^^^^^^^^^^^^ + | +help: use this syntax instead + | +LL | E1::Z0 => {} + | ~~~~~~ +help: a tuple variant with a similar name exists + | +LL | E1::Z1(_, _) => {} + | ~~ + +error[E0532]: expected unit struct, unit variant or constant, found tuple variant `E1::Z1` + --> $DIR/pat-tuple-overfield.rs:69:9 + | +LL | Z0, + | -- similarly named unit variant `Z0` defined here +LL | Z1(), + | ---- `E1::Z1` defined here +... +LL | E1::Z1 => {} + | ^^^^^^ + | +help: use the tuple variant pattern syntax instead + | +LL | E1::Z1() => {} + | ~~~~~~~~ +help: a unit variant with a similar name exists + | +LL | E1::Z0 => {} + | ~~ + error[E0308]: mismatched types - --> $DIR/pat-tuple-overfield.rs:5:9 + --> $DIR/pat-tuple-overfield.rs:19:9 | LL | match (1, 2, 3) { | --------- this expression has type `({integer}, {integer}, {integer})` @@ -10,7 +159,7 @@ LL | (1, 2, 3, 4) => {} found tuple `(_, _, _, _)` error[E0308]: mismatched types - --> $DIR/pat-tuple-overfield.rs:6:9 + --> $DIR/pat-tuple-overfield.rs:20:9 | LL | match (1, 2, 3) { | --------- this expression has type `({integer}, {integer}, {integer})` @@ -22,24 +171,139 @@ LL | (1, 2, .., 3, 4) => {} found tuple `(_, _, _, _)` error[E0023]: this pattern has 4 fields, but the corresponding tuple struct has 3 fields - --> $DIR/pat-tuple-overfield.rs:10:9 + --> $DIR/pat-tuple-overfield.rs:24:11 | LL | struct S(u8, u8, u8); - | --------------------- tuple struct defined here + | -- -- -- tuple struct has 3 fields ... LL | S(1, 2, 3, 4) => {} - | ^^^^^^^^^^^^^ expected 3 fields, found 4 + | ^ ^ ^ ^ expected 3 fields, found 4 error[E0023]: this pattern has 4 fields, but the corresponding tuple struct has 3 fields - --> $DIR/pat-tuple-overfield.rs:12:9 + --> $DIR/pat-tuple-overfield.rs:26:11 | LL | struct S(u8, u8, u8); - | --------------------- tuple struct defined here + | -- -- -- tuple struct has 3 fields ... LL | S(1, 2, .., 3, 4) => {} - | ^^^^^^^^^^^^^^^^^ expected 3 fields, found 4 + | ^ ^ ^ ^ expected 3 fields, found 4 + +error[E0023]: this pattern has 6 fields, but the corresponding tuple struct has 5 fields + --> $DIR/pat-tuple-overfield.rs:31:11 + | +LL | struct M( + | - tuple struct defined here +LL | u8, + | -- +LL | u8, + | -- +LL | u8, + | -- +LL | u8, + | -- +LL | u8, + | -- tuple struct has 5 fields +... +LL | M(1, 2, 3, 4, 5, 6) => {} + | ^ ^ ^ ^ ^ ^ expected 5 fields, found 6 + +error[E0023]: this pattern has 6 fields, but the corresponding tuple struct has 5 fields + --> $DIR/pat-tuple-overfield.rs:33:11 + | +LL | struct M( + | - tuple struct defined here +LL | u8, + | -- +LL | u8, + | -- +LL | u8, + | -- +LL | u8, + | -- +LL | u8, + | -- tuple struct has 5 fields +... +LL | M(1, + | - ^ +LL | 2, + | ^ +LL | 3, + | ^ +LL | 4, + | ^ +LL | 5, + | ^ +LL | 6) => {} + | ^ expected 5 fields, found 6 + +error[E0023]: this pattern has 6 fields, but the corresponding tuple struct has 5 fields + --> $DIR/pat-tuple-overfield.rs:41:13 + | +LL | struct M( + | - tuple struct defined here +LL | u8, + | -- +LL | u8, + | -- +LL | u8, + | -- +LL | u8, + | -- +LL | u8, + | -- tuple struct has 5 fields +... +LL | M( + | - +LL | 1, + | ^ +LL | 2, + | ^ +LL | 3, + | ^ +LL | 4, + | ^ +LL | 5, + | ^ +LL | 6, + | ^ expected 5 fields, found 6 + +error[E0023]: this pattern has 1 field, but the corresponding tuple struct has 0 fields + --> $DIR/pat-tuple-overfield.rs:59:12 + | +LL | struct Z1(); + | ------------ tuple struct has 0 fields +... +LL | Z1(_) => {} + | ^ expected 0 fields, found 1 + +error[E0023]: this pattern has 2 fields, but the corresponding tuple struct has 0 fields + --> $DIR/pat-tuple-overfield.rs:60:12 + | +LL | struct Z1(); + | ------------ tuple struct has 0 fields +... +LL | Z1(_, _) => {} + | ^ ^ expected 0 fields, found 2 + +error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 0 fields + --> $DIR/pat-tuple-overfield.rs:71:16 + | +LL | Z1(), + | ---- tuple variant has 0 fields +... +LL | E1::Z1(_) => {} + | ^ expected 0 fields, found 1 + +error[E0023]: this pattern has 2 fields, but the corresponding tuple variant has 0 fields + --> $DIR/pat-tuple-overfield.rs:72:16 + | +LL | Z1(), + | ---- tuple variant has 0 fields +... +LL | E1::Z1(_, _) => {} + | ^ ^ expected 0 fields, found 2 -error: aborting due to 4 previous errors +error: aborting due to 19 previous errors -Some errors have detailed explanations: E0023, E0308. +Some errors have detailed explanations: E0023, E0308, E0530, E0532. For more information about an error, try `rustc --explain E0023`. diff --git a/src/test/ui/pattern/pat-tuple-underfield.rs b/src/test/ui/pattern/pat-tuple-underfield.rs index ed852a47bb4ee..dac60e3fab2c0 100644 --- a/src/test/ui/pattern/pat-tuple-underfield.rs +++ b/src/test/ui/pattern/pat-tuple-underfield.rs @@ -21,6 +21,12 @@ fn main() { //~^ ERROR this pattern has 0 fields, but the corresponding tuple struct has 2 fields //~| HELP use `_` to explicitly ignore each field //~| HELP use `..` to ignore all fields + + // Test non-standard formatting + S () => {} + //~^ ERROR this pattern has 0 fields, but the corresponding tuple struct has 2 fields + //~| HELP use `_` to explicitly ignore each field + //~| HELP use `..` to ignore all fields } match E::S(0, 1.0) { @@ -39,6 +45,12 @@ fn main() { //~^ ERROR this pattern has 0 fields, but the corresponding tuple variant has 2 fields //~| HELP use `_` to explicitly ignore each field //~| HELP use `..` to ignore all fields + + // Test non-standard formatting + E::S () => {} + //~^ ERROR this pattern has 0 fields, but the corresponding tuple variant has 2 fields + //~| HELP use `_` to explicitly ignore each field + //~| HELP use `..` to ignore all fields } match E::S(0, 1.0) { E::S => {} diff --git a/src/test/ui/pattern/pat-tuple-underfield.stderr b/src/test/ui/pattern/pat-tuple-underfield.stderr index 2fbcb6d67ba36..e75f9b38da566 100644 --- a/src/test/ui/pattern/pat-tuple-underfield.stderr +++ b/src/test/ui/pattern/pat-tuple-underfield.stderr @@ -1,5 +1,5 @@ error[E0532]: expected unit struct, unit variant or constant, found tuple variant `E::S` - --> $DIR/pat-tuple-underfield.rs:44:9 + --> $DIR/pat-tuple-underfield.rs:56:9 | LL | S(i32, f32), | ----------- `E::S` defined here @@ -8,13 +8,13 @@ LL | E::S => {} | ^^^^ help: use the tuple variant pattern syntax instead: `E::S(_, _)` error[E0023]: this pattern has 1 field, but the corresponding tuple struct has 2 fields - --> $DIR/pat-tuple-underfield.rs:9:9 + --> $DIR/pat-tuple-underfield.rs:9:11 | LL | struct S(i32, f32); - | ------------------- tuple struct defined here + | --- --- tuple struct has 2 fields ... LL | S(x) => {} - | ^^^^ expected 2 fields, found 1 + | ^ expected 2 fields, found 1 | help: use `_` to explicitly ignore each field | @@ -22,13 +22,13 @@ LL | S(x, _) => {} | +++ error[E0023]: this pattern has 1 field, but the corresponding tuple struct has 2 fields - --> $DIR/pat-tuple-underfield.rs:14:9 + --> $DIR/pat-tuple-underfield.rs:14:11 | LL | struct S(i32, f32); - | ------------------- tuple struct defined here + | --- --- tuple struct has 2 fields ... LL | S(_) => {} - | ^^^^ expected 2 fields, found 1 + | ^ expected 2 fields, found 1 | help: use `_` to explicitly ignore each field | @@ -43,7 +43,7 @@ error[E0023]: this pattern has 0 fields, but the corresponding tuple struct has --> $DIR/pat-tuple-underfield.rs:20:9 | LL | struct S(i32, f32); - | ------------------- tuple struct defined here + | --- --- tuple struct has 2 fields ... LL | S() => {} | ^^^ expected 2 fields, found 0 @@ -57,14 +57,32 @@ help: use `..` to ignore all fields LL | S(..) => {} | ++ +error[E0023]: this pattern has 0 fields, but the corresponding tuple struct has 2 fields + --> $DIR/pat-tuple-underfield.rs:26:9 + | +LL | struct S(i32, f32); + | --- --- tuple struct has 2 fields +... +LL | S () => {} + | ^^^^ expected 2 fields, found 0 + | +help: use `_` to explicitly ignore each field + | +LL | S (_, _) => {} + | ++++ +help: use `..` to ignore all fields + | +LL | S (..) => {} + | ++ + error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 2 fields - --> $DIR/pat-tuple-underfield.rs:27:9 + --> $DIR/pat-tuple-underfield.rs:33:14 | LL | S(i32, f32), - | ----------- tuple variant defined here + | --- --- tuple variant has 2 fields ... LL | E::S(x) => {} - | ^^^^^^^ expected 2 fields, found 1 + | ^ expected 2 fields, found 1 | help: use `_` to explicitly ignore each field | @@ -72,13 +90,13 @@ LL | E::S(x, _) => {} | +++ error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 2 fields - --> $DIR/pat-tuple-underfield.rs:32:9 + --> $DIR/pat-tuple-underfield.rs:38:14 | LL | S(i32, f32), - | ----------- tuple variant defined here + | --- --- tuple variant has 2 fields ... LL | E::S(_) => {} - | ^^^^^^^ expected 2 fields, found 1 + | ^ expected 2 fields, found 1 | help: use `_` to explicitly ignore each field | @@ -90,10 +108,10 @@ LL | E::S(..) => {} | ~~ error[E0023]: this pattern has 0 fields, but the corresponding tuple variant has 2 fields - --> $DIR/pat-tuple-underfield.rs:38:9 + --> $DIR/pat-tuple-underfield.rs:44:9 | LL | S(i32, f32), - | ----------- tuple variant defined here + | --- --- tuple variant has 2 fields ... LL | E::S() => {} | ^^^^^^ expected 2 fields, found 0 @@ -107,14 +125,32 @@ help: use `..` to ignore all fields LL | E::S(..) => {} | ++ -error[E0023]: this pattern has 2 fields, but the corresponding tuple struct has 4 fields +error[E0023]: this pattern has 0 fields, but the corresponding tuple variant has 2 fields --> $DIR/pat-tuple-underfield.rs:50:9 | +LL | S(i32, f32), + | --- --- tuple variant has 2 fields +... +LL | E::S () => {} + | ^^^^^^^ expected 2 fields, found 0 + | +help: use `_` to explicitly ignore each field + | +LL | E::S (_, _) => {} + | ++++ +help: use `..` to ignore all fields + | +LL | E::S (..) => {} + | ++ + +error[E0023]: this pattern has 2 fields, but the corresponding tuple struct has 4 fields + --> $DIR/pat-tuple-underfield.rs:62:19 + | LL | struct Point4(i32, i32, i32, i32); - | ---------------------------------- tuple struct defined here + | --- --- --- --- tuple struct has 4 fields ... LL | Point4( a , _ ) => {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ expected 4 fields, found 2 + | ^ ^ expected 4 fields, found 2 | help: use `_` to explicitly ignore each field | @@ -125,7 +161,7 @@ help: use `..` to ignore the rest of the fields LL | Point4( a, ..) => {} | ~~~~ -error: aborting due to 8 previous errors +error: aborting due to 10 previous errors Some errors have detailed explanations: E0023, E0532. For more information about an error, try `rustc --explain E0023`. diff --git a/src/test/ui/pattern/pattern-error-continue.stderr b/src/test/ui/pattern/pattern-error-continue.stderr index 3f28ffed29162..c800afdae2afb 100644 --- a/src/test/ui/pattern/pattern-error-continue.stderr +++ b/src/test/ui/pattern/pattern-error-continue.stderr @@ -26,13 +26,13 @@ LL | A::B(_) => (), | ~ error[E0023]: this pattern has 3 fields, but the corresponding tuple variant has 2 fields - --> $DIR/pattern-error-continue.rs:17:9 + --> $DIR/pattern-error-continue.rs:17:14 | LL | B(isize, isize), - | --------------- tuple variant defined here + | ----- ----- tuple variant has 2 fields ... LL | A::B(_, _, _) => (), - | ^^^^^^^^^^^^^ expected 2 fields, found 3 + | ^ ^ ^ expected 2 fields, found 3 error[E0308]: mismatched types --> $DIR/pattern-error-continue.rs:22:9 diff --git a/src/test/ui/type-alias-enum-variants/enum-variant-priority-higher-than-other-inherent.stderr b/src/test/ui/type-alias-enum-variants/enum-variant-priority-higher-than-other-inherent.stderr index 1d520613a288f..37543c137f66f 100644 --- a/src/test/ui/type-alias-enum-variants/enum-variant-priority-higher-than-other-inherent.stderr +++ b/src/test/ui/type-alias-enum-variants/enum-variant-priority-higher-than-other-inherent.stderr @@ -5,6 +5,12 @@ LL | ::V(); | ^^^^^^-- supplied 0 arguments | | | expected 1 argument + | +note: tuple variant defined here + --> $DIR/enum-variant-priority-higher-than-other-inherent.rs:5:5 + | +LL | V(u8) + | ^ error[E0308]: mismatched types --> $DIR/enum-variant-priority-higher-than-other-inherent.rs:22:17 diff --git a/src/test/ui/type-alias-impl-trait/field-types.rs b/src/test/ui/type-alias-impl-trait/field-types.rs new file mode 100644 index 0000000000000..91494a82d0fb2 --- /dev/null +++ b/src/test/ui/type-alias-impl-trait/field-types.rs @@ -0,0 +1,20 @@ +#![feature(type_alias_impl_trait)] +#![allow(dead_code)] + +// FIXME This should compile, but it currently doesn't + +use std::fmt::Debug; + +type Foo = impl Debug; +//~^ ERROR: could not find defining uses + +struct Bar { + foo: Foo, +} + +fn bar() -> Bar { + Bar { foo: "foo" } + //~^ ERROR: mismatched types [E0308] +} + +fn main() {} diff --git a/src/test/ui/type-alias-impl-trait/field-types.stderr b/src/test/ui/type-alias-impl-trait/field-types.stderr new file mode 100644 index 0000000000000..18c2abbdf3721 --- /dev/null +++ b/src/test/ui/type-alias-impl-trait/field-types.stderr @@ -0,0 +1,21 @@ +error[E0308]: mismatched types + --> $DIR/field-types.rs:16:16 + | +LL | type Foo = impl Debug; + | ---------- the expected opaque type +... +LL | Bar { foo: "foo" } + | ^^^^^ expected opaque type, found `&str` + | + = note: expected opaque type `impl Debug` + found reference `&'static str` + +error: could not find defining uses + --> $DIR/field-types.rs:8:12 + | +LL | type Foo = impl Debug; + | ^^^^^^^^^^ + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0308`. diff --git a/src/test/ui/type-alias-impl-trait/static-const-types.rs b/src/test/ui/type-alias-impl-trait/static-const-types.rs new file mode 100644 index 0000000000000..f630d27833503 --- /dev/null +++ b/src/test/ui/type-alias-impl-trait/static-const-types.rs @@ -0,0 +1,16 @@ +#![feature(type_alias_impl_trait)] +#![allow(dead_code)] + +// FIXME: This should compile, but it currently doesn't + +use std::fmt::Debug; + +type Foo = impl Debug; +//~^ ERROR: could not find defining uses + +static FOO1: Foo = 22_u32; +//~^ ERROR: mismatched types [E0308] +const FOO2: Foo = 22_u32; +//~^ ERROR: mismatched types [E0308] + +fn main() {} diff --git a/src/test/ui/type-alias-impl-trait/static-const-types.stderr b/src/test/ui/type-alias-impl-trait/static-const-types.stderr new file mode 100644 index 0000000000000..72083d014fe3a --- /dev/null +++ b/src/test/ui/type-alias-impl-trait/static-const-types.stderr @@ -0,0 +1,33 @@ +error[E0308]: mismatched types + --> $DIR/static-const-types.rs:11:20 + | +LL | type Foo = impl Debug; + | ---------- the expected opaque type +... +LL | static FOO1: Foo = 22_u32; + | ^^^^^^ expected opaque type, found `u32` + | + = note: expected opaque type `impl Debug` + found type `u32` + +error[E0308]: mismatched types + --> $DIR/static-const-types.rs:13:19 + | +LL | type Foo = impl Debug; + | ---------- the expected opaque type +... +LL | const FOO2: Foo = 22_u32; + | ^^^^^^ expected opaque type, found `u32` + | + = note: expected opaque type `impl Debug` + found type `u32` + +error: could not find defining uses + --> $DIR/static-const-types.rs:8:12 + | +LL | type Foo = impl Debug; + | ^^^^^^^^^^ + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0308`. diff --git a/src/test/ui/type-alias-impl-trait/type_of_a_let2.rs b/src/test/ui/type-alias-impl-trait/type_of_a_let2.rs deleted file mode 100644 index 33d3f164ce15e..0000000000000 --- a/src/test/ui/type-alias-impl-trait/type_of_a_let2.rs +++ /dev/null @@ -1,25 +0,0 @@ -#![feature(type_alias_impl_trait)] -#![allow(dead_code)] - -// FIXME This should be under a feature flag - -use std::fmt::Debug; - -fn foo1() -> u32 { - let x: impl Debug = 22_u32; - //~^ ERROR: `impl Trait` not allowed outside of function and method return types [E0562] - x // ERROR: we only know x: Debug, we don't know x = u32 -} - -fn foo2() -> u32 { - let x: impl Debug = 22_u32; - //~^ ERROR: `impl Trait` not allowed outside of function and method return types [E0562] - let y: impl Debug = x; - //~^ ERROR: `impl Trait` not allowed outside of function and method return types [E0562] - same_type((x, y)); // ERROR - x -} - -fn same_type(x: (T, T)) {} - -fn main() {} diff --git a/src/test/ui/type-alias-impl-trait/type_of_a_let2.stderr b/src/test/ui/type-alias-impl-trait/type_of_a_let2.stderr deleted file mode 100644 index 7a1825a8e2d9a..0000000000000 --- a/src/test/ui/type-alias-impl-trait/type_of_a_let2.stderr +++ /dev/null @@ -1,21 +0,0 @@ -error[E0562]: `impl Trait` not allowed outside of function and method return types - --> $DIR/type_of_a_let2.rs:9:12 - | -LL | let x: impl Debug = 22_u32; - | ^^^^^^^^^^ - -error[E0562]: `impl Trait` not allowed outside of function and method return types - --> $DIR/type_of_a_let2.rs:15:12 - | -LL | let x: impl Debug = 22_u32; - | ^^^^^^^^^^ - -error[E0562]: `impl Trait` not allowed outside of function and method return types - --> $DIR/type_of_a_let2.rs:17:12 - | -LL | let y: impl Debug = x; - | ^^^^^^^^^^ - -error: aborting due to 3 previous errors - -For more information about this error, try `rustc --explain E0562`. diff --git a/src/test/ui/typeck/struct-enum-wrong-args.stderr b/src/test/ui/typeck/struct-enum-wrong-args.stderr index d77ef73028b0c..6e99feed33f9c 100644 --- a/src/test/ui/typeck/struct-enum-wrong-args.stderr +++ b/src/test/ui/typeck/struct-enum-wrong-args.stderr @@ -29,6 +29,12 @@ LL | let _ = Wrapper(); | ^^^^^^^-- supplied 0 arguments | | | expected 1 argument + | +note: tuple struct defined here + --> $DIR/struct-enum-wrong-args.rs:2:8 + | +LL | struct Wrapper(i32); + | ^^^^^^^ error[E0061]: this struct takes 1 argument but 2 arguments were supplied --> $DIR/struct-enum-wrong-args.rs:10:13 @@ -37,6 +43,12 @@ LL | let _ = Wrapper(5, 2); | ^^^^^^^ - - supplied 2 arguments | | | expected 1 argument + | +note: tuple struct defined here + --> $DIR/struct-enum-wrong-args.rs:2:8 + | +LL | struct Wrapper(i32); + | ^^^^^^^ error[E0061]: this struct takes 2 arguments but 0 arguments were supplied --> $DIR/struct-enum-wrong-args.rs:11:13 @@ -45,6 +57,12 @@ LL | let _ = DoubleWrapper(); | ^^^^^^^^^^^^^-- supplied 0 arguments | | | expected 2 arguments + | +note: tuple struct defined here + --> $DIR/struct-enum-wrong-args.rs:3:8 + | +LL | struct DoubleWrapper(i32, i32); + | ^^^^^^^^^^^^^ error[E0061]: this struct takes 2 arguments but 1 argument was supplied --> $DIR/struct-enum-wrong-args.rs:12:13 @@ -53,6 +71,12 @@ LL | let _ = DoubleWrapper(5); | ^^^^^^^^^^^^^ - supplied 1 argument | | | expected 2 arguments + | +note: tuple struct defined here + --> $DIR/struct-enum-wrong-args.rs:3:8 + | +LL | struct DoubleWrapper(i32, i32); + | ^^^^^^^^^^^^^ error[E0061]: this struct takes 2 arguments but 3 arguments were supplied --> $DIR/struct-enum-wrong-args.rs:13:13 @@ -61,6 +85,12 @@ LL | let _ = DoubleWrapper(5, 2, 7); | ^^^^^^^^^^^^^ - - - supplied 3 arguments | | | expected 2 arguments + | +note: tuple struct defined here + --> $DIR/struct-enum-wrong-args.rs:3:8 + | +LL | struct DoubleWrapper(i32, i32); + | ^^^^^^^^^^^^^ error: aborting due to 8 previous errors diff --git a/src/tools/linkchecker/main.rs b/src/tools/linkchecker/main.rs index 8cbe0a0c2e8e1..94ebbb33e8d8f 100644 --- a/src/tools/linkchecker/main.rs +++ b/src/tools/linkchecker/main.rs @@ -30,6 +30,7 @@ use regex::Regex; // If at all possible you should use intra-doc links to avoid linkcheck issues. These // are cases where that does not work // [(generated_documentation_page, &[broken_links])] +#[rustfmt::skip] const LINKCHECK_EXCEPTIONS: &[(&str, &[&str])] = &[ // These try to link to std::collections, but are defined in alloc // https://github.com/rust-lang/rust/issues/74481 @@ -37,6 +38,19 @@ const LINKCHECK_EXCEPTIONS: &[(&str, &[&str])] = &[ ("std/collections/btree_set/struct.BTreeSet.html", &["#insert-and-complex-keys"]), ("alloc/collections/btree_map/struct.BTreeMap.html", &["#insert-and-complex-keys"]), ("alloc/collections/btree_set/struct.BTreeSet.html", &["#insert-and-complex-keys"]), + + // These try to link to various things in std, but are defined in core. + // The docs in std::primitive use proper intra-doc links, so these seem fine to special-case. + // Most these are broken because liballoc uses `#[lang_item]` magic to define things on + // primitives that aren't available in core. + ("alloc/slice/trait.Join.html", &["#method.join"]), + ("alloc/slice/trait.Concat.html", &["#method.concat"]), + ("alloc/slice/index.html", &["#method.concat", "#method.join"]), + ("alloc/vec/struct.Vec.html", &["#method.sort_by_key", "#method.sort_by_cached_key"]), + ("core/primitive.str.html", &["#method.to_ascii_uppercase", "#method.to_ascii_lowercase"]), + ("core/primitive.slice.html", &["#method.to_ascii_uppercase", "#method.to_ascii_lowercase", + "core/slice::sort_by_key", "core\\slice::sort_by_key", + "#method.sort_by_cached_key"]), ]; #[rustfmt::skip] @@ -376,6 +390,10 @@ impl Checker { /// Load a file from disk, or from the cache if available. fn load_file(&mut self, file: &Path, report: &mut Report) -> (String, &FileEntry) { + // https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499- + #[cfg(windows)] + const ERROR_INVALID_NAME: i32 = 123; + let pretty_path = file.strip_prefix(&self.root).unwrap_or(&file).to_str().unwrap().to_string(); @@ -392,6 +410,14 @@ impl Checker { } Err(e) if e.kind() == ErrorKind::NotFound => FileEntry::Missing, Err(e) => { + // If a broken intra-doc link contains `::`, on windows, it will cause `ERROR_INVALID_NAME` rather than `NotFound`. + // Explicitly check for that so that the broken link can be allowed in `LINKCHECK_EXCEPTIONS`. + #[cfg(windows)] + if e.raw_os_error() == Some(ERROR_INVALID_NAME) + && file.as_os_str().to_str().map_or(false, |s| s.contains("::")) + { + return FileEntry::Missing; + } panic!("unexpected read error for {}: {}", file.display(), e); } }); diff --git a/src/tools/tidy/src/lib.rs b/src/tools/tidy/src/lib.rs index a1c41eb99810e..35809e599266c 100644 --- a/src/tools/tidy/src/lib.rs +++ b/src/tools/tidy/src/lib.rs @@ -46,6 +46,7 @@ pub mod errors; pub mod extdeps; pub mod features; pub mod pal; +pub mod primitive_docs; pub mod style; pub mod target_specific_tests; pub mod ui_tests; diff --git a/src/tools/tidy/src/main.rs b/src/tools/tidy/src/main.rs index 440c352ea5320..d555f7c8e34ff 100644 --- a/src/tools/tidy/src/main.rs +++ b/src/tools/tidy/src/main.rs @@ -71,6 +71,7 @@ fn main() { // Checks that only make sense for the std libs. check!(pal, &library_path); + check!(primitive_docs, &library_path); // Checks that need to be done for both the compiler and std libraries. check!(unit_tests, &src_path); diff --git a/src/tools/tidy/src/primitive_docs.rs b/src/tools/tidy/src/primitive_docs.rs new file mode 100644 index 0000000000000..8476650d9b5f7 --- /dev/null +++ b/src/tools/tidy/src/primitive_docs.rs @@ -0,0 +1,17 @@ +//! Tidy check to make sure `library/{std,core}/src/primitive_docs.rs` are the same file. These are +//! different files so that relative links work properly without having to have `CARGO_PKG_NAME` +//! set, but conceptually they should always be the same. + +use std::path::Path; + +pub fn check(library_path: &Path, bad: &mut bool) { + let std_name = "std/src/primitive_docs.rs"; + let core_name = "core/src/primitive_docs.rs"; + let std_contents = std::fs::read_to_string(library_path.join(std_name)) + .unwrap_or_else(|e| panic!("failed to read library/{}: {}", std_name, e)); + let core_contents = std::fs::read_to_string(library_path.join(core_name)) + .unwrap_or_else(|e| panic!("failed to read library/{}: {}", core_name, e)); + if std_contents != core_contents { + tidy_error!(bad, "library/{} and library/{} have different contents", core_name, std_name); + } +}