diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 5258f179d95d2..74cc1ec17e6f0 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -30,8 +30,9 @@ use rustc_data_structures::stack::ensure_sufficient_stack; use rustc_data_structures::tagged_ptr::Tag; use rustc_macros::{Decodable, Encodable, HashStable_Generic, Walkable}; pub use rustc_span::AttrId; -use rustc_span::source_map::{Spanned, respan}; -use rustc_span::{ByteSymbol, DUMMY_SP, ErrorGuaranteed, Ident, Span, Symbol, kw, sym}; +use rustc_span::{ + ByteSymbol, DUMMY_SP, ErrorGuaranteed, Ident, Span, Spanned, Symbol, kw, respan, sym, +}; use thin_vec::{ThinVec, thin_vec}; use crate::attr::data_structures::CfgEntry; diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs index be8e1d22c9dbf..881b6ff107b56 100644 --- a/compiler/rustc_ast/src/mut_visit.rs +++ b/compiler/rustc_ast/src/mut_visit.rs @@ -11,8 +11,7 @@ use std::ops::DerefMut; use std::panic; use rustc_data_structures::flat_map_in_place::FlatMapInPlace; -use rustc_span::source_map::Spanned; -use rustc_span::{Ident, Span, Symbol}; +use rustc_span::{Ident, Span, Spanned, Symbol}; use smallvec::{SmallVec, smallvec}; use thin_vec::ThinVec; diff --git a/compiler/rustc_ast/src/visit.rs b/compiler/rustc_ast/src/visit.rs index c3c1c518d8495..bdf290f9a9e63 100644 --- a/compiler/rustc_ast/src/visit.rs +++ b/compiler/rustc_ast/src/visit.rs @@ -15,8 +15,7 @@ pub use rustc_ast_ir::visit::VisitorResult; pub use rustc_ast_ir::{try_visit, visit_opt, walk_list, walk_visitable_list}; -use rustc_span::source_map::Spanned; -use rustc_span::{Ident, Span, Symbol}; +use rustc_span::{Ident, Span, Spanned, Symbol}; use thin_vec::ThinVec; use crate::ast::*; diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index 4a2992038003c..305df40651969 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -13,8 +13,7 @@ use rustc_hir::{HirId, Target, find_attr}; use rustc_middle::span_bug; use rustc_middle::ty::TyCtxt; use rustc_session::errors::report_lit_error; -use rustc_span::source_map::{Spanned, respan}; -use rustc_span::{ByteSymbol, DUMMY_SP, DesugaringKind, Ident, Span, Symbol, sym}; +use rustc_span::{ByteSymbol, DUMMY_SP, DesugaringKind, Ident, Span, Spanned, Symbol, respan, sym}; use thin_vec::{ThinVec, thin_vec}; use visit::{Visitor, walk_expr}; diff --git a/compiler/rustc_ast_lowering/src/pat.rs b/compiler/rustc_ast_lowering/src/pat.rs index e066bce95158d..c1c13977e1037 100644 --- a/compiler/rustc_ast_lowering/src/pat.rs +++ b/compiler/rustc_ast_lowering/src/pat.rs @@ -6,8 +6,7 @@ use rustc_hir::def::{DefKind, Res}; use rustc_hir::definitions::DefPathData; use rustc_hir::{self as hir, LangItem, Target}; use rustc_middle::span_bug; -use rustc_span::source_map::{Spanned, respan}; -use rustc_span::{DesugaringKind, Ident, Span}; +use rustc_span::{DesugaringKind, Ident, Span, Spanned, respan}; use super::errors::{ ArbitraryExpressionInPattern, ExtraDoubleDot, MisplacedDoubleDot, SubTupleBinding, diff --git a/compiler/rustc_ast_passes/src/feature_gate.rs b/compiler/rustc_ast_passes/src/feature_gate.rs index 72679d7456659..ea8521a831400 100644 --- a/compiler/rustc_ast_passes/src/feature_gate.rs +++ b/compiler/rustc_ast_passes/src/feature_gate.rs @@ -7,8 +7,7 @@ use rustc_hir::Attribute; use rustc_hir::attrs::AttributeKind; use rustc_session::Session; use rustc_session::parse::{feature_err, feature_warn}; -use rustc_span::source_map::Spanned; -use rustc_span::{DUMMY_SP, Span, Symbol, sym}; +use rustc_span::{DUMMY_SP, Span, Spanned, Symbol, sym}; use thin_vec::ThinVec; use crate::errors; diff --git a/compiler/rustc_ast_pretty/src/pprust/state.rs b/compiler/rustc_ast_pretty/src/pprust/state.rs index c85d6f454321e..4ba5dc541342a 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state.rs @@ -20,9 +20,11 @@ use rustc_ast::{ RangeEnd, RangeSyntax, Safety, SelfKind, Term, attr, }; use rustc_span::edition::Edition; -use rustc_span::source_map::{SourceMap, Spanned}; +use rustc_span::source_map::SourceMap; use rustc_span::symbol::IdentPrinter; -use rustc_span::{BytePos, CharPos, DUMMY_SP, FileName, Ident, Pos, Span, Symbol, kw, sym}; +use rustc_span::{ + BytePos, CharPos, DUMMY_SP, FileName, Ident, Pos, Span, Spanned, Symbol, kw, sym, +}; use crate::pp::Breaks::{Consistent, Inconsistent}; use crate::pp::{self, BoxMarker, Breaks}; diff --git a/compiler/rustc_borrowck/src/diagnostics/mod.rs b/compiler/rustc_borrowck/src/diagnostics/mod.rs index af8f723ff378d..cb11d35c44b66 100644 --- a/compiler/rustc_borrowck/src/diagnostics/mod.rs +++ b/compiler/rustc_borrowck/src/diagnostics/mod.rs @@ -23,8 +23,7 @@ use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_middle::{bug, span_bug}; use rustc_mir_dataflow::move_paths::{InitLocation, LookupResult, MoveOutIndex}; use rustc_span::def_id::LocalDefId; -use rustc_span::source_map::Spanned; -use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, Symbol, sym}; +use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, Spanned, Symbol, sym}; use rustc_trait_selection::error_reporting::InferCtxtErrorExt; use rustc_trait_selection::error_reporting::traits::call_kind::{CallDesugaringKind, call_kind}; use rustc_trait_selection::infer::InferCtxtExt; diff --git a/compiler/rustc_borrowck/src/type_check/mod.rs b/compiler/rustc_borrowck/src/type_check/mod.rs index 29d38af472c22..9aa8f7a2067a5 100644 --- a/compiler/rustc_borrowck/src/type_check/mod.rs +++ b/compiler/rustc_borrowck/src/type_check/mod.rs @@ -32,8 +32,7 @@ use rustc_middle::ty::{ use rustc_mir_dataflow::move_paths::MoveData; use rustc_mir_dataflow::points::DenseLocationMap; use rustc_span::def_id::CRATE_DEF_ID; -use rustc_span::source_map::Spanned; -use rustc_span::{Span, sym}; +use rustc_span::{Span, Spanned, sym}; use rustc_trait_selection::infer::InferCtxtExt; use rustc_trait_selection::traits::query::type_op::custom::scrape_region_constraints; use rustc_trait_selection::traits::query::type_op::{TypeOp, TypeOpOutput}; diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/ty.rs b/compiler/rustc_builtin_macros/src/deriving/generic/ty.rs index 1458553d4925e..e7972c5436e13 100644 --- a/compiler/rustc_builtin_macros/src/deriving/generic/ty.rs +++ b/compiler/rustc_builtin_macros/src/deriving/generic/ty.rs @@ -4,8 +4,7 @@ pub(crate) use Ty::*; use rustc_ast::{self as ast, Expr, GenericArg, GenericParamKind, Generics, SelfKind, TyKind}; use rustc_expand::base::ExtCtxt; -use rustc_span::source_map::respan; -use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw}; +use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw, respan}; use thin_vec::ThinVec; /// A path, e.g., `::std::option::Option::` (global). Has support diff --git a/compiler/rustc_builtin_macros/src/env.rs b/compiler/rustc_builtin_macros/src/env.rs index af78db156a221..12f59467ffd16 100644 --- a/compiler/rustc_builtin_macros/src/env.rs +++ b/compiler/rustc_builtin_macros/src/env.rs @@ -140,18 +140,21 @@ pub(crate) fn expand_env<'cx>( unreachable!("`expr_to_string` ensures this is a string lit") }; + let var = var.as_str(); let guar = match err { VarError::NotPresent => { if let Some(msg_from_user) = custom_msg { cx.dcx() .emit_err(errors::EnvNotDefinedWithUserMessage { span, msg_from_user }) - } else if let Some(suggested_var) = find_similar_cargo_var(var.as_str()) { + } else if let Some(suggested_var) = find_similar_cargo_var(var) + && suggested_var != var + { cx.dcx().emit_err(errors::EnvNotDefined::CargoEnvVarTypo { span, var: *symbol, suggested_var: Symbol::intern(suggested_var), }) - } else if is_cargo_env_var(var.as_str()) { + } else if is_cargo_env_var(var) { cx.dcx().emit_err(errors::EnvNotDefined::CargoEnvVar { span, var: *symbol, @@ -177,7 +180,7 @@ pub(crate) fn expand_env<'cx>( ExpandResult::Ready(MacEager::expr(e)) } -/// Returns `true` if an environment variable from `env!` is one used by Cargo. +/// Returns `true` if an environment variable from `env!` could be one used by Cargo. fn is_cargo_env_var(var: &str) -> bool { var.starts_with("CARGO_") || var.starts_with("DEP_") @@ -187,25 +190,28 @@ fn is_cargo_env_var(var: &str) -> bool { const KNOWN_CARGO_VARS: &[&str] = &[ // List of known Cargo environment variables that are set for crates (not build scripts, OUT_DIR etc). // See: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates - "CARGO_PKG_VERSION", - "CARGO_PKG_VERSION_MAJOR", - "CARGO_PKG_VERSION_MINOR", - "CARGO_PKG_VERSION_PATCH", - "CARGO_PKG_VERSION_PRE", + // tidy-alphabetical-start + "CARGO_BIN_NAME", + "CARGO_CRATE_NAME", + "CARGO_MANIFEST_DIR", + "CARGO_MANIFEST_PATH", "CARGO_PKG_AUTHORS", - "CARGO_PKG_NAME", "CARGO_PKG_DESCRIPTION", "CARGO_PKG_HOMEPAGE", - "CARGO_PKG_REPOSITORY", "CARGO_PKG_LICENSE", "CARGO_PKG_LICENSE_FILE", - "CARGO_PKG_RUST_VERSION", + "CARGO_PKG_NAME", "CARGO_PKG_README", - "CARGO_MANIFEST_DIR", - "CARGO_MANIFEST_PATH", - "CARGO_CRATE_NAME", - "CARGO_BIN_NAME", + "CARGO_PKG_REPOSITORY", + "CARGO_PKG_RUST_VERSION", + "CARGO_PKG_VERSION", + "CARGO_PKG_VERSION_MAJOR", + "CARGO_PKG_VERSION_MINOR", + "CARGO_PKG_VERSION_PATCH", + "CARGO_PKG_VERSION_PRE", "CARGO_PRIMARY_PACKAGE", + "CARGO_TARGET_TMPDIR", + // tidy-alphabetical-end ]; fn find_similar_cargo_var(var: &str) -> Option<&'static str> { @@ -219,7 +225,13 @@ fn find_similar_cargo_var(var: &str) -> Option<&'static str> { let mut best_distance = usize::MAX; for &known_var in KNOWN_CARGO_VARS { - if let Some(distance) = edit_distance(var, known_var, max_dist) { + if let Some(mut distance) = edit_distance(var, known_var, max_dist) { + // assume `PACKAGE` to equals `PKG` + // (otherwise, `d("CARGO_PACKAGE_NAME", "CARGO_PKG_NAME") == d("CARGO_PACKAGE_NAME", "CARGO_CRATE_NAME") == 4`) + if var.contains("PACKAGE") && known_var.contains("PKG") { + distance = distance.saturating_sub(const { "PACKAGE".len() - "PKG".len() }) // == d("PACKAGE", "PKG") + } + if distance < best_distance { best_distance = distance; best_match = Some(known_var); diff --git a/compiler/rustc_builtin_macros/src/errors.rs b/compiler/rustc_builtin_macros/src/errors.rs index 39d210e14a098..171c15eb4b9fc 100644 --- a/compiler/rustc_builtin_macros/src/errors.rs +++ b/compiler/rustc_builtin_macros/src/errors.rs @@ -545,6 +545,7 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for EnvNotDefinedWithUserMessag #[derive(Diagnostic)] pub(crate) enum EnvNotDefined<'a> { #[diag("environment variable `{$var}` not defined at compile time")] + #[help("`{$var}` may not be available for the current Cargo target")] #[help( "Cargo sets build script variables at run time. Use `std::env::var({$var_expr})` instead" )] diff --git a/compiler/rustc_codegen_cranelift/src/abi/mod.rs b/compiler/rustc_codegen_cranelift/src/abi/mod.rs index 97a19b8976d3a..13f5ad5157cef 100644 --- a/compiler/rustc_codegen_cranelift/src/abi/mod.rs +++ b/compiler/rustc_codegen_cranelift/src/abi/mod.rs @@ -20,7 +20,7 @@ use rustc_middle::ty::TypeVisitableExt; use rustc_middle::ty::layout::FnAbiOf; use rustc_middle::ty::print::with_no_trimmed_paths; use rustc_session::Session; -use rustc_span::source_map::Spanned; +use rustc_span::Spanned; use rustc_target::callconv::{FnAbi, PassMode}; use rustc_target::spec::Arch; use smallvec::{SmallVec, smallvec}; diff --git a/compiler/rustc_codegen_cranelift/src/common.rs b/compiler/rustc_codegen_cranelift/src/common.rs index b11f42408f58b..b22afca847aa9 100644 --- a/compiler/rustc_codegen_cranelift/src/common.rs +++ b/compiler/rustc_codegen_cranelift/src/common.rs @@ -6,8 +6,7 @@ use rustc_middle::ty::TypeFoldable; use rustc_middle::ty::layout::{ self, FnAbiError, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOfHelpers, }; -use rustc_span::Symbol; -use rustc_span::source_map::Spanned; +use rustc_span::{Spanned, Symbol}; use rustc_target::callconv::FnAbi; use rustc_target::spec::{Arch, HasTargetSpec, Target}; diff --git a/compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs b/compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs index ab9a11305baa3..da8569dac7468 100644 --- a/compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs +++ b/compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs @@ -24,8 +24,7 @@ use rustc_middle::ty; use rustc_middle::ty::GenericArgsRef; use rustc_middle::ty::layout::ValidityRequirement; use rustc_middle::ty::print::{with_no_trimmed_paths, with_no_visible_paths}; -use rustc_span::source_map::Spanned; -use rustc_span::{Symbol, sym}; +use rustc_span::{Spanned, Symbol, sym}; use rustc_target::spec::PanicStrategy; pub(crate) use self::llvm::codegen_llvm_intrinsic_call; diff --git a/compiler/rustc_codegen_gcc/src/context.rs b/compiler/rustc_codegen_gcc/src/context.rs index ada3d73f612e4..5423ee5390fe5 100644 --- a/compiler/rustc_codegen_gcc/src/context.rs +++ b/compiler/rustc_codegen_gcc/src/context.rs @@ -21,8 +21,7 @@ use rustc_middle::ty::{self, ExistentialTraitRef, Instance, Ty, TyCtxt}; use rustc_session::Session; #[cfg(feature = "master")] use rustc_session::config::DebugInfo; -use rustc_span::source_map::respan; -use rustc_span::{DUMMY_SP, Span}; +use rustc_span::{DUMMY_SP, Span, respan}; use rustc_target::spec::{HasTargetSpec, HasX86AbiOpt, Target, TlsModel, X86Abi}; #[cfg(feature = "master")] diff --git a/compiler/rustc_codegen_llvm/src/context.rs b/compiler/rustc_codegen_llvm/src/context.rs index 2760683dad9d1..28aa12cc83dc5 100644 --- a/compiler/rustc_codegen_llvm/src/context.rs +++ b/compiler/rustc_codegen_llvm/src/context.rs @@ -25,8 +25,7 @@ use rustc_session::Session; use rustc_session::config::{ BranchProtection, CFGuard, CFProtection, CrateType, DebugInfo, FunctionReturn, PAuthKey, PacRet, }; -use rustc_span::source_map::Spanned; -use rustc_span::{DUMMY_SP, Span, Symbol}; +use rustc_span::{DUMMY_SP, Span, Spanned, Symbol}; use rustc_symbol_mangling::mangle_internal_symbol; use rustc_target::spec::{ Abi, Arch, Env, HasTargetSpec, Os, RelocModel, SmallDataThresholdSupport, Target, TlsModel, diff --git a/compiler/rustc_codegen_ssa/src/mir/block.rs b/compiler/rustc_codegen_ssa/src/mir/block.rs index cf643931717be..d4c10689f65ca 100644 --- a/compiler/rustc_codegen_ssa/src/mir/block.rs +++ b/compiler/rustc_codegen_ssa/src/mir/block.rs @@ -12,8 +12,7 @@ use rustc_middle::ty::print::{with_no_trimmed_paths, with_no_visible_paths}; use rustc_middle::ty::{self, Instance, Ty, TypeVisitableExt}; use rustc_middle::{bug, span_bug}; use rustc_session::config::OptLevel; -use rustc_span::Span; -use rustc_span::source_map::Spanned; +use rustc_span::{Span, Spanned}; use rustc_target::callconv::{ArgAbi, ArgAttributes, CastTarget, FnAbi, PassMode}; use tracing::{debug, info}; diff --git a/compiler/rustc_const_eval/src/interpret/step.rs b/compiler/rustc_const_eval/src/interpret/step.rs index 083fd97aec4dc..2dee1157e2a12 100644 --- a/compiler/rustc_const_eval/src/interpret/step.rs +++ b/compiler/rustc_const_eval/src/interpret/step.rs @@ -10,7 +10,7 @@ use rustc_data_structures::fx::FxHashSet; use rustc_index::IndexSlice; use rustc_middle::ty::{self, Instance, Ty}; use rustc_middle::{bug, mir, span_bug}; -use rustc_span::source_map::Spanned; +use rustc_span::Spanned; use rustc_target::callconv::FnAbi; use tracing::field::Empty; use tracing::{info, instrument, trace}; diff --git a/compiler/rustc_errors/src/diagnostic.rs b/compiler/rustc_errors/src/diagnostic.rs index 18f1af36b6202..ceb76254e03fe 100644 --- a/compiler/rustc_errors/src/diagnostic.rs +++ b/compiler/rustc_errors/src/diagnostic.rs @@ -11,8 +11,7 @@ use rustc_data_structures::sync::DynSend; use rustc_error_messages::{DiagArgMap, DiagArgName, DiagArgValue, IntoDiagArg}; use rustc_lint_defs::{Applicability, LintExpectationId}; use rustc_macros::{Decodable, Encodable}; -use rustc_span::source_map::Spanned; -use rustc_span::{DUMMY_SP, Span, Symbol}; +use rustc_span::{DUMMY_SP, Span, Spanned, Symbol}; use tracing::debug; use crate::{ diff --git a/compiler/rustc_expand/src/build.rs b/compiler/rustc_expand/src/build.rs index 19a2d65762e86..3680f24707df0 100644 --- a/compiler/rustc_expand/src/build.rs +++ b/compiler/rustc_expand/src/build.rs @@ -5,8 +5,7 @@ use rustc_ast::{ self as ast, AnonConst, AttrItem, AttrVec, BlockCheckMode, Expr, LocalKind, MatchKind, MgcaDisambiguation, PatKind, UnOp, attr, token, tokenstream, }; -use rustc_span::source_map::Spanned; -use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw, sym}; +use rustc_span::{DUMMY_SP, Ident, Span, Spanned, Symbol, kw, sym}; use thin_vec::{ThinVec, thin_vec}; use crate::base::ExtCtxt; diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 45a363b97722a..7cce0eda4dda7 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -23,9 +23,8 @@ use rustc_error_messages::{DiagArgValue, IntoDiagArg}; use rustc_index::IndexVec; use rustc_macros::{Decodable, Encodable, HashStable_Generic}; use rustc_span::def_id::LocalDefId; -use rustc_span::source_map::Spanned; use rustc_span::{ - BytePos, DUMMY_SP, DesugaringKind, ErrorGuaranteed, Ident, Span, Symbol, kw, sym, + BytePos, DUMMY_SP, DesugaringKind, ErrorGuaranteed, Ident, Span, Spanned, Symbol, kw, sym, }; use rustc_target::asm::InlineAsmRegOrRegClass; use smallvec::SmallVec; diff --git a/compiler/rustc_hir_analysis/src/check/check.rs b/compiler/rustc_hir_analysis/src/check/check.rs index 4157b110fbf6e..83b4833b51b03 100644 --- a/compiler/rustc_hir_analysis/src/check/check.rs +++ b/compiler/rustc_hir_analysis/src/check/check.rs @@ -24,7 +24,7 @@ use rustc_middle::ty::{ TypeVisitable, TypeVisitableExt, fold_regions, }; use rustc_session::lint::builtin::UNINHABITED_STATIC; -use rustc_span::source_map::Spanned; +use rustc_span::Spanned; use rustc_target::spec::{AbiMap, AbiMapping}; use rustc_trait_selection::error_reporting::InferCtxtErrorExt; use rustc_trait_selection::traits; diff --git a/compiler/rustc_hir_analysis/src/check/region.rs b/compiler/rustc_hir_analysis/src/check/region.rs index 0c611e6c4c9e6..8865638e3af2f 100644 --- a/compiler/rustc_hir_analysis/src/check/region.rs +++ b/compiler/rustc_hir_analysis/src/check/region.rs @@ -18,7 +18,7 @@ use rustc_index::Idx; use rustc_middle::middle::region::*; use rustc_middle::ty::TyCtxt; use rustc_session::lint; -use rustc_span::source_map; +use rustc_span::Spanned; use tracing::debug; #[derive(Debug, Copy, Clone)] @@ -181,7 +181,7 @@ fn resolve_cond<'tcx>(visitor: &mut ScopeResolutionVisitor<'tcx>, cond: &'tcx hi // operands will be terminated). Any temporaries that would need to be dropped will be // dropped before we leave this operator's scope; terminating them here would be redundant. hir::ExprKind::Binary( - source_map::Spanned { node: hir::BinOpKind::And | hir::BinOpKind::Or, .. }, + Spanned { node: hir::BinOpKind::And | hir::BinOpKind::Or, .. }, _, _, ) => false, @@ -264,7 +264,7 @@ fn resolve_expr<'tcx>( // scopes, meaning that temporaries cannot outlive them. // This ensures fixed size stacks. hir::ExprKind::Binary( - source_map::Spanned { node: hir::BinOpKind::And | hir::BinOpKind::Or, .. }, + Spanned { node: hir::BinOpKind::And | hir::BinOpKind::Or, .. }, left, right, ) => { @@ -293,7 +293,7 @@ fn resolve_expr<'tcx>( // This is purely an optimization to reduce the number of // terminating scopes. hir::ExprKind::Binary( - source_map::Spanned { node: hir::BinOpKind::And | hir::BinOpKind::Or, .. }, + Spanned { node: hir::BinOpKind::And | hir::BinOpKind::Or, .. }, .., ) => false, // otherwise: mark it as terminating diff --git a/compiler/rustc_hir_pretty/src/lib.rs b/compiler/rustc_hir_pretty/src/lib.rs index e806d72d3dd23..82540a9327410 100644 --- a/compiler/rustc_hir_pretty/src/lib.rs +++ b/compiler/rustc_hir_pretty/src/lib.rs @@ -23,8 +23,8 @@ use rustc_hir::{ GenericParam, GenericParamKind, HirId, ImplicitSelfKind, LifetimeParamKind, Node, PatKind, PreciseCapturingArg, RangeEnd, Term, TyFieldPath, TyPatKind, }; -use rustc_span::source_map::{SourceMap, Spanned}; -use rustc_span::{DUMMY_SP, FileName, Ident, Span, Symbol, kw, sym}; +use rustc_span::source_map::SourceMap; +use rustc_span::{DUMMY_SP, FileName, Ident, Span, Spanned, Symbol, kw, sym}; pub fn id_to_string(cx: &dyn rustc_hir::intravisit::HirTyCtxt<'_>, hir_id: HirId) -> String { to_string(&cx, |s| s.print_node(cx.hir_node(hir_id))) diff --git a/compiler/rustc_hir_typeck/src/coercion.rs b/compiler/rustc_hir_typeck/src/coercion.rs index 8a73125d64765..bfc677046e0f4 100644 --- a/compiler/rustc_hir_typeck/src/coercion.rs +++ b/compiler/rustc_hir_typeck/src/coercion.rs @@ -114,6 +114,26 @@ fn success<'tcx>( Ok(InferOk { value: (adj, target), obligations }) } +/// Data extracted from a reference (pinned or not) for coercion to a reference (pinned or not). +struct CoerceMaybePinnedRef<'tcx> { + /// coercion source, must be a pinned (i.e. `Pin<&T>` or `Pin<&mut T>`) or normal reference (`&T` or `&mut T`) + a: Ty<'tcx>, + /// coercion target, must be a pinned (i.e. `Pin<&T>` or `Pin<&mut T>`) or normal reference (`&T` or `&mut T`) + b: Ty<'tcx>, + /// referent type of the source + a_ty: Ty<'tcx>, + /// pinnedness of the source + a_pin: ty::Pinnedness, + /// mutability of the source + a_mut: ty::Mutability, + /// region of the source + a_r: ty::Region<'tcx>, + /// pinnedness of the target + b_pin: ty::Pinnedness, + /// mutability of the target + b_mut: ty::Mutability, +} + /// Whether to force a leak check to occur in `Coerce::unify_raw`. /// Note that leak checks may still occur evn with `ForceLeakCheck::No`. /// @@ -269,16 +289,13 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> { return self.coerce_to_raw_ptr(a, b, b_mutbl); } ty::Ref(r_b, _, mutbl_b) => { + if let Some(pin_ref_to_ref) = self.maybe_pin_ref_to_ref(a, b) { + return self.coerce_pin_ref_to_ref(pin_ref_to_ref); + } return self.coerce_to_ref(a, b, r_b, mutbl_b); } - ty::Adt(pin, _) - if self.tcx.features().pin_ergonomics() - && self.tcx.is_lang_item(pin.did(), hir::LangItem::Pin) => - { - let pin_coerce = self.commit_if_ok(|_| self.coerce_to_pin_ref(a, b)); - if pin_coerce.is_ok() { - return pin_coerce; - } + _ if let Some(to_pin_ref) = self.maybe_to_pin_ref(a, b) => { + return self.coerce_to_pin_ref(to_pin_ref); } _ => {} } @@ -790,61 +807,131 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> { Ok(()) } - /// Applies reborrowing for `Pin` + /// Create an obligation for `ty: Unpin`, where . + fn unpin_obligation( + &self, + source: Ty<'tcx>, + target: Ty<'tcx>, + ty: Ty<'tcx>, + ) -> PredicateObligation<'tcx> { + let pred = ty::TraitRef::new( + self.tcx, + self.tcx.require_lang_item(hir::LangItem::Unpin, self.cause.span), + [ty], + ); + let cause = self.cause(self.cause.span, ObligationCauseCode::Coercion { source, target }); + PredicateObligation::new(self.tcx, cause, self.param_env, pred) + } + + /// Checks if the given types are compatible for coercion from a pinned reference to a normal reference. + fn maybe_pin_ref_to_ref(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> Option> { + if !self.tcx.features().pin_ergonomics() { + return None; + } + if let Some((a_ty, a_pin @ ty::Pinnedness::Pinned, a_mut, a_r)) = a.maybe_pinned_ref() + && let Some((_, b_pin @ ty::Pinnedness::Not, b_mut, _)) = b.maybe_pinned_ref() + { + return Some(CoerceMaybePinnedRef { a, b, a_ty, a_pin, a_mut, a_r, b_pin, b_mut }); + } + debug!("not fitting pinned ref to ref coercion (`{:?}` -> `{:?}`)", a, b); + None + } + + /// Coerces from a pinned reference to a normal reference. + #[instrument(skip(self), level = "trace")] + fn coerce_pin_ref_to_ref( + &self, + CoerceMaybePinnedRef { a, b, a_ty, a_pin, a_mut, a_r, b_pin, b_mut }: CoerceMaybePinnedRef< + 'tcx, + >, + ) -> CoerceResult<'tcx> { + debug_assert!(self.shallow_resolve(a) == a); + debug_assert!(self.shallow_resolve(b) == b); + debug_assert!(self.tcx.features().pin_ergonomics()); + debug_assert_eq!(a_pin, ty::Pinnedness::Pinned); + debug_assert_eq!(b_pin, ty::Pinnedness::Not); + + coerce_mutbls(a_mut, b_mut)?; + + let unpin_obligation = self.unpin_obligation(a, b, a_ty); + + let a = Ty::new_ref(self.tcx, a_r, a_ty, b_mut); + let mut coerce = self.unify_and( + a, + b, + [Adjustment { kind: Adjust::Deref(DerefAdjustKind::Pin), target: a_ty }], + Adjust::Borrow(AutoBorrow::Ref(AutoBorrowMutability::new(b_mut, self.allow_two_phase))), + ForceLeakCheck::No, + )?; + coerce.obligations.push(unpin_obligation); + Ok(coerce) + } + + /// Checks if the given types are compatible for coercion to a pinned reference. + fn maybe_to_pin_ref(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> Option> { + if !self.tcx.features().pin_ergonomics() { + return None; + } + if let Some((a_ty, a_pin, a_mut, a_r)) = a.maybe_pinned_ref() + && let Some((_, b_pin @ ty::Pinnedness::Pinned, b_mut, _)) = b.maybe_pinned_ref() + { + return Some(CoerceMaybePinnedRef { a, b, a_ty, a_pin, a_mut, a_r, b_pin, b_mut }); + } + debug!("not fitting ref to pinned ref coercion (`{:?}` -> `{:?}`)", a, b); + None + } + + /// Applies reborrowing and auto-borrowing that results to `Pin<&T>` or `Pin<&mut T>`: /// - /// We currently only support reborrowing `Pin<&mut T>` as `Pin<&mut T>`. This is accomplished - /// by inserting a call to `Pin::as_mut` during MIR building. + /// Currently we only support the following coercions: + /// - Reborrowing `Pin<&mut T>` -> `Pin<&mut T>` + /// - Reborrowing `Pin<&T>` -> `Pin<&T>` + /// - Auto-borrowing `&mut T` -> `Pin<&mut T>` where `T: Unpin` + /// - Auto-borrowing `&mut T` -> `Pin<&T>` where `T: Unpin` + /// - Auto-borrowing `&T` -> `Pin<&T>` where `T: Unpin` /// /// In the future we might want to support other reborrowing coercions, such as: - /// - `Pin<&mut T>` as `Pin<&T>` - /// - `Pin<&T>` as `Pin<&T>` /// - `Pin>` as `Pin<&T>` /// - `Pin>` as `Pin<&mut T>` #[instrument(skip(self), level = "trace")] - fn coerce_to_pin_ref(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> CoerceResult<'tcx> { + fn coerce_to_pin_ref( + &self, + CoerceMaybePinnedRef { a, b, a_ty, a_pin, a_mut, a_r, b_pin, b_mut }: CoerceMaybePinnedRef< + 'tcx, + >, + ) -> CoerceResult<'tcx> { debug_assert!(self.shallow_resolve(a) == a); debug_assert!(self.shallow_resolve(b) == b); - - // We need to make sure the two types are compatible for coercion. - // Then we will build a ReborrowPin adjustment and return that as an InferOk. - - // Right now we can only reborrow if this is a `Pin<&mut T>`. - let extract_pin_mut = |ty: Ty<'tcx>| { - // Get the T out of Pin - let (pin, ty) = match ty.kind() { - ty::Adt(pin, args) if self.tcx.is_lang_item(pin.did(), hir::LangItem::Pin) => { - (*pin, args[0].expect_ty()) - } - _ => { - debug!("can't reborrow {:?} as pinned", ty); - return Err(TypeError::Mismatch); - } - }; - // Make sure the T is something we understand (just `&mut U` for now) - match ty.kind() { - ty::Ref(region, ty, mutbl) => Ok((pin, *region, *ty, *mutbl)), - _ => { - debug!("can't reborrow pin of inner type {:?}", ty); - Err(TypeError::Mismatch) - } + debug_assert!(self.tcx.features().pin_ergonomics()); + debug_assert_eq!(b_pin, ty::Pinnedness::Pinned); + + // We need to deref the reference first before we reborrow it to a pinned reference. + let (deref, unpin_obligation) = match a_pin { + // no `Unpin` required when reborrowing a pinned reference to a pinned reference + ty::Pinnedness::Pinned => (DerefAdjustKind::Pin, None), + // `Unpin` required when reborrowing a non-pinned reference to a pinned reference + ty::Pinnedness::Not => { + (DerefAdjustKind::Builtin, Some(self.unpin_obligation(a, b, a_ty))) } }; - let (pin, a_region, a_ty, mut_a) = extract_pin_mut(a)?; - let (_, _, _b_ty, mut_b) = extract_pin_mut(b)?; - - coerce_mutbls(mut_a, mut_b)?; + coerce_mutbls(a_mut, b_mut)?; // update a with b's mutability since we'll be coercing mutability - let a = Ty::new_adt( - self.tcx, - pin, - self.tcx.mk_args(&[Ty::new_ref(self.tcx, a_region, a_ty, mut_b).into()]), - ); + let a = Ty::new_pinned_ref(self.tcx, a_r, a_ty, b_mut); // To complete the reborrow, we need to make sure we can unify the inner types, and if so we // add the adjustments. - self.unify_and(a, b, [], Adjust::ReborrowPin(mut_b), ForceLeakCheck::No) + let mut coerce = self.unify_and( + a, + b, + [Adjustment { kind: Adjust::Deref(deref), target: a_ty }], + Adjust::Borrow(AutoBorrow::Pin(b_mut)), + ForceLeakCheck::No, + )?; + + coerce.obligations.extend(unpin_obligation); + Ok(coerce) } fn coerce_from_fn_pointer( diff --git a/compiler/rustc_hir_typeck/src/errors.rs b/compiler/rustc_hir_typeck/src/errors.rs index 5c6a66403019c..6eef156846972 100644 --- a/compiler/rustc_hir_typeck/src/errors.rs +++ b/compiler/rustc_hir_typeck/src/errors.rs @@ -14,8 +14,7 @@ use rustc_hir::ExprKind; use rustc_macros::{Diagnostic, Subdiagnostic}; use rustc_middle::ty::{self, Ty}; use rustc_span::edition::{Edition, LATEST_STABLE_EDITION}; -use rustc_span::source_map::Spanned; -use rustc_span::{Ident, Span, Symbol}; +use rustc_span::{Ident, Span, Spanned, Symbol}; use crate::FnCtxt; diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs index e45d1cf8b5e3b..17c7c4b76b2d6 100644 --- a/compiler/rustc_hir_typeck/src/expr.rs +++ b/compiler/rustc_hir_typeck/src/expr.rs @@ -34,8 +34,7 @@ use rustc_session::errors::ExprParenthesesNeeded; use rustc_session::parse::feature_err; use rustc_span::edit_distance::find_best_match_for_name; use rustc_span::hygiene::DesugaringKind; -use rustc_span::source_map::Spanned; -use rustc_span::{Ident, Span, Symbol, kw, sym}; +use rustc_span::{Ident, Span, Spanned, Symbol, kw, sym}; use rustc_trait_selection::infer::InferCtxtExt; use rustc_trait_selection::traits::{self, ObligationCauseCode, ObligationCtxt}; use tracing::{debug, instrument, trace}; diff --git a/compiler/rustc_hir_typeck/src/expr_use_visitor.rs b/compiler/rustc_hir_typeck/src/expr_use_visitor.rs index 0a492c795b29c..b6dfda33142c5 100644 --- a/compiler/rustc_hir_typeck/src/expr_use_visitor.rs +++ b/compiler/rustc_hir_typeck/src/expr_use_visitor.rs @@ -736,7 +736,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx self.consume_or_copy(&place_with_id, place_with_id.hir_id); } - adjustment::Adjust::Deref(DerefAdjustKind::Builtin) => {} + adjustment::Adjust::Deref(DerefAdjustKind::Builtin | DerefAdjustKind::Pin) => {} // Autoderefs for overloaded Deref calls in fact reference // their receiver. That is, if we have `(*x)` where `x` @@ -791,7 +791,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx ); } - adjustment::AutoBorrow::RawPtr(m) => { + adjustment::AutoBorrow::RawPtr(m) | adjustment::AutoBorrow::Pin(m) => { debug!("walk_autoref: expr.hir_id={} base_place={:?}", expr.hir_id, base_place); self.delegate.borrow_mut().borrow( diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs index c24d1127d5a62..f817ca8421473 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs @@ -331,6 +331,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { Adjust::Deref(DerefAdjustKind::Builtin) => { // FIXME(const_trait_impl): We *could* enforce `&T: [const] Deref` here. } + Adjust::Deref(DerefAdjustKind::Pin) => { + // FIXME(const_trait_impl): We *could* enforce `Pin<&T>: [const] Deref` here. + } Adjust::Pointer(_pointer_coercion) => { // FIXME(const_trait_impl): We should probably enforce these. } diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index 801f5acf9d2f3..a2f4c57bd442c 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -23,8 +23,7 @@ use rustc_middle::ty::{ suggest_constraining_type_params, }; use rustc_session::errors::ExprParenthesesNeeded; -use rustc_span::source_map::Spanned; -use rustc_span::{ExpnKind, Ident, MacroKind, Span, Symbol, sym}; +use rustc_span::{ExpnKind, Ident, MacroKind, Span, Spanned, Symbol, sym}; use rustc_trait_selection::error_reporting::InferCtxtErrorExt; use rustc_trait_selection::error_reporting::traits::DefIdOrName; use rustc_trait_selection::infer::InferCtxtExt; diff --git a/compiler/rustc_hir_typeck/src/op.rs b/compiler/rustc_hir_typeck/src/op.rs index 4b325a7ad14a1..cf61728f7c2a3 100644 --- a/compiler/rustc_hir_typeck/src/op.rs +++ b/compiler/rustc_hir_typeck/src/op.rs @@ -13,8 +13,7 @@ use rustc_middle::ty::adjustment::{ use rustc_middle::ty::print::with_no_trimmed_paths; use rustc_middle::ty::{self, IsSuggestable, Ty, TyCtxt, TypeVisitableExt}; use rustc_session::errors::ExprParenthesesNeeded; -use rustc_span::source_map::Spanned; -use rustc_span::{Span, Symbol, sym}; +use rustc_span::{Span, Spanned, Symbol, sym}; use rustc_trait_selection::infer::InferCtxtExt; use rustc_trait_selection::traits::{FulfillmentError, Obligation, ObligationCtxt}; use tracing::debug; diff --git a/compiler/rustc_lint/src/autorefs.rs b/compiler/rustc_lint/src/autorefs.rs index 34049288ea809..910a2918f9f71 100644 --- a/compiler/rustc_lint/src/autorefs.rs +++ b/compiler/rustc_lint/src/autorefs.rs @@ -173,7 +173,7 @@ fn has_implicit_borrow(Adjustment { kind, .. }: &Adjustment<'_>) -> Option<(Muta Adjust::NeverToAny | Adjust::Pointer(..) | Adjust::ReborrowPin(..) - | Adjust::Deref(DerefAdjustKind::Builtin) - | Adjust::Borrow(AutoBorrow::RawPtr(..)) => None, + | Adjust::Deref(DerefAdjustKind::Builtin | DerefAdjustKind::Pin) + | Adjust::Borrow(AutoBorrow::RawPtr(..) | AutoBorrow::Pin(..)) => None, } } diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index 54c8c75b88fcb..af590d98c301c 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -39,8 +39,7 @@ pub use rustc_session::lint::builtin::*; use rustc_session::lint::fcw; use rustc_session::{declare_lint, declare_lint_pass, impl_lint_pass}; use rustc_span::edition::Edition; -use rustc_span::source_map::Spanned; -use rustc_span::{DUMMY_SP, Ident, InnerSpan, Span, Symbol, kw, sym}; +use rustc_span::{DUMMY_SP, Ident, InnerSpan, Span, Spanned, Symbol, kw, sym}; use rustc_target::asm::InlineAsmArch; use rustc_trait_selection::infer::{InferCtxtExt, TyCtxtInferExt}; use rustc_trait_selection::traits; diff --git a/compiler/rustc_lint/src/invalid_from_utf8.rs b/compiler/rustc_lint/src/invalid_from_utf8.rs index 41b670c92c4c5..f095d0a6a2f40 100644 --- a/compiler/rustc_lint/src/invalid_from_utf8.rs +++ b/compiler/rustc_lint/src/invalid_from_utf8.rs @@ -3,8 +3,7 @@ use std::str::Utf8Error; use rustc_ast::LitKind; use rustc_hir::{Expr, ExprKind}; use rustc_session::{declare_lint, declare_lint_pass}; -use rustc_span::source_map::Spanned; -use rustc_span::sym; +use rustc_span::{Spanned, sym}; use crate::lints::InvalidFromUtf8Diag; use crate::{LateContext, LateLintPass, LintContext}; diff --git a/compiler/rustc_lint/src/unused.rs b/compiler/rustc_lint/src/unused.rs index 03a566efc8a54..2864cf9032a1c 100644 --- a/compiler/rustc_lint/src/unused.rs +++ b/compiler/rustc_lint/src/unused.rs @@ -600,7 +600,7 @@ impl UnusedDelimLint for UnusedParens { && !value.span.from_expansion() && (ctx != UnusedDelimsCtx::LetScrutineeExpr || !matches!(inner.kind, ast::ExprKind::Binary( - rustc_span::source_map::Spanned { node, .. }, + rustc_span::Spanned { node, .. }, _, _, ) if node.is_lazy())) diff --git a/compiler/rustc_middle/src/dep_graph/graph.rs b/compiler/rustc_middle/src/dep_graph/graph.rs index 963d5f1a53bdd..73a3a65eb6c30 100644 --- a/compiler/rustc_middle/src/dep_graph/graph.rs +++ b/compiler/rustc_middle/src/dep_graph/graph.rs @@ -1021,6 +1021,7 @@ impl DepGraphData { let prev_deps = self.previous.edge_targets_from(prev_dep_node_index); + eprintln!("e {}", prev_deps.clone().count()); for dep_dep_node_index in prev_deps { self.try_mark_parent_green(tcx, dep_dep_node_index, &frame)?; } diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index 8ac532ff4863d..2dd205e8468da 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -24,8 +24,7 @@ use rustc_index::bit_set::DenseBitSet; use rustc_index::{Idx, IndexSlice, IndexVec}; use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable}; use rustc_serialize::{Decodable, Encodable}; -use rustc_span::source_map::Spanned; -use rustc_span::{DUMMY_SP, Span, Symbol}; +use rustc_span::{DUMMY_SP, Span, Spanned, Symbol}; use tracing::{debug, trace}; pub use self::query::*; diff --git a/compiler/rustc_middle/src/mir/syntax.rs b/compiler/rustc_middle/src/mir/syntax.rs index c3da2f0394852..455089f285d17 100644 --- a/compiler/rustc_middle/src/mir/syntax.rs +++ b/compiler/rustc_middle/src/mir/syntax.rs @@ -11,8 +11,7 @@ use rustc_hir::def_id::DefId; use rustc_index::IndexVec; use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable}; use rustc_span::def_id::LocalDefId; -use rustc_span::source_map::Spanned; -use rustc_span::{Span, Symbol}; +use rustc_span::{Span, Spanned, Symbol}; use rustc_target::asm::InlineAsmRegOrRegClass; use smallvec::SmallVec; diff --git a/compiler/rustc_middle/src/queries.rs b/compiler/rustc_middle/src/queries.rs index 2636fc7024ca3..c20a389f56b97 100644 --- a/compiler/rustc_middle/src/queries.rs +++ b/compiler/rustc_middle/src/queries.rs @@ -96,8 +96,7 @@ use rustc_session::cstore::{ }; use rustc_session::lint::LintExpectationId; use rustc_span::def_id::LOCAL_CRATE; -use rustc_span::source_map::Spanned; -use rustc_span::{DUMMY_SP, LocalExpnId, Span, Symbol}; +use rustc_span::{DUMMY_SP, LocalExpnId, Span, Spanned, Symbol}; use rustc_target::spec::PanicStrategy; use crate::infer::canonical::{self, Canonical}; diff --git a/compiler/rustc_middle/src/query/erase.rs b/compiler/rustc_middle/src/query/erase.rs index 4d4833b4943e4..a02f6f89b9b90 100644 --- a/compiler/rustc_middle/src/query/erase.rs +++ b/compiler/rustc_middle/src/query/erase.rs @@ -10,8 +10,7 @@ use std::intrinsics::transmute_unchecked; use std::mem::MaybeUninit; use rustc_ast::tokenstream::TokenStream; -use rustc_span::ErrorGuaranteed; -use rustc_span::source_map::Spanned; +use rustc_span::{ErrorGuaranteed, Spanned}; use crate::mir::interpret::EvalToValTreeResult; use crate::mir::mono::{MonoItem, NormalizationErrorInMono}; diff --git a/compiler/rustc_middle/src/query/on_disk_cache.rs b/compiler/rustc_middle/src/query/on_disk_cache.rs index f2d418fea162b..8f4e4564c5830 100644 --- a/compiler/rustc_middle/src/query/on_disk_cache.rs +++ b/compiler/rustc_middle/src/query/on_disk_cache.rs @@ -17,10 +17,9 @@ use rustc_session::Session; use rustc_span::hygiene::{ ExpnId, HygieneDecodeContext, HygieneEncodeContext, SyntaxContext, SyntaxContextKey, }; -use rustc_span::source_map::Spanned; use rustc_span::{ BlobDecoder, BytePos, ByteSymbol, CachingSourceMapView, ExpnData, ExpnHash, RelativeBytePos, - SourceFile, Span, SpanDecoder, SpanEncoder, StableSourceFileId, Symbol, + SourceFile, Span, SpanDecoder, SpanEncoder, Spanned, StableSourceFileId, Symbol, }; use crate::dep_graph::{DepNodeIndex, QuerySideEffect, SerializedDepNodeIndex}; diff --git a/compiler/rustc_middle/src/ty/adjustment.rs b/compiler/rustc_middle/src/ty/adjustment.rs index 6d546aede4cf4..58687be7440b5 100644 --- a/compiler/rustc_middle/src/ty/adjustment.rs +++ b/compiler/rustc_middle/src/ty/adjustment.rs @@ -105,6 +105,7 @@ pub enum Adjust { Pointer(PointerCoercion), /// Take a pinned reference and reborrow as a `Pin<&mut T>` or `Pin<&T>`. + // FIXME(pin_ergonomics): This can be replaced with a `Deref(Pin)` followed by a `Borrow(Pin)` ReborrowPin(hir::Mutability), } @@ -112,6 +113,7 @@ pub enum Adjust { pub enum DerefAdjustKind { Builtin, Overloaded(OverloadedDeref), + Pin, } /// An overloaded autoderef step, representing a `Deref(Mut)::deref(_mut)` @@ -196,6 +198,9 @@ pub enum AutoBorrow { /// Converts from T to *T. RawPtr(hir::Mutability), + + /// Converts from T to Pin<&T>. + Pin(hir::Mutability), } /// Information for `CoerceUnsized` impls, storing information we diff --git a/compiler/rustc_middle/src/ty/codec.rs b/compiler/rustc_middle/src/ty/codec.rs index d1987f51f6e01..6652c5a758daa 100644 --- a/compiler/rustc_middle/src/ty/codec.rs +++ b/compiler/rustc_middle/src/ty/codec.rs @@ -15,8 +15,7 @@ use rustc_data_structures::fx::FxHashMap; use rustc_hir::def_id::LocalDefId; use rustc_middle::ty::Const; use rustc_serialize::{Decodable, Encodable}; -use rustc_span::source_map::Spanned; -use rustc_span::{Span, SpanDecoder, SpanEncoder}; +use rustc_span::{Span, SpanDecoder, SpanEncoder, Spanned}; use crate::arena::ArenaAllocatable; use crate::infer::canonical::{CanonicalVarKind, CanonicalVarKinds}; diff --git a/compiler/rustc_middle/src/ty/structural_impls.rs b/compiler/rustc_middle/src/ty/structural_impls.rs index 04985dd3acde5..68e43bbbea205 100644 --- a/compiler/rustc_middle/src/ty/structural_impls.rs +++ b/compiler/rustc_middle/src/ty/structural_impls.rs @@ -8,7 +8,7 @@ use std::fmt::{self, Debug}; use rustc_abi::TyAndLayout; use rustc_hir::def::Namespace; use rustc_hir::def_id::LocalDefId; -use rustc_span::source_map::Spanned; +use rustc_span::Spanned; use rustc_type_ir::{ConstKind, TypeFolder, VisitorResult, try_visit}; use super::{GenericArg, GenericArgKind, Pattern, Region}; diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index 2acc58422902b..9c22d158154fb 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -1365,6 +1365,14 @@ impl<'tcx> Ty<'tcx> { } } + /// Returns the type, pinnedness, mutability, and the region of a reference (`&T` or `&mut T`) + /// or a pinned-reference type (`Pin<&T>` or `Pin<&mut T>`). + /// + /// Regarding the [`pin_ergonomics`] feature, one of the goals is to make pinned references + /// (`Pin<&T>` and `Pin<&mut T>`) behaves similar to normal references (`&T` and `&mut T`). + /// This function is useful when references and pinned references are processed similarly. + /// + /// [`pin_ergonomics`]: https://github.com/rust-lang/rust/issues/130494 pub fn maybe_pinned_ref( self, ) -> Option<(Ty<'tcx>, ty::Pinnedness, ty::Mutability, Region<'tcx>)> { diff --git a/compiler/rustc_mir_build/src/builder/custom/parse/instruction.rs b/compiler/rustc_mir_build/src/builder/custom/parse/instruction.rs index 13082d408ec08..a47a2e08c9f4b 100644 --- a/compiler/rustc_mir_build/src/builder/custom/parse/instruction.rs +++ b/compiler/rustc_mir_build/src/builder/custom/parse/instruction.rs @@ -4,8 +4,7 @@ use rustc_middle::mir::*; use rustc_middle::thir::*; use rustc_middle::ty; use rustc_middle::ty::cast::mir_cast_kind; -use rustc_span::Span; -use rustc_span::source_map::Spanned; +use rustc_span::{Span, Spanned}; use super::{PResult, ParseCtxt, parse_by_kind}; use crate::builder::custom::ParseError; diff --git a/compiler/rustc_mir_build/src/builder/expr/as_rvalue.rs b/compiler/rustc_mir_build/src/builder/expr/as_rvalue.rs index 0f206c1f01ecc..a1b41510f1e1c 100644 --- a/compiler/rustc_mir_build/src/builder/expr/as_rvalue.rs +++ b/compiler/rustc_mir_build/src/builder/expr/as_rvalue.rs @@ -11,8 +11,7 @@ use rustc_middle::ty::adjustment::PointerCoercion; use rustc_middle::ty::cast::{CastTy, mir_cast_kind}; use rustc_middle::ty::util::IntTypeExt; use rustc_middle::ty::{self, Ty, UpvarArgs}; -use rustc_span::source_map::Spanned; -use rustc_span::{DUMMY_SP, Span}; +use rustc_span::{DUMMY_SP, Span, Spanned}; use tracing::debug; use crate::builder::expr::as_place::PlaceBase; diff --git a/compiler/rustc_mir_build/src/builder/expr/into.rs b/compiler/rustc_mir_build/src/builder/expr/into.rs index 24d184121ffd0..446b2939e3705 100644 --- a/compiler/rustc_mir_build/src/builder/expr/into.rs +++ b/compiler/rustc_mir_build/src/builder/expr/into.rs @@ -10,8 +10,7 @@ use rustc_middle::mir::*; use rustc_middle::span_bug; use rustc_middle::thir::*; use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, Ty}; -use rustc_span::source_map::Spanned; -use rustc_span::{DUMMY_SP, sym}; +use rustc_span::{DUMMY_SP, Spanned, sym}; use rustc_trait_selection::infer::InferCtxtExt; use tracing::{debug, instrument}; diff --git a/compiler/rustc_mir_build/src/builder/expr/stmt.rs b/compiler/rustc_mir_build/src/builder/expr/stmt.rs index 3d603afbaa4a7..99e16d182a97d 100644 --- a/compiler/rustc_mir_build/src/builder/expr/stmt.rs +++ b/compiler/rustc_mir_build/src/builder/expr/stmt.rs @@ -2,7 +2,7 @@ use rustc_middle::middle::region::{self, TempLifetime}; use rustc_middle::mir::*; use rustc_middle::span_bug; use rustc_middle::thir::*; -use rustc_span::source_map::Spanned; +use rustc_span::Spanned; use tracing::debug; use crate::builder::scope::{BreakableTarget, LintLevel}; diff --git a/compiler/rustc_mir_build/src/builder/matches/test.rs b/compiler/rustc_mir_build/src/builder/matches/test.rs index 5c3173a7b1488..d8911870d3ad4 100644 --- a/compiler/rustc_mir_build/src/builder/matches/test.rs +++ b/compiler/rustc_mir_build/src/builder/matches/test.rs @@ -14,8 +14,7 @@ use rustc_middle::mir::*; use rustc_middle::ty::util::IntTypeExt; use rustc_middle::ty::{self, GenericArg, Ty, TyCtxt}; use rustc_span::def_id::DefId; -use rustc_span::source_map::Spanned; -use rustc_span::{DUMMY_SP, Span, Symbol, sym}; +use rustc_span::{DUMMY_SP, Span, Spanned, Symbol, sym}; use tracing::{debug, instrument}; use crate::builder::Builder; diff --git a/compiler/rustc_mir_build/src/builder/scope.rs b/compiler/rustc_mir_build/src/builder/scope.rs index b10df60e0f75b..91610e768d012 100644 --- a/compiler/rustc_mir_build/src/builder/scope.rs +++ b/compiler/rustc_mir_build/src/builder/scope.rs @@ -94,8 +94,7 @@ use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt, ValTree}; use rustc_middle::{bug, span_bug}; use rustc_pattern_analysis::rustc::RustcPatCtxt; use rustc_session::lint::Level; -use rustc_span::source_map::Spanned; -use rustc_span::{DUMMY_SP, Span}; +use rustc_span::{DUMMY_SP, Span, Spanned}; use tracing::{debug, instrument}; use super::matches::BuiltMatchTree; diff --git a/compiler/rustc_mir_build/src/thir/cx/expr.rs b/compiler/rustc_mir_build/src/thir/cx/expr.rs index c5eeb8b1aa856..c646b0fc45ea1 100644 --- a/compiler/rustc_mir_build/src/thir/cx/expr.rs +++ b/compiler/rustc_mir_build/src/thir/cx/expr.rs @@ -143,6 +143,19 @@ impl<'tcx> ThirBuildCx<'tcx> { adjust_span(&mut expr); ExprKind::Deref { arg: self.thir.exprs.push(expr) } } + Adjust::Deref(DerefAdjustKind::Pin) => { + adjust_span(&mut expr); + // pointer = ($expr).pointer + let pin_ty = expr.ty.pinned_ty().expect("Deref(Pin) with non-Pin type"); + let pointer_target = ExprKind::Field { + lhs: self.thir.exprs.push(expr), + variant_index: FIRST_VARIANT, + name: FieldIdx::ZERO, + }; + let expr = Expr { temp_scope_id, ty: pin_ty, span, kind: pointer_target }; + // expr = *pointer + ExprKind::Deref { arg: self.thir.exprs.push(expr) } + } Adjust::Deref(DerefAdjustKind::Overloaded(deref)) => { // We don't need to do call adjust_span here since // deref coercions always start with a built-in deref. @@ -177,6 +190,37 @@ impl<'tcx> ThirBuildCx<'tcx> { Adjust::Borrow(AutoBorrow::RawPtr(mutability)) => { ExprKind::RawBorrow { mutability, arg: self.thir.exprs.push(expr) } } + Adjust::Borrow(AutoBorrow::Pin(mutbl)) => { + // expr = &pin (mut|const|) arget + let borrow_kind = match mutbl { + hir::Mutability::Mut => BorrowKind::Mut { kind: mir::MutBorrowKind::Default }, + hir::Mutability::Not => BorrowKind::Shared, + }; + let new_pin_target = + Ty::new_ref(self.tcx, self.tcx.lifetimes.re_erased, expr.ty, mutbl); + let arg = self.thir.exprs.push(expr); + let expr = self.thir.exprs.push(Expr { + temp_scope_id, + ty: new_pin_target, + span, + kind: ExprKind::Borrow { borrow_kind, arg }, + }); + + // kind = Pin { pointer } + let pin_did = self.tcx.require_lang_item(rustc_hir::LangItem::Pin, span); + let args = self.tcx.mk_args(&[new_pin_target.into()]); + let kind = ExprKind::Adt(Box::new(AdtExpr { + adt_def: self.tcx.adt_def(pin_did), + variant_index: FIRST_VARIANT, + args, + fields: Box::new([FieldExpr { name: FieldIdx::ZERO, expr }]), + user_ty: None, + base: AdtExprBase::None, + })); + + debug!(?kind); + kind + } Adjust::ReborrowPin(mutbl) => { debug!("apply ReborrowPin adjustment"); // Rewrite `$expr` as `Pin { __pointer: &(mut)? *($expr).__pointer }` diff --git a/compiler/rustc_mir_transform/src/coroutine.rs b/compiler/rustc_mir_transform/src/coroutine.rs index c83b10a5e583a..332196e3afee7 100644 --- a/compiler/rustc_mir_transform/src/coroutine.rs +++ b/compiler/rustc_mir_transform/src/coroutine.rs @@ -83,8 +83,7 @@ use rustc_mir_dataflow::{ Analysis, Results, ResultsCursor, ResultsVisitor, visit_reachable_results, }; use rustc_span::def_id::{DefId, LocalDefId}; -use rustc_span::source_map::dummy_spanned; -use rustc_span::{DUMMY_SP, Span}; +use rustc_span::{DUMMY_SP, Span, dummy_spanned}; use rustc_trait_selection::error_reporting::InferCtxtErrorExt; use rustc_trait_selection::infer::TyCtxtInferExt as _; use rustc_trait_selection::traits::{ObligationCause, ObligationCauseCode, ObligationCtxt}; diff --git a/compiler/rustc_mir_transform/src/elaborate_drop.rs b/compiler/rustc_mir_transform/src/elaborate_drop.rs index e17629215b796..1fe745a5d5197 100644 --- a/compiler/rustc_mir_transform/src/elaborate_drop.rs +++ b/compiler/rustc_mir_transform/src/elaborate_drop.rs @@ -9,8 +9,7 @@ use rustc_middle::ty::adjustment::PointerCoercion; use rustc_middle::ty::util::IntTypeExt; use rustc_middle::ty::{self, GenericArg, GenericArgsRef, Ty, TyCtxt}; use rustc_middle::{bug, span_bug, traits}; -use rustc_span::DUMMY_SP; -use rustc_span::source_map::{Spanned, dummy_spanned}; +use rustc_span::{DUMMY_SP, Spanned, dummy_spanned}; use tracing::{debug, instrument}; use crate::patch::MirPatch; diff --git a/compiler/rustc_mir_transform/src/function_item_references.rs b/compiler/rustc_mir_transform/src/function_item_references.rs index 2ae8f43cf6c81..0216e0d4cfbc5 100644 --- a/compiler/rustc_mir_transform/src/function_item_references.rs +++ b/compiler/rustc_mir_transform/src/function_item_references.rs @@ -5,8 +5,7 @@ use rustc_middle::mir::visit::Visitor; use rustc_middle::mir::*; use rustc_middle::ty::{self, EarlyBinder, GenericArgsRef, Ty, TyCtxt}; use rustc_session::lint::builtin::FUNCTION_ITEM_REFERENCES; -use rustc_span::source_map::Spanned; -use rustc_span::{Span, sym}; +use rustc_span::{Span, Spanned, sym}; use crate::errors; diff --git a/compiler/rustc_mir_transform/src/inline.rs b/compiler/rustc_mir_transform/src/inline.rs index 9069f279e9811..afcea3236dbda 100644 --- a/compiler/rustc_mir_transform/src/inline.rs +++ b/compiler/rustc_mir_transform/src/inline.rs @@ -15,7 +15,7 @@ use rustc_middle::mir::visit::*; use rustc_middle::mir::*; use rustc_middle::ty::{self, Instance, InstanceKind, Ty, TyCtxt, TypeFlags, TypeVisitableExt}; use rustc_session::config::{DebugInfo, OptLevel}; -use rustc_span::source_map::Spanned; +use rustc_span::Spanned; use tracing::{debug, instrument, trace, trace_span}; use crate::cost_checker::{CostChecker, is_call_like}; diff --git a/compiler/rustc_mir_transform/src/lib.rs b/compiler/rustc_mir_transform/src/lib.rs index 6bf4221d9ab78..094aae499c4b4 100644 --- a/compiler/rustc_mir_transform/src/lib.rs +++ b/compiler/rustc_mir_transform/src/lib.rs @@ -27,8 +27,7 @@ use rustc_middle::mir::{ use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt}; use rustc_middle::util::Providers; use rustc_middle::{bug, query, span_bug}; -use rustc_span::source_map::Spanned; -use rustc_span::{DUMMY_SP, sym}; +use rustc_span::{DUMMY_SP, Spanned, sym}; use tracing::debug; #[macro_use] diff --git a/compiler/rustc_mir_transform/src/mentioned_items.rs b/compiler/rustc_mir_transform/src/mentioned_items.rs index a9f2c32171c39..c98ca2f4da52d 100644 --- a/compiler/rustc_mir_transform/src/mentioned_items.rs +++ b/compiler/rustc_mir_transform/src/mentioned_items.rs @@ -3,7 +3,7 @@ use rustc_middle::mir::{self, Location, MentionedItem}; use rustc_middle::ty::adjustment::PointerCoercion; use rustc_middle::ty::{self, TyCtxt}; use rustc_session::Session; -use rustc_span::source_map::Spanned; +use rustc_span::Spanned; pub(super) struct MentionedItems; diff --git a/compiler/rustc_mir_transform/src/promote_consts.rs b/compiler/rustc_mir_transform/src/promote_consts.rs index 3f5a630a21749..a47b3ce64ed23 100644 --- a/compiler/rustc_mir_transform/src/promote_consts.rs +++ b/compiler/rustc_mir_transform/src/promote_consts.rs @@ -23,8 +23,7 @@ use rustc_middle::mir::visit::{MutVisitor, MutatingUseContext, PlaceContext, Vis use rustc_middle::mir::*; use rustc_middle::ty::{self, GenericArgs, List, Ty, TyCtxt, TypeVisitableExt}; use rustc_middle::{bug, mir, span_bug}; -use rustc_span::Span; -use rustc_span::source_map::Spanned; +use rustc_span::{Span, Spanned}; use tracing::{debug, instrument}; /// A `MirPass` for promotion. diff --git a/compiler/rustc_mir_transform/src/shim.rs b/compiler/rustc_mir_transform/src/shim.rs index 1abbfed1a8222..4fd0629befecf 100644 --- a/compiler/rustc_mir_transform/src/shim.rs +++ b/compiler/rustc_mir_transform/src/shim.rs @@ -12,8 +12,7 @@ use rustc_middle::ty::{ self, CoroutineArgs, CoroutineArgsExt, EarlyBinder, GenericArgs, Ty, TyCtxt, }; use rustc_middle::{bug, span_bug}; -use rustc_span::source_map::{Spanned, dummy_spanned}; -use rustc_span::{DUMMY_SP, Span}; +use rustc_span::{DUMMY_SP, Span, Spanned, dummy_spanned}; use tracing::{debug, instrument}; use crate::deref_separator::deref_finder; diff --git a/compiler/rustc_monomorphize/src/collector.rs b/compiler/rustc_monomorphize/src/collector.rs index 3aa55cc8eb9fb..e56c6dc32ff17 100644 --- a/compiler/rustc_monomorphize/src/collector.rs +++ b/compiler/rustc_monomorphize/src/collector.rs @@ -236,8 +236,7 @@ use rustc_middle::ty::{ use rustc_middle::util::Providers; use rustc_middle::{bug, span_bug}; use rustc_session::config::{DebugInfo, EntryFnType}; -use rustc_span::source_map::{Spanned, dummy_spanned, respan}; -use rustc_span::{DUMMY_SP, Span}; +use rustc_span::{DUMMY_SP, Span, Spanned, dummy_spanned, respan}; use tracing::{debug, instrument, trace}; use crate::collector::autodiff::collect_autodiff_fn; diff --git a/compiler/rustc_monomorphize/src/mono_checks/move_check.rs b/compiler/rustc_monomorphize/src/mono_checks/move_check.rs index a8f3104c0d98d..a24b0443d39c9 100644 --- a/compiler/rustc_monomorphize/src/mono_checks/move_check.rs +++ b/compiler/rustc_monomorphize/src/mono_checks/move_check.rs @@ -6,8 +6,7 @@ use rustc_middle::mir::visit::Visitor as MirVisitor; use rustc_middle::mir::{self, Location, traversal}; use rustc_middle::ty::{self, AssocTag, Instance, Ty, TyCtxt, TypeFoldable}; use rustc_session::lint::builtin::LARGE_ASSIGNMENTS; -use rustc_span::source_map::Spanned; -use rustc_span::{Ident, Span, sym}; +use rustc_span::{Ident, Span, Spanned, sym}; use tracing::{debug, trace}; use crate::errors::LargeAssignmentsLint; diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 284e651d94ad0..47ac91feefd4b 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -16,9 +16,8 @@ use rustc_errors::{ pluralize, }; use rustc_session::errors::ExprParenthesesNeeded; -use rustc_span::source_map::Spanned; use rustc_span::symbol::used_keywords; -use rustc_span::{BytePos, DUMMY_SP, Ident, Span, SpanSnippetError, Symbol, kw, sym}; +use rustc_span::{BytePos, DUMMY_SP, Ident, Span, SpanSnippetError, Spanned, Symbol, kw, sym}; use thin_vec::{ThinVec, thin_vec}; use tracing::{debug, trace}; diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 4297086983b40..adfc68f4bb22a 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -25,8 +25,7 @@ use rustc_session::errors::{ExprParenthesesNeeded, report_lit_error}; use rustc_session::lint::BuiltinLintDiag; use rustc_session::lint::builtin::BREAK_WITH_LABEL_AND_LOOP; use rustc_span::edition::Edition; -use rustc_span::source_map::{self, Spanned}; -use rustc_span::{BytePos, ErrorGuaranteed, Ident, Pos, Span, Symbol, kw, sym}; +use rustc_span::{BytePos, ErrorGuaranteed, Ident, Pos, Span, Spanned, Symbol, kw, respan, sym}; use thin_vec::{ThinVec, thin_vec}; use tracing::instrument; @@ -296,12 +295,12 @@ impl<'a> Parser<'a> { let span = self.mk_expr_sp(&lhs, lhs_span, op_span, rhs.span); lhs = match op { AssocOp::Binary(ast_op) => { - let binary = self.mk_binary(source_map::respan(cur_op_span, ast_op), lhs, rhs); + let binary = self.mk_binary(respan(cur_op_span, ast_op), lhs, rhs); self.mk_expr(span, binary) } AssocOp::Assign => self.mk_expr(span, ExprKind::Assign(lhs, rhs, cur_op_span)), AssocOp::AssignOp(aop) => { - let aopexpr = self.mk_assign_op(source_map::respan(cur_op_span, aop), lhs, rhs); + let aopexpr = self.mk_assign_op(respan(cur_op_span, aop), lhs, rhs); self.mk_expr(span, aopexpr) } AssocOp::Cast | AssocOp::Range(_) => { @@ -409,7 +408,7 @@ impl<'a> Parser<'a> { } _ => return None, }; - Some(source_map::respan(span, op)) + Some(respan(span, op)) } /// Checks if this expression is a successfully parsed statement. diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index db5be5feaeb6e..0f4927432f6fa 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -13,7 +13,7 @@ use rustc_errors::{Applicability, PResult, StashKey, msg, struct_span_code_err}; use rustc_session::lint::builtin::VARARGS_WITHOUT_PATTERN; use rustc_span::edit_distance::edit_distance; use rustc_span::edition::Edition; -use rustc_span::{DUMMY_SP, ErrorGuaranteed, Ident, Span, Symbol, kw, source_map, sym}; +use rustc_span::{DUMMY_SP, ErrorGuaranteed, Ident, Span, Symbol, kw, respan, sym}; use thin_vec::{ThinVec, thin_vec}; use tracing::debug; @@ -3557,7 +3557,7 @@ impl<'a> Parser<'a> { _ => return Ok(None), }; - let eself = source_map::respan(eself_lo.to(eself_hi), eself); + let eself = respan(eself_lo.to(eself_hi), eself); Ok(Some(Param::from_self(AttrVec::default(), eself, eself_ident))) } diff --git a/compiler/rustc_parse/src/parser/pat.rs b/compiler/rustc_parse/src/parser/pat.rs index 528b69abbf1aa..7ee7781f6be0a 100644 --- a/compiler/rustc_parse/src/parser/pat.rs +++ b/compiler/rustc_parse/src/parser/pat.rs @@ -12,8 +12,7 @@ use rustc_ast::{ use rustc_ast_pretty::pprust; use rustc_errors::{Applicability, Diag, DiagArgValue, PResult, StashKey}; use rustc_session::errors::ExprParenthesesNeeded; -use rustc_span::source_map::{Spanned, respan}; -use rustc_span::{BytePos, ErrorGuaranteed, Ident, Span, kw, sym}; +use rustc_span::{BytePos, ErrorGuaranteed, Ident, Span, Spanned, kw, respan, sym}; use thin_vec::{ThinVec, thin_vec}; use super::{ForceCollect, Parser, PathStyle, Restrictions, Trailing, UsePreAttrPos}; diff --git a/compiler/rustc_passes/src/abi_test.rs b/compiler/rustc_passes/src/abi_test.rs index 3da0978e5ff86..7d1ee1ffbc364 100644 --- a/compiler/rustc_passes/src/abi_test.rs +++ b/compiler/rustc_passes/src/abi_test.rs @@ -5,8 +5,7 @@ use rustc_hir::find_attr; use rustc_middle::span_bug; use rustc_middle::ty::layout::{FnAbiError, LayoutError}; use rustc_middle::ty::{self, GenericArgs, Instance, Ty, TyCtxt}; -use rustc_span::Span; -use rustc_span::source_map::Spanned; +use rustc_span::{Span, Spanned}; use rustc_target::callconv::FnAbi; use super::layout_test::ensure_wf; diff --git a/compiler/rustc_passes/src/layout_test.rs b/compiler/rustc_passes/src/layout_test.rs index 68834cf7d55af..f4f5ae6c1683f 100644 --- a/compiler/rustc_passes/src/layout_test.rs +++ b/compiler/rustc_passes/src/layout_test.rs @@ -6,8 +6,7 @@ use rustc_hir::find_attr; use rustc_middle::span_bug; use rustc_middle::ty::layout::{HasTyCtxt, HasTypingEnv, LayoutError, LayoutOfHelpers}; use rustc_middle::ty::{self, Ty, TyCtxt}; -use rustc_span::Span; -use rustc_span::source_map::Spanned; +use rustc_span::{Span, Spanned}; use rustc_trait_selection::error_reporting::InferCtxtErrorExt; use rustc_trait_selection::infer::TyCtxtInferExt; use rustc_trait_selection::traits; diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index 1b1198af41c0e..12cc5649c41dd 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -32,8 +32,10 @@ use rustc_session::utils::was_invoked_from_cargo; use rustc_span::edit_distance::find_best_match_for_name; use rustc_span::edition::Edition; use rustc_span::hygiene::MacroKind; -use rustc_span::source_map::{SourceMap, Spanned}; -use rustc_span::{BytePos, Ident, RemapPathScopeComponents, Span, Symbol, SyntaxContext, kw, sym}; +use rustc_span::source_map::SourceMap; +use rustc_span::{ + BytePos, Ident, RemapPathScopeComponents, Span, Spanned, Symbol, SyntaxContext, kw, sym, +}; use thin_vec::{ThinVec, thin_vec}; use tracing::{debug, instrument}; diff --git a/compiler/rustc_resolve/src/errors.rs b/compiler/rustc_resolve/src/errors.rs index 63ffdbc37f7d1..7ab85e5a205f8 100644 --- a/compiler/rustc_resolve/src/errors.rs +++ b/compiler/rustc_resolve/src/errors.rs @@ -5,8 +5,7 @@ use rustc_errors::{ EmissionGuarantee, IntoDiagArg, Level, MultiSpan, Subdiagnostic, msg, }; use rustc_macros::{Diagnostic, Subdiagnostic}; -use rustc_span::source_map::Spanned; -use rustc_span::{Ident, Span, Symbol}; +use rustc_span::{Ident, Span, Spanned, Symbol}; use crate::Res; use crate::late::PatternSource; diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index 1e2daff6d97cb..453fe9d7a8e0e 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -37,8 +37,7 @@ use rustc_middle::{bug, span_bug}; use rustc_session::config::{CrateType, ResolveDocLinks}; use rustc_session::lint; use rustc_session::parse::feature_err; -use rustc_span::source_map::{Spanned, respan}; -use rustc_span::{BytePos, DUMMY_SP, Ident, Span, Symbol, kw, sym}; +use rustc_span::{BytePos, DUMMY_SP, Ident, Span, Spanned, Symbol, kw, respan, sym}; use smallvec::{SmallVec, smallvec}; use thin_vec::ThinVec; use tracing::{debug, instrument, trace}; diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index a1d43d9863849..d7d2ecb5bbe79 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -90,6 +90,20 @@ use sha2::Sha256; #[cfg(test)] mod tests; +#[derive(Clone, Encodable, Decodable, Debug, Copy, PartialEq, Hash, HashStable_Generic)] +pub struct Spanned { + pub node: T, + pub span: Span, +} + +pub fn respan(sp: Span, t: T) -> Spanned { + Spanned { node: t, span: sp } +} + +pub fn dummy_spanned(t: T) -> Spanned { + respan(DUMMY_SP, t) +} + /// Per-session global variables: this struct is stored in thread-local storage /// in such a way that it is accessible without any kind of handle to all /// threads within the compilation session, but is not accessible outside the diff --git a/compiler/rustc_span/src/source_map.rs b/compiler/rustc_span/src/source_map.rs index 35694be9e492b..ec335e7b43390 100644 --- a/compiler/rustc_span/src/source_map.rs +++ b/compiler/rustc_span/src/source_map.rs @@ -15,7 +15,6 @@ use std::{fs, path}; use rustc_data_structures::sync::{IntoDynSyncSend, MappedReadGuard, ReadGuard, RwLock}; use rustc_data_structures::unhash::UnhashMap; -use rustc_macros::{Decodable, Encodable}; use tracing::{debug, instrument, trace}; use crate::*; @@ -74,20 +73,6 @@ mod monotonic { impl !DerefMut for MonotonicVec {} } -#[derive(Clone, Encodable, Decodable, Debug, Copy, PartialEq, Hash, HashStable_Generic)] -pub struct Spanned { - pub node: T, - pub span: Span, -} - -pub fn respan(sp: Span, t: T) -> Spanned { - Spanned { node: t, span: sp } -} - -pub fn dummy_spanned(t: T) -> Spanned { - respan(DUMMY_SP, t) -} - // _____________________________________________________________________________ // SourceFile, MultiByteChar, FileName, FileLines // diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs index adb2d2c185cac..0fde0009debe7 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs @@ -272,18 +272,21 @@ impl Trait for X { values.found, values.expected, ) }; - if !(self.suggest_constraining_opaque_associated_type( - diag, - msg, - proj_ty, - values.expected, - ) || self.suggest_constraint( - diag, - &msg, - body_owner_def_id, - proj_ty, - values.expected, - )) { + let suggested_projection_constraint = proj_ty.kind(tcx) + == ty::AliasTyKind::Projection + && (self.suggest_constraining_opaque_associated_type( + diag, + msg, + proj_ty, + values.expected, + ) || self.suggest_constraint( + diag, + &msg, + body_owner_def_id, + proj_ty, + values.expected, + )); + if !suggested_projection_constraint { diag.help(msg()); diag.note( "for more information, visit \ diff --git a/src/tools/clippy/clippy_lints/src/bool_comparison.rs b/src/tools/clippy/clippy_lints/src/bool_comparison.rs index 722095909a6fe..feeefefc3b8f1 100644 --- a/src/tools/clippy/clippy_lints/src/bool_comparison.rs +++ b/src/tools/clippy/clippy_lints/src/bool_comparison.rs @@ -6,8 +6,7 @@ use rustc_errors::Applicability; use rustc_hir::{BinOpKind, Expr, ExprKind}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::declare_lint_pass; -use rustc_span::Span; -use rustc_span::source_map::Spanned; +use rustc_span::{Span, Spanned}; declare_clippy_lint! { /// ### What it does diff --git a/src/tools/clippy/clippy_lints/src/casts/manual_dangling_ptr.rs b/src/tools/clippy/clippy_lints/src/casts/manual_dangling_ptr.rs index 56779e8ce3d95..55b0945f0962b 100644 --- a/src/tools/clippy/clippy_lints/src/casts/manual_dangling_ptr.rs +++ b/src/tools/clippy/clippy_lints/src/casts/manual_dangling_ptr.rs @@ -6,7 +6,7 @@ use rustc_ast::LitKind; use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind, GenericArg, Mutability, QPath, Ty, TyKind}; use rustc_lint::LateContext; -use rustc_span::source_map::Spanned; +use rustc_span::Spanned; use super::MANUAL_DANGLING_PTR; diff --git a/src/tools/clippy/clippy_lints/src/floating_point_arithmetic/custom_abs.rs b/src/tools/clippy/clippy_lints/src/floating_point_arithmetic/custom_abs.rs index d12a32e158814..6578cf20e6378 100644 --- a/src/tools/clippy/clippy_lints/src/floating_point_arithmetic/custom_abs.rs +++ b/src/tools/clippy/clippy_lints/src/floating_point_arithmetic/custom_abs.rs @@ -7,7 +7,7 @@ use rustc_errors::Applicability; use rustc_hir::{BinOpKind, Expr, ExprKind, UnOp}; use rustc_lint::LateContext; use rustc_span::SyntaxContext; -use rustc_span::source_map::Spanned; +use rustc_span::Spanned; use super::SUBOPTIMAL_FLOPS; diff --git a/src/tools/clippy/clippy_lints/src/floating_point_arithmetic/expm1.rs b/src/tools/clippy/clippy_lints/src/floating_point_arithmetic/expm1.rs index 9a4c975693087..bf376a51fec93 100644 --- a/src/tools/clippy/clippy_lints/src/floating_point_arithmetic/expm1.rs +++ b/src/tools/clippy/clippy_lints/src/floating_point_arithmetic/expm1.rs @@ -6,7 +6,7 @@ use clippy_utils::sym; use rustc_errors::Applicability; use rustc_hir::{BinOpKind, Expr, ExprKind}; use rustc_lint::LateContext; -use rustc_span::source_map::Spanned; +use rustc_span::Spanned; use super::IMPRECISE_FLOPS; diff --git a/src/tools/clippy/clippy_lints/src/floating_point_arithmetic/hypot.rs b/src/tools/clippy/clippy_lints/src/floating_point_arithmetic/hypot.rs index 49f8ba4bf8256..8d3bd8084db8f 100644 --- a/src/tools/clippy/clippy_lints/src/floating_point_arithmetic/hypot.rs +++ b/src/tools/clippy/clippy_lints/src/floating_point_arithmetic/hypot.rs @@ -6,7 +6,7 @@ use clippy_utils::{eq_expr_value, sym}; use rustc_errors::Applicability; use rustc_hir::{BinOpKind, Expr, ExprKind, PathSegment}; use rustc_lint::LateContext; -use rustc_span::source_map::Spanned; +use rustc_span::Spanned; use super::IMPRECISE_FLOPS; diff --git a/src/tools/clippy/clippy_lints/src/floating_point_arithmetic/ln1p.rs b/src/tools/clippy/clippy_lints/src/floating_point_arithmetic/ln1p.rs index 4c9aa96b5042a..f37737c71aee1 100644 --- a/src/tools/clippy/clippy_lints/src/floating_point_arithmetic/ln1p.rs +++ b/src/tools/clippy/clippy_lints/src/floating_point_arithmetic/ln1p.rs @@ -4,7 +4,7 @@ use clippy_utils::diagnostics::span_lint_and_then; use rustc_errors::Applicability; use rustc_hir::{BinOpKind, Expr, ExprKind}; use rustc_lint::LateContext; -use rustc_span::source_map::Spanned; +use rustc_span::Spanned; use super::IMPRECISE_FLOPS; diff --git a/src/tools/clippy/clippy_lints/src/floating_point_arithmetic/log_division.rs b/src/tools/clippy/clippy_lints/src/floating_point_arithmetic/log_division.rs index e3419ffad72a5..947935369de16 100644 --- a/src/tools/clippy/clippy_lints/src/floating_point_arithmetic/log_division.rs +++ b/src/tools/clippy/clippy_lints/src/floating_point_arithmetic/log_division.rs @@ -4,7 +4,7 @@ use clippy_utils::{eq_expr_value, sym}; use rustc_errors::Applicability; use rustc_hir::{BinOpKind, Expr, ExprKind, PathSegment}; use rustc_lint::LateContext; -use rustc_span::source_map::Spanned; +use rustc_span::Spanned; use super::SUBOPTIMAL_FLOPS; diff --git a/src/tools/clippy/clippy_lints/src/floating_point_arithmetic/mul_add.rs b/src/tools/clippy/clippy_lints/src/floating_point_arithmetic/mul_add.rs index e0a6498f62fd4..888d5b7b762a0 100644 --- a/src/tools/clippy/clippy_lints/src/floating_point_arithmetic/mul_add.rs +++ b/src/tools/clippy/clippy_lints/src/floating_point_arithmetic/mul_add.rs @@ -5,7 +5,7 @@ use rustc_ast::AssignOpKind; use rustc_errors::Applicability; use rustc_hir::{BinOpKind, Expr, ExprKind, PathSegment}; use rustc_lint::LateContext; -use rustc_span::source_map::Spanned; +use rustc_span::Spanned; use super::SUBOPTIMAL_FLOPS; diff --git a/src/tools/clippy/clippy_lints/src/floating_point_arithmetic/powi.rs b/src/tools/clippy/clippy_lints/src/floating_point_arithmetic/powi.rs index a61a2a82c0237..b0b65b357d7ab 100644 --- a/src/tools/clippy/clippy_lints/src/floating_point_arithmetic/powi.rs +++ b/src/tools/clippy/clippy_lints/src/floating_point_arithmetic/powi.rs @@ -6,7 +6,7 @@ use clippy_utils::{get_parent_expr, sym}; use rustc_errors::Applicability; use rustc_hir::{BinOpKind, Expr, ExprKind, PathSegment}; use rustc_lint::LateContext; -use rustc_span::source_map::Spanned; +use rustc_span::Spanned; use super::SUBOPTIMAL_FLOPS; diff --git a/src/tools/clippy/clippy_lints/src/floating_point_arithmetic/radians.rs b/src/tools/clippy/clippy_lints/src/floating_point_arithmetic/radians.rs index 2021f00a97e8d..8480626133fdd 100644 --- a/src/tools/clippy/clippy_lints/src/floating_point_arithmetic/radians.rs +++ b/src/tools/clippy/clippy_lints/src/floating_point_arithmetic/radians.rs @@ -6,7 +6,7 @@ use rustc_ast::ast; use rustc_errors::Applicability; use rustc_hir::{BinOpKind, Expr, ExprKind}; use rustc_lint::LateContext; -use rustc_span::source_map::Spanned; +use rustc_span::Spanned; use std::f32::consts as f32_consts; use std::f64::consts as f64_consts; diff --git a/src/tools/clippy/clippy_lints/src/len_zero.rs b/src/tools/clippy/clippy_lints/src/len_zero.rs index d683abd3d33b3..f906bba423b35 100644 --- a/src/tools/clippy/clippy_lints/src/len_zero.rs +++ b/src/tools/clippy/clippy_lints/src/len_zero.rs @@ -13,8 +13,7 @@ use rustc_hir::{BinOpKind, Expr, ExprKind, PatExprKind, PatKind, RustcVersion, S use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::ty::{self, Ty}; use rustc_session::impl_lint_pass; -use rustc_span::source_map::Spanned; -use rustc_span::{Span, Symbol}; +use rustc_span::{Span, Spanned, Symbol}; declare_clippy_lint! { /// ### What it does diff --git a/src/tools/clippy/clippy_lints/src/loops/manual_slice_fill.rs b/src/tools/clippy/clippy_lints/src/loops/manual_slice_fill.rs index a2ff60a3d8ac8..5d09f755bcd15 100644 --- a/src/tools/clippy/clippy_lints/src/loops/manual_slice_fill.rs +++ b/src/tools/clippy/clippy_lints/src/loops/manual_slice_fill.rs @@ -13,8 +13,7 @@ use rustc_hir::QPath::Resolved; use rustc_hir::def::Res; use rustc_hir::{Expr, ExprKind, Pat}; use rustc_lint::LateContext; -use rustc_span::source_map::Spanned; -use rustc_span::sym; +use rustc_span::{Spanned, sym}; use super::MANUAL_SLICE_FILL; diff --git a/src/tools/clippy/clippy_lints/src/loops/utils.rs b/src/tools/clippy/clippy_lints/src/loops/utils.rs index 2c37e2679d972..86f4e606a176a 100644 --- a/src/tools/clippy/clippy_lints/src/loops/utils.rs +++ b/src/tools/clippy/clippy_lints/src/loops/utils.rs @@ -8,7 +8,7 @@ use rustc_hir::{AssignOpKind, BorrowKind, Expr, ExprKind, HirId, HirIdMap, LetSt use rustc_lint::LateContext; use rustc_middle::hir::nested_filter; use rustc_middle::ty::{self, Ty}; -use rustc_span::source_map::Spanned; +use rustc_span::Spanned; use rustc_span::symbol::{Symbol, sym}; #[derive(Debug, PartialEq, Eq)] diff --git a/src/tools/clippy/clippy_lints/src/manual_strip.rs b/src/tools/clippy/clippy_lints/src/manual_strip.rs index 5261632445969..e0a60e3747a0f 100644 --- a/src/tools/clippy/clippy_lints/src/manual_strip.rs +++ b/src/tools/clippy/clippy_lints/src/manual_strip.rs @@ -15,8 +15,7 @@ use rustc_hir::{BinOpKind, BorrowKind, Expr, ExprKind, Node, PatKind}; use rustc_lint::{LateContext, LateLintPass, LintContext as _}; use rustc_middle::ty; use rustc_session::impl_lint_pass; -use rustc_span::source_map::Spanned; -use rustc_span::{Symbol, SyntaxContext}; +use rustc_span::{Spanned, Symbol, SyntaxContext}; use std::iter; declare_clippy_lint! { diff --git a/src/tools/clippy/clippy_lints/src/matches/match_like_matches.rs b/src/tools/clippy/clippy_lints/src/matches/match_like_matches.rs index 347560b14eeac..3a16c2ed8790b 100644 --- a/src/tools/clippy/clippy_lints/src/matches/match_like_matches.rs +++ b/src/tools/clippy/clippy_lints/src/matches/match_like_matches.rs @@ -10,7 +10,7 @@ use rustc_errors::Applicability; use rustc_hir::{Arm, BorrowKind, Expr, ExprKind, Pat, PatKind, QPath}; use rustc_lint::{LateContext, LintContext}; use rustc_middle::ty; -use rustc_span::source_map::Spanned; +use rustc_span::Spanned; use super::MATCH_LIKE_MATCHES_MACRO; diff --git a/src/tools/clippy/clippy_lints/src/methods/case_sensitive_file_extension_comparisons.rs b/src/tools/clippy/clippy_lints/src/methods/case_sensitive_file_extension_comparisons.rs index b4b10e972f6d4..23a0046dec766 100644 --- a/src/tools/clippy/clippy_lints/src/methods/case_sensitive_file_extension_comparisons.rs +++ b/src/tools/clippy/clippy_lints/src/methods/case_sensitive_file_extension_comparisons.rs @@ -8,7 +8,7 @@ use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind, LangItem}; use rustc_lint::LateContext; use rustc_span::Span; -use rustc_span::source_map::Spanned; +use rustc_span::Spanned; use super::CASE_SENSITIVE_FILE_EXTENSION_COMPARISONS; diff --git a/src/tools/clippy/clippy_lints/src/methods/get_first.rs b/src/tools/clippy/clippy_lints/src/methods/get_first.rs index 414c4e0cd381f..cb5d1ec374fce 100644 --- a/src/tools/clippy/clippy_lints/src/methods/get_first.rs +++ b/src/tools/clippy/clippy_lints/src/methods/get_first.rs @@ -7,7 +7,7 @@ use rustc_data_structures::packed::Pu128; use rustc_errors::Applicability; use rustc_hir as hir; use rustc_lint::LateContext; -use rustc_span::source_map::Spanned; +use rustc_span::Spanned; use super::GET_FIRST; diff --git a/src/tools/clippy/clippy_lints/src/methods/get_last_with_len.rs b/src/tools/clippy/clippy_lints/src/methods/get_last_with_len.rs index 171079f52d9a5..14e40328a4168 100644 --- a/src/tools/clippy/clippy_lints/src/methods/get_last_with_len.rs +++ b/src/tools/clippy/clippy_lints/src/methods/get_last_with_len.rs @@ -5,7 +5,7 @@ use rustc_errors::Applicability; use rustc_hir::{BinOpKind, Expr, ExprKind}; use rustc_lint::LateContext; use rustc_middle::ty; -use rustc_span::source_map::Spanned; +use rustc_span::Spanned; use super::GET_LAST_WITH_LEN; diff --git a/src/tools/clippy/clippy_lints/src/methods/manual_contains.rs b/src/tools/clippy/clippy_lints/src/methods/manual_contains.rs index d2906b2776f8c..f1d399e1bbd2f 100644 --- a/src/tools/clippy/clippy_lints/src/methods/manual_contains.rs +++ b/src/tools/clippy/clippy_lints/src/methods/manual_contains.rs @@ -9,7 +9,7 @@ use rustc_hir::def::Res; use rustc_hir::{BinOpKind, Body, Expr, ExprKind, HirId, QPath}; use rustc_lint::LateContext; use rustc_middle::ty; -use rustc_span::source_map::Spanned; +use rustc_span::Spanned; use super::MANUAL_CONTAINS; diff --git a/src/tools/clippy/clippy_lints/src/methods/open_options.rs b/src/tools/clippy/clippy_lints/src/methods/open_options.rs index 1b520a9edb566..042558d645e65 100644 --- a/src/tools/clippy/clippy_lints/src/methods/open_options.rs +++ b/src/tools/clippy/clippy_lints/src/methods/open_options.rs @@ -8,7 +8,7 @@ use rustc_hir::{Expr, ExprKind}; use rustc_lint::LateContext; use rustc_middle::ty::Ty; use rustc_span::Span; -use rustc_span::source_map::Spanned; +use rustc_span::Spanned; use super::{NONSENSICAL_OPEN_OPTIONS, SUSPICIOUS_OPEN_OPTIONS}; diff --git a/src/tools/clippy/clippy_lints/src/methods/suspicious_splitn.rs b/src/tools/clippy/clippy_lints/src/methods/suspicious_splitn.rs index be1481ebb996e..98b7834178f24 100644 --- a/src/tools/clippy/clippy_lints/src/methods/suspicious_splitn.rs +++ b/src/tools/clippy/clippy_lints/src/methods/suspicious_splitn.rs @@ -3,7 +3,7 @@ use rustc_ast::LitKind; use rustc_hir::{Expr, ExprKind}; use rustc_lint::LateContext; use rustc_span::Symbol; -use rustc_span::source_map::Spanned; +use rustc_span::Spanned; use super::SUSPICIOUS_SPLITN; diff --git a/src/tools/clippy/clippy_lints/src/methods/vec_resize_to_zero.rs b/src/tools/clippy/clippy_lints/src/methods/vec_resize_to_zero.rs index 5debaab2067bd..8979f9973d6f6 100644 --- a/src/tools/clippy/clippy_lints/src/methods/vec_resize_to_zero.rs +++ b/src/tools/clippy/clippy_lints/src/methods/vec_resize_to_zero.rs @@ -5,8 +5,7 @@ use rustc_data_structures::packed::Pu128; use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind}; use rustc_lint::LateContext; -use rustc_span::source_map::Spanned; -use rustc_span::{Span, sym}; +use rustc_span::{Span, Spanned, sym}; use super::VEC_RESIZE_TO_ZERO; diff --git a/src/tools/clippy/clippy_lints/src/missing_asserts_for_indexing.rs b/src/tools/clippy/clippy_lints/src/missing_asserts_for_indexing.rs index 2773f537efc65..b5becbdeb30d9 100644 --- a/src/tools/clippy/clippy_lints/src/missing_asserts_for_indexing.rs +++ b/src/tools/clippy/clippy_lints/src/missing_asserts_for_indexing.rs @@ -15,8 +15,7 @@ use rustc_errors::{Applicability, Diag}; use rustc_hir::{Block, Body, Expr, ExprKind, UnOp}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::declare_lint_pass; -use rustc_span::source_map::Spanned; -use rustc_span::{Span, Symbol, sym}; +use rustc_span::{Span, Spanned, Symbol, sym}; declare_clippy_lint! { /// ### What it does diff --git a/src/tools/clippy/clippy_lints/src/operators/const_comparisons.rs b/src/tools/clippy/clippy_lints/src/operators/const_comparisons.rs index 56001a185771a..e5a5b46b7b2e4 100644 --- a/src/tools/clippy/clippy_lints/src/operators/const_comparisons.rs +++ b/src/tools/clippy/clippy_lints/src/operators/const_comparisons.rs @@ -7,8 +7,7 @@ use rustc_hir::{BinOpKind, Expr, ExprKind}; use rustc_lint::LateContext; use rustc_middle::ty::layout::HasTyCtxt; use rustc_middle::ty::{Ty, TypeckResults}; -use rustc_span::Span; -use rustc_span::source_map::Spanned; +use rustc_span::{Span, Spanned}; use clippy_utils::SpanlessEq; use clippy_utils::diagnostics::span_lint_and_note; diff --git a/src/tools/clippy/clippy_lints/src/operators/float_equality_without_abs.rs b/src/tools/clippy/clippy_lints/src/operators/float_equality_without_abs.rs index 97b3c0d02e84e..6c6221525b578 100644 --- a/src/tools/clippy/clippy_lints/src/operators/float_equality_without_abs.rs +++ b/src/tools/clippy/clippy_lints/src/operators/float_equality_without_abs.rs @@ -6,7 +6,7 @@ use rustc_hir::def::{DefKind, Res}; use rustc_hir::{BinOpKind, Expr, ExprKind}; use rustc_lint::LateContext; use rustc_middle::ty; -use rustc_span::source_map::Spanned; +use rustc_span::Spanned; use super::FLOAT_EQUALITY_WITHOUT_ABS; diff --git a/src/tools/clippy/clippy_lints/src/operators/manual_div_ceil.rs b/src/tools/clippy/clippy_lints/src/operators/manual_div_ceil.rs index 6751532ecbc78..a0f61f6d36c4e 100644 --- a/src/tools/clippy/clippy_lints/src/operators/manual_div_ceil.rs +++ b/src/tools/clippy/clippy_lints/src/operators/manual_div_ceil.rs @@ -9,7 +9,7 @@ use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind}; use rustc_lint::LateContext; use rustc_middle::ty::{self, Ty}; -use rustc_span::source_map::Spanned; +use rustc_span::Spanned; use super::MANUAL_DIV_CEIL; diff --git a/src/tools/clippy/clippy_lints/src/precedence.rs b/src/tools/clippy/clippy_lints/src/precedence.rs index 034fe8edc715d..1f0371c782000 100644 --- a/src/tools/clippy/clippy_lints/src/precedence.rs +++ b/src/tools/clippy/clippy_lints/src/precedence.rs @@ -5,7 +5,7 @@ use rustc_ast::ast::{BinOpKind, Expr, ExprKind}; use rustc_errors::Applicability; use rustc_lint::{EarlyContext, EarlyLintPass, Lint}; use rustc_session::declare_lint_pass; -use rustc_span::source_map::Spanned; +use rustc_span::Spanned; declare_clippy_lint! { /// ### What it does diff --git a/src/tools/clippy/clippy_lints/src/ranges.rs b/src/tools/clippy/clippy_lints/src/ranges.rs index 61087ad884577..39019c646bd5a 100644 --- a/src/tools/clippy/clippy_lints/src/ranges.rs +++ b/src/tools/clippy/clippy_lints/src/ranges.rs @@ -14,8 +14,7 @@ use rustc_hir::{BinOpKind, Expr, ExprKind, HirId, LangItem, Node}; use rustc_lint::{LateContext, LateLintPass, Lint}; use rustc_middle::ty::{self, ClauseKind, GenericArgKind, PredicatePolarity, Ty}; use rustc_session::impl_lint_pass; -use rustc_span::source_map::Spanned; -use rustc_span::{DesugaringKind, Span}; +use rustc_span::{DesugaringKind, Span, Spanned}; use std::cmp::Ordering; declare_clippy_lint! { diff --git a/src/tools/clippy/clippy_lints/src/strings.rs b/src/tools/clippy/clippy_lints/src/strings.rs index ab11c37b5041d..61b5842ea542a 100644 --- a/src/tools/clippy/clippy_lints/src/strings.rs +++ b/src/tools/clippy/clippy_lints/src/strings.rs @@ -11,7 +11,7 @@ use rustc_hir::{BinOpKind, BorrowKind, Expr, ExprKind, LangItem, Node}; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_middle::ty; use rustc_session::declare_lint_pass; -use rustc_span::source_map::Spanned; +use rustc_span::Spanned; declare_clippy_lint! { /// ### What it does diff --git a/src/tools/clippy/clippy_lints/src/suspicious_operation_groupings.rs b/src/tools/clippy/clippy_lints/src/suspicious_operation_groupings.rs index 9fa4d2c142b5b..44e35b8dc71b0 100644 --- a/src/tools/clippy/clippy_lints/src/suspicious_operation_groupings.rs +++ b/src/tools/clippy/clippy_lints/src/suspicious_operation_groupings.rs @@ -8,7 +8,7 @@ use rustc_errors::Applicability; use rustc_lint::{EarlyContext, EarlyLintPass}; use rustc_session::declare_lint_pass; use rustc_span::Span; -use rustc_span::source_map::Spanned; +use rustc_span::Spanned; use rustc_span::symbol::Ident; declare_clippy_lint! { diff --git a/src/tools/clippy/clippy_lints/src/swap.rs b/src/tools/clippy/clippy_lints/src/swap.rs index 0c78ed84adc4a..01482b2475f7c 100644 --- a/src/tools/clippy/clippy_lints/src/swap.rs +++ b/src/tools/clippy/clippy_lints/src/swap.rs @@ -14,9 +14,8 @@ use rustc_hir::{AssignOpKind, Block, Expr, ExprKind, LetStmt, PatKind, QPath, St use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_middle::ty; use rustc_session::declare_lint_pass; -use rustc_span::source_map::Spanned; use rustc_span::symbol::Ident; -use rustc_span::{Span, SyntaxContext}; +use rustc_span::{Span, Spanned, SyntaxContext}; declare_clippy_lint! { /// ### What it does diff --git a/src/tools/clippy/clippy_lints_internal/src/lint_without_lint_pass.rs b/src/tools/clippy/clippy_lints_internal/src/lint_without_lint_pass.rs index f56b6f31550ba..cab1422c3000a 100644 --- a/src/tools/clippy/clippy_lints_internal/src/lint_without_lint_pass.rs +++ b/src/tools/clippy/clippy_lints_internal/src/lint_without_lint_pass.rs @@ -12,9 +12,8 @@ use rustc_hir::{ExprKind, HirId, Item, MutTy, Mutability, Path, TyKind}; use rustc_lint::{LateContext, LateLintPass}; use rustc_middle::hir::nested_filter; use rustc_session::{declare_tool_lint, impl_lint_pass}; -use rustc_span::Span; -use rustc_span::source_map::Spanned; use rustc_span::symbol::Symbol; +use rustc_span::{Span, Spanned}; declare_tool_lint! { /// ### What it does diff --git a/src/tools/rustfmt/src/patterns.rs b/src/tools/rustfmt/src/patterns.rs index df2a8dc5c6fde..0a9ff4771b0e0 100644 --- a/src/tools/rustfmt/src/patterns.rs +++ b/src/tools/rustfmt/src/patterns.rs @@ -361,7 +361,7 @@ pub(crate) fn rewrite_range_pat( shape: Shape, lhs: &Option>, rhs: &Option>, - end_kind: &rustc_span::source_map::Spanned, + end_kind: &rustc_span::Spanned, span: Span, ) -> RewriteResult { let infix = match end_kind.node { diff --git a/src/tools/rustfmt/src/spanned.rs b/src/tools/rustfmt/src/spanned.rs index 020651e2daace..143fb1dea2223 100644 --- a/src/tools/rustfmt/src/spanned.rs +++ b/src/tools/rustfmt/src/spanned.rs @@ -1,7 +1,7 @@ use std::cmp::max; use rustc_ast::ast; -use rustc_span::{Span, source_map}; +use rustc_span::Span; use crate::macros::MacroArg; use crate::patterns::RangeOperand; @@ -18,7 +18,7 @@ impl Spanned for Box { } } -impl Spanned for source_map::Spanned { +impl Spanned for rustc_span::Spanned { fn span(&self) -> Span { self.span } diff --git a/tests/ui-fulldeps/pprust-expr-roundtrip.rs b/tests/ui-fulldeps/pprust-expr-roundtrip.rs index 8d7425186680a..84f9a09990fdd 100644 --- a/tests/ui-fulldeps/pprust-expr-roundtrip.rs +++ b/tests/ui-fulldeps/pprust-expr-roundtrip.rs @@ -38,9 +38,8 @@ use rustc_ast::mut_visit::MutVisitor; use rustc_ast::*; use rustc_ast_pretty::pprust; use rustc_session::parse::ParseSess; -use rustc_span::source_map::Spanned; use rustc_span::symbol::Ident; -use rustc_span::DUMMY_SP; +use rustc_span::{DUMMY_SP, Spanned}; use thin_vec::{thin_vec, ThinVec}; // Helper functions for building exprs diff --git a/tests/ui/associated-inherent-types/inherent-assoc-ty-mismatch-issue-153539.rs b/tests/ui/associated-inherent-types/inherent-assoc-ty-mismatch-issue-153539.rs new file mode 100644 index 0000000000000..960e19c21a2c5 --- /dev/null +++ b/tests/ui/associated-inherent-types/inherent-assoc-ty-mismatch-issue-153539.rs @@ -0,0 +1,21 @@ +//@ compile-flags: -Znext-solver=globally +#![feature(inherent_associated_types)] +#![expect(incomplete_features)] + +// Regression test for https://github.com/rust-lang/rust/issues/153539: + +struct S<'a>(&'a ()); + +impl S<'_> { + //~^ ERROR the type parameter `X` is not constrained by the impl trait, self type, or predicates + type P = (); +} + +fn ret_ref_local<'e>() -> &'e i32 { + let f: for<'a> fn(&'a i32) -> S<'a>::P = |x| _ = x; + + f(&1) + //~^ ERROR mismatched types +} + +fn main() {} diff --git a/tests/ui/associated-inherent-types/inherent-assoc-ty-mismatch-issue-153539.stderr b/tests/ui/associated-inherent-types/inherent-assoc-ty-mismatch-issue-153539.stderr new file mode 100644 index 0000000000000..74d88889223f3 --- /dev/null +++ b/tests/ui/associated-inherent-types/inherent-assoc-ty-mismatch-issue-153539.stderr @@ -0,0 +1,22 @@ +error[E0207]: the type parameter `X` is not constrained by the impl trait, self type, or predicates + --> $DIR/inherent-assoc-ty-mismatch-issue-153539.rs:9:6 + | +LL | impl S<'_> { + | ^ unconstrained type parameter + +error[E0308]: mismatched types + --> $DIR/inherent-assoc-ty-mismatch-issue-153539.rs:17:5 + | +LL | fn ret_ref_local<'e>() -> &'e i32 { + | ------- expected `&'e i32` because of return type +... +LL | f(&1) + | ^^^^^ expected `&i32`, found associated type + | + = help: consider constraining the associated type `S<'_>::P` to `&'e i32` + = note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0207, E0308. +For more information about an error, try `rustc --explain E0207`. diff --git a/tests/ui/env-macro/env-cargo-var-typo-issue-148439.rs b/tests/ui/env-macro/env-cargo-var-typo-issue-148439.rs index f859decd09ecb..f72b43bb2543c 100644 --- a/tests/ui/env-macro/env-cargo-var-typo-issue-148439.rs +++ b/tests/ui/env-macro/env-cargo-var-typo-issue-148439.rs @@ -1,4 +1,5 @@ //@ edition: 2021 +//@ compile-flags: --crate-type=lib // Regression test for issue #148439 // Ensure that when using misspelled Cargo environment variables in env!(), @@ -44,7 +45,14 @@ fn test_cargo_unknown_var() { // Cargo-prefixed but not similar to any known variable let _ = env!("CARGO_SOMETHING_TOTALLY_UNKNOWN"); //~^ ERROR environment variable `CARGO_SOMETHING_TOTALLY_UNKNOWN` not defined at compile time + //~| HELP `CARGO_SOMETHING_TOTALLY_UNKNOWN` may not be available for the current Cargo target //~| HELP Cargo sets build script variables at run time. Use `std::env::var("CARGO_SOMETHING_TOTALLY_UNKNOWN")` instead } -fn main() {} +fn test_cargo_conditional_var() { + // Only set for binairies + let _ = env!("CARGO_BIN_NAME"); + //~^ ERROR environment variable `CARGO_BIN_NAME` not defined at compile time + //~| HELP `CARGO_BIN_NAME` may not be available for the current Cargo target + //~| HELP Cargo sets build script variables at run time. Use `std::env::var("CARGO_BIN_NAME")` instead +} diff --git a/tests/ui/env-macro/env-cargo-var-typo-issue-148439.stderr b/tests/ui/env-macro/env-cargo-var-typo-issue-148439.stderr index e16c4d9a1f4c2..50a4cf1a616be 100644 --- a/tests/ui/env-macro/env-cargo-var-typo-issue-148439.stderr +++ b/tests/ui/env-macro/env-cargo-var-typo-issue-148439.stderr @@ -1,5 +1,5 @@ error: environment variable `CARGO_PACKAGE_VERSION` not defined at compile time - --> $DIR/env-cargo-var-typo-issue-148439.rs:7:13 + --> $DIR/env-cargo-var-typo-issue-148439.rs:8:13 | LL | let _ = env!("CARGO_PACKAGE_VERSION"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -7,7 +7,7 @@ LL | let _ = env!("CARGO_PACKAGE_VERSION"); = help: there is a similar Cargo environment variable: `CARGO_PKG_VERSION` error: environment variable `CARGO_PACKAGE_NAME` not defined at compile time - --> $DIR/env-cargo-var-typo-issue-148439.rs:13:13 + --> $DIR/env-cargo-var-typo-issue-148439.rs:14:13 | LL | let _ = env!("CARGO_PACKAGE_NAME"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -15,7 +15,7 @@ LL | let _ = env!("CARGO_PACKAGE_NAME"); = help: there is a similar Cargo environment variable: `CARGO_PKG_NAME` error: environment variable `CARGO_PACKAGE_AUTHORS` not defined at compile time - --> $DIR/env-cargo-var-typo-issue-148439.rs:19:13 + --> $DIR/env-cargo-var-typo-issue-148439.rs:20:13 | LL | let _ = env!("CARGO_PACKAGE_AUTHORS"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -23,7 +23,7 @@ LL | let _ = env!("CARGO_PACKAGE_AUTHORS"); = help: there is a similar Cargo environment variable: `CARGO_PKG_AUTHORS` error: environment variable `CARGO_MANIFEST_DIRECTORY` not defined at compile time - --> $DIR/env-cargo-var-typo-issue-148439.rs:25:13 + --> $DIR/env-cargo-var-typo-issue-148439.rs:26:13 | LL | let _ = env!("CARGO_MANIFEST_DIRECTORY"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -31,7 +31,7 @@ LL | let _ = env!("CARGO_MANIFEST_DIRECTORY"); = help: there is a similar Cargo environment variable: `CARGO_MANIFEST_DIR` error: environment variable `CARGO_PKG_VERSIO` not defined at compile time - --> $DIR/env-cargo-var-typo-issue-148439.rs:31:13 + --> $DIR/env-cargo-var-typo-issue-148439.rs:32:13 | LL | let _ = env!("CARGO_PKG_VERSIO"); | ^^^^^^^^^^^^^^^^^^^^^^^^ @@ -39,7 +39,7 @@ LL | let _ = env!("CARGO_PKG_VERSIO"); = help: there is a similar Cargo environment variable: `CARGO_PKG_VERSION` error: environment variable `MY_CUSTOM_VAR` not defined at compile time - --> $DIR/env-cargo-var-typo-issue-148439.rs:38:13 + --> $DIR/env-cargo-var-typo-issue-148439.rs:39:13 | LL | let _ = env!("MY_CUSTOM_VAR"); | ^^^^^^^^^^^^^^^^^^^^^ @@ -47,12 +47,22 @@ LL | let _ = env!("MY_CUSTOM_VAR"); = help: use `std::env::var("MY_CUSTOM_VAR")` to read the variable at run time error: environment variable `CARGO_SOMETHING_TOTALLY_UNKNOWN` not defined at compile time - --> $DIR/env-cargo-var-typo-issue-148439.rs:45:13 + --> $DIR/env-cargo-var-typo-issue-148439.rs:46:13 | LL | let _ = env!("CARGO_SOMETHING_TOTALLY_UNKNOWN"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | + = help: `CARGO_SOMETHING_TOTALLY_UNKNOWN` may not be available for the current Cargo target = help: Cargo sets build script variables at run time. Use `std::env::var("CARGO_SOMETHING_TOTALLY_UNKNOWN")` instead -error: aborting due to 7 previous errors +error: environment variable `CARGO_BIN_NAME` not defined at compile time + --> $DIR/env-cargo-var-typo-issue-148439.rs:54:13 + | +LL | let _ = env!("CARGO_BIN_NAME"); + | ^^^^^^^^^^^^^^^^^^^^^^ + | + = help: `CARGO_BIN_NAME` may not be available for the current Cargo target + = help: Cargo sets build script variables at run time. Use `std::env::var("CARGO_BIN_NAME")` instead + +error: aborting due to 8 previous errors diff --git a/tests/ui/env-macro/env-not-defined-default.stderr b/tests/ui/env-macro/env-not-defined-default.stderr index 77ba00e45c1c6..8340bb0e8fba6 100644 --- a/tests/ui/env-macro/env-not-defined-default.stderr +++ b/tests/ui/env-macro/env-not-defined-default.stderr @@ -4,6 +4,7 @@ error: environment variable `CARGO__HOPEFULLY_NOT_DEFINED__` not defined at comp LL | env!("CARGO__HOPEFULLY_NOT_DEFINED__"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | + = help: `CARGO__HOPEFULLY_NOT_DEFINED__` may not be available for the current Cargo target = help: Cargo sets build script variables at run time. Use `std::env::var("CARGO__HOPEFULLY_NOT_DEFINED__")` instead error: aborting due to 1 previous error diff --git a/tests/ui/pin-ergonomics/pin-coercion-check.normal.stderr b/tests/ui/pin-ergonomics/pin-coercion-check.normal.stderr new file mode 100644 index 0000000000000..0806ad64448c4 --- /dev/null +++ b/tests/ui/pin-ergonomics/pin-coercion-check.normal.stderr @@ -0,0 +1,227 @@ +error[E0277]: `T` cannot be unpinned + --> $DIR/pin-coercion-check.rs:9:36 + | +LL | |x: Pin<&mut T>| -> &mut T { x.get_mut() }; + | ^^^^^^^ the trait `Unpin` is not implemented for `T` + | + = note: consider using the `pin!` macro + consider using `Box::pin` if you need to access the pinned value outside of the current scope +note: required by a bound in `Pin::<&'a mut T>::get_mut` + --> $SRC_DIR/core/src/pin.rs:LL:COL +help: consider restricting type parameter `T` with trait `Unpin` + | +LL | fn get() { + | ++++++++++++++++++++ + +error[E0308]: mismatched types + --> $DIR/pin-coercion-check.rs:18:34 + | +LL | |x: Pin<&mut T>| -> &mut T { x }; + | ------ ^ expected `&mut T`, found `Pin<&mut T>` + | | + | expected `&mut T` because of return type + | + = note: expected mutable reference `&mut T` + found struct `Pin<&mut T>` +help: consider mutably borrowing here + | +LL | |x: Pin<&mut T>| -> &mut T { &mut x }; + | ++++ + +error[E0308]: mismatched types + --> $DIR/pin-coercion-check.rs:21:30 + | +LL | |x: Pin<&mut T>| -> &T { x }; + | -- ^ expected `&T`, found `Pin<&mut T>` + | | + | expected `&T` because of return type + | + = note: expected reference `&T` + found struct `Pin<&mut T>` +help: consider borrowing here + | +LL | |x: Pin<&mut T>| -> &T { &x }; + | + + +error[E0308]: mismatched types + --> $DIR/pin-coercion-check.rs:24:26 + | +LL | |x: Pin<&T>| -> &T { x }; + | -- ^ expected `&T`, found `Pin<&T>` + | | + | expected `&T` because of return type + | + = note: expected reference `&T` + found struct `Pin<&T>` +help: consider borrowing here + | +LL | |x: Pin<&T>| -> &T { &x }; + | + + +error[E0308]: mismatched types + --> $DIR/pin-coercion-check.rs:27:30 + | +LL | |x: Pin<&T>| -> &mut T { x }; + | ------ ^ expected `&mut T`, found `Pin<&T>` + | | + | expected `&mut T` because of return type + | + = note: expected mutable reference `&mut T` + found struct `Pin<&T>` +help: consider mutably borrowing here + | +LL | |x: Pin<&T>| -> &mut T { &mut x }; + | ++++ + +error[E0308]: mismatched types + --> $DIR/pin-coercion-check.rs:31:34 + | +LL | |x: Pin<&mut U>| -> &mut U { x }; + | ------ ^ expected `&mut U`, found `Pin<&mut U>` + | | + | expected `&mut U` because of return type + | + = note: expected mutable reference `&mut U` + found struct `Pin<&mut U>` +help: consider mutably borrowing here + | +LL | |x: Pin<&mut U>| -> &mut U { &mut x }; + | ++++ + +error[E0308]: mismatched types + --> $DIR/pin-coercion-check.rs:33:30 + | +LL | |x: Pin<&mut U>| -> &U { x }; + | -- ^ expected `&U`, found `Pin<&mut U>` + | | + | expected `&U` because of return type + | + = note: expected reference `&U` + found struct `Pin<&mut U>` +help: consider borrowing here + | +LL | |x: Pin<&mut U>| -> &U { &x }; + | + + +error[E0308]: mismatched types + --> $DIR/pin-coercion-check.rs:35:26 + | +LL | |x: Pin<&U>| -> &U { x }; + | -- ^ expected `&U`, found `Pin<&U>` + | | + | expected `&U` because of return type + | + = note: expected reference `&U` + found struct `Pin<&U>` +help: consider borrowing here + | +LL | |x: Pin<&U>| -> &U { &x }; + | + + +error[E0308]: mismatched types + --> $DIR/pin-coercion-check.rs:37:30 + | +LL | |x: Pin<&U>| -> &mut U { x }; + | ------ ^ expected `&mut U`, found `Pin<&U>` + | | + | expected `&mut U` because of return type + | + = note: expected mutable reference `&mut U` + found struct `Pin<&U>` +help: consider mutably borrowing here + | +LL | |x: Pin<&U>| -> &mut U { &mut x }; + | ++++ + +error[E0308]: mismatched types + --> $DIR/pin-coercion-check.rs:43:34 + | +LL | |x: &mut T| -> Pin<&mut T> { x }; + | ----------- ^ expected `Pin<&mut T>`, found `&mut T` + | | + | expected `Pin<&mut T>` because of return type + | + = note: expected struct `Pin<&mut T>` + found mutable reference `&mut T` + +error[E0308]: mismatched types + --> $DIR/pin-coercion-check.rs:46:30 + | +LL | |x: &mut T| -> Pin<&T> { x }; + | ------- ^ expected `Pin<&T>`, found `&mut T` + | | + | expected `Pin<&T>` because of return type + | + = note: expected struct `Pin<&T>` + found mutable reference `&mut T` + +error[E0308]: mismatched types + --> $DIR/pin-coercion-check.rs:49:26 + | +LL | |x: &T| -> Pin<&T> { x }; + | ------- ^ expected `Pin<&T>`, found `&T` + | | + | expected `Pin<&T>` because of return type + | + = note: expected struct `Pin<&T>` + found reference `&T` + +error[E0308]: mismatched types + --> $DIR/pin-coercion-check.rs:52:30 + | +LL | |x: &T| -> Pin<&mut T> { x }; + | ----------- ^ expected `Pin<&mut T>`, found `&T` + | | + | expected `Pin<&mut T>` because of return type + | + = note: expected struct `Pin<&mut T>` + found reference `&T` + +error[E0308]: mismatched types + --> $DIR/pin-coercion-check.rs:56:34 + | +LL | |x: &mut U| -> Pin<&mut U> { x }; + | ----------- ^ expected `Pin<&mut U>`, found `&mut U` + | | + | expected `Pin<&mut U>` because of return type + | + = note: expected struct `Pin<&mut U>` + found mutable reference `&mut U` + +error[E0308]: mismatched types + --> $DIR/pin-coercion-check.rs:58:30 + | +LL | |x: &mut U| -> Pin<&U> { x }; + | ------- ^ expected `Pin<&U>`, found `&mut U` + | | + | expected `Pin<&U>` because of return type + | + = note: expected struct `Pin<&U>` + found mutable reference `&mut U` + +error[E0308]: mismatched types + --> $DIR/pin-coercion-check.rs:60:26 + | +LL | |x: &U| -> Pin<&U> { x }; + | ------- ^ expected `Pin<&U>`, found `&U` + | | + | expected `Pin<&U>` because of return type + | + = note: expected struct `Pin<&U>` + found reference `&U` + +error[E0308]: mismatched types + --> $DIR/pin-coercion-check.rs:62:30 + | +LL | |x: &U| -> Pin<&mut U> { x }; + | ----------- ^ expected `Pin<&mut U>`, found `&U` + | | + | expected `Pin<&mut U>` because of return type + | + = note: expected struct `Pin<&mut U>` + found reference `&U` + +error: aborting due to 17 previous errors + +Some errors have detailed explanations: E0277, E0308. +For more information about an error, try `rustc --explain E0277`. diff --git a/tests/ui/pin-ergonomics/pin-coercion-check.pin_ergonomics.stderr b/tests/ui/pin-ergonomics/pin-coercion-check.pin_ergonomics.stderr new file mode 100644 index 0000000000000..3dc59cecb3c1e --- /dev/null +++ b/tests/ui/pin-ergonomics/pin-coercion-check.pin_ergonomics.stderr @@ -0,0 +1,155 @@ +error[E0277]: `T` cannot be unpinned + --> $DIR/pin-coercion-check.rs:9:36 + | +LL | |x: Pin<&mut T>| -> &mut T { x.get_mut() }; + | ^^^^^^^ the trait `Unpin` is not implemented for `T` + | + = note: consider using the `pin!` macro + consider using `Box::pin` if you need to access the pinned value outside of the current scope +note: required by a bound in `Pin::<&'a mut T>::get_mut` + --> $SRC_DIR/core/src/pin.rs:LL:COL +help: consider restricting type parameter `T` with trait `Unpin` + | +LL | fn get() { + | ++++++++++++++++++++ + +error[E0277]: `T` cannot be unpinned + --> $DIR/pin-coercion-check.rs:18:34 + | +LL | |x: Pin<&mut T>| -> &mut T { x }; + | ^ the trait `Unpin` is not implemented for `T` + | + = note: consider using the `pin!` macro + consider using `Box::pin` if you need to access the pinned value outside of the current scope + = note: required for the cast from `Pin<&mut T>` to `&mut T` +help: consider restricting type parameter `T` with trait `Unpin` + | +LL | fn pin_to_ref() { + | ++++++++++++++++++++ + +error[E0277]: `T` cannot be unpinned + --> $DIR/pin-coercion-check.rs:21:30 + | +LL | |x: Pin<&mut T>| -> &T { x }; + | ^ the trait `Unpin` is not implemented for `T` + | + = note: consider using the `pin!` macro + consider using `Box::pin` if you need to access the pinned value outside of the current scope + = note: required for the cast from `Pin<&mut T>` to `&T` +help: consider restricting type parameter `T` with trait `Unpin` + | +LL | fn pin_to_ref() { + | ++++++++++++++++++++ + +error[E0277]: `T` cannot be unpinned + --> $DIR/pin-coercion-check.rs:24:26 + | +LL | |x: Pin<&T>| -> &T { x }; + | ^ the trait `Unpin` is not implemented for `T` + | + = note: consider using the `pin!` macro + consider using `Box::pin` if you need to access the pinned value outside of the current scope + = note: required for the cast from `Pin<&T>` to `&T` +help: consider restricting type parameter `T` with trait `Unpin` + | +LL | fn pin_to_ref() { + | ++++++++++++++++++++ + +error[E0308]: mismatched types + --> $DIR/pin-coercion-check.rs:27:30 + | +LL | |x: Pin<&T>| -> &mut T { x }; + | ------ ^ types differ in mutability + | | + | expected `&mut T` because of return type + | + = note: expected mutable reference `&mut T` + found struct `Pin<&T>` +help: consider mutably borrowing here + | +LL | |x: Pin<&T>| -> &mut T { &mut x }; + | ++++ + +error[E0308]: mismatched types + --> $DIR/pin-coercion-check.rs:37:30 + | +LL | |x: Pin<&U>| -> &mut U { x }; + | ------ ^ types differ in mutability + | | + | expected `&mut U` because of return type + | + = note: expected mutable reference `&mut U` + found struct `Pin<&U>` +help: consider mutably borrowing here + | +LL | |x: Pin<&U>| -> &mut U { &mut x }; + | ++++ + +error[E0277]: `T` cannot be unpinned + --> $DIR/pin-coercion-check.rs:43:34 + | +LL | |x: &mut T| -> Pin<&mut T> { x }; + | ^ the trait `Unpin` is not implemented for `T` + | + = note: consider using the `pin!` macro + consider using `Box::pin` if you need to access the pinned value outside of the current scope + = note: required for the cast from `&mut T` to `Pin<&mut T>` +help: consider restricting type parameter `T` with trait `Unpin` + | +LL | fn ref_to_pin() { + | ++++++++++++++++++++ + +error[E0277]: `T` cannot be unpinned + --> $DIR/pin-coercion-check.rs:46:30 + | +LL | |x: &mut T| -> Pin<&T> { x }; + | ^ the trait `Unpin` is not implemented for `T` + | + = note: consider using the `pin!` macro + consider using `Box::pin` if you need to access the pinned value outside of the current scope + = note: required for the cast from `&mut T` to `Pin<&T>` +help: consider restricting type parameter `T` with trait `Unpin` + | +LL | fn ref_to_pin() { + | ++++++++++++++++++++ + +error[E0277]: `T` cannot be unpinned + --> $DIR/pin-coercion-check.rs:49:26 + | +LL | |x: &T| -> Pin<&T> { x }; + | ^ the trait `Unpin` is not implemented for `T` + | + = note: consider using the `pin!` macro + consider using `Box::pin` if you need to access the pinned value outside of the current scope + = note: required for the cast from `&T` to `Pin<&T>` +help: consider restricting type parameter `T` with trait `Unpin` + | +LL | fn ref_to_pin() { + | ++++++++++++++++++++ + +error[E0308]: mismatched types + --> $DIR/pin-coercion-check.rs:52:30 + | +LL | |x: &T| -> Pin<&mut T> { x }; + | ----------- ^ types differ in mutability + | | + | expected `Pin<&mut T>` because of return type + | + = note: expected struct `Pin<&mut T>` + found reference `&T` + +error[E0308]: mismatched types + --> $DIR/pin-coercion-check.rs:62:30 + | +LL | |x: &U| -> Pin<&mut U> { x }; + | ----------- ^ types differ in mutability + | | + | expected `Pin<&mut U>` because of return type + | + = note: expected struct `Pin<&mut U>` + found reference `&U` + +error: aborting due to 11 previous errors + +Some errors have detailed explanations: E0277, E0308. +For more information about an error, try `rustc --explain E0277`. diff --git a/tests/ui/pin-ergonomics/pin-coercion-check.rs b/tests/ui/pin-ergonomics/pin-coercion-check.rs new file mode 100644 index 0000000000000..9b51851219ccd --- /dev/null +++ b/tests/ui/pin-ergonomics/pin-coercion-check.rs @@ -0,0 +1,66 @@ +//@ revisions: pin_ergonomics normal +//@ edition:2024 +#![cfg_attr(pin_ergonomics, feature(pin_ergonomics))] +#![allow(incomplete_features)] + +use std::pin::Pin; + +fn get() { + |x: Pin<&mut T>| -> &mut T { x.get_mut() }; //~ ERROR `T` cannot be unpinned + |x: Pin<&T>| -> &T { x.get_ref() }; + + |x: Pin<&mut U>| -> &mut U { x.get_mut() }; + |x: Pin<&U>| -> &U { x.get_ref() }; +} + +fn pin_to_ref() { + // T: !Unpin + |x: Pin<&mut T>| -> &mut T { x }; + //[normal]~^ ERROR mismatched types + //[pin_ergonomics]~^^ ERROR `T` cannot be unpinned + |x: Pin<&mut T>| -> &T { x }; + //[normal]~^ ERROR mismatched types + //[pin_ergonomics]~^^ ERROR `T` cannot be unpinned + |x: Pin<&T>| -> &T { x }; + //[normal]~^ ERROR mismatched types + //[pin_ergonomics]~^^ ERROR `T` cannot be unpinned + |x: Pin<&T>| -> &mut T { x }; + //~^ ERROR mismatched types + + // U: Unpin + |x: Pin<&mut U>| -> &mut U { x }; + //[normal]~^ ERROR mismatched types + |x: Pin<&mut U>| -> &U { x }; + //[normal]~^ ERROR mismatched types + |x: Pin<&U>| -> &U { x }; + //[normal]~^ ERROR mismatched types + |x: Pin<&U>| -> &mut U { x }; + //~^ ERROR mismatched types +} + +fn ref_to_pin() { + // T: !Unpin + |x: &mut T| -> Pin<&mut T> { x }; + //[normal]~^ ERROR mismatched types + //[pin_ergonomics]~^^ ERROR `T` cannot be unpinned + |x: &mut T| -> Pin<&T> { x }; + //[normal]~^ ERROR mismatched types + //[pin_ergonomics]~^^ ERROR `T` cannot be unpinned + |x: &T| -> Pin<&T> { x }; + //[normal]~^ ERROR mismatched types + //[pin_ergonomics]~^^ ERROR `T` cannot be unpinned + |x: &T| -> Pin<&mut T> { x }; + //~^ ERROR mismatched types + + // U: Unpin + |x: &mut U| -> Pin<&mut U> { x }; + //[normal]~^ ERROR mismatched types + |x: &mut U| -> Pin<&U> { x }; + //[normal]~^ ERROR mismatched types + |x: &U| -> Pin<&U> { x }; + //[normal]~^ ERROR mismatched types + |x: &U| -> Pin<&mut U> { x }; + //~^ ERROR mismatched types +} + +fn main() {} diff --git a/tests/ui/pin-ergonomics/pin-coercion.rs b/tests/ui/pin-ergonomics/pin-coercion.rs new file mode 100644 index 0000000000000..6ace4b97edb34 --- /dev/null +++ b/tests/ui/pin-ergonomics/pin-coercion.rs @@ -0,0 +1,56 @@ +//@ run-pass +//@ edition:2024 +#![feature(pin_ergonomics)] +#![allow(incomplete_features)] +#![deny(dead_code)] + +use std::cell::RefCell; + +fn coerce_mut_to_pin_mut(x: &mut T) -> &pin mut T { + x +} +fn coerce_ref_to_pin_ref(x: &T) -> &pin const T { + x +} +fn coerce_pin_mut_to_mut(x: &pin mut T) -> &mut T { + x +} +fn coerce_pin_ref_to_ref(x: &pin const T) -> &T { + x +} + +fn coerce_pin_mut_to_ref(x: &pin mut T) -> &T { + x +} +fn coerce_mut_to_pin_ref(x: &mut T) -> &pin const T { + x +} + +fn test(x: &mut RefCell) { + let mut x: &pin mut _ = coerce_mut_to_pin_mut(x); + x.get_mut().get_mut().push_str("&mut T -> &pin mut T\n"); + let x_ref: &_ = coerce_pin_mut_to_ref(x.as_mut()); + x_ref.borrow_mut().push_str("&pin mut T -> &T\n"); + let x: &mut _ = coerce_pin_mut_to_mut(x); + x.get_mut().push_str("&pin mut T -> &mut T\n"); + let x: &pin const _ = coerce_mut_to_pin_ref(x); + x.borrow_mut().push_str("&mut T -> &pin const T\n"); + let x: &_ = coerce_pin_ref_to_ref(x); + x.borrow_mut().push_str("&pin const T -> &T\n"); + let x: &pin const _ = coerce_ref_to_pin_ref(x); + x.borrow_mut().push_str("&T -> &pin const T\n"); +} + +fn main() { + let mut x = RefCell::new(String::new()); + test(&mut x); + assert_eq!( + x.borrow().as_str(), + "&mut T -> &pin mut T\n\ + &pin mut T -> &T\n\ + &pin mut T -> &mut T\n\ + &mut T -> &pin const T\n\ + &pin const T -> &T\n\ + &T -> &pin const T\n" + ); +}