diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index d2ce6bdb08e58..e9c05eb5f455f 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -324,16 +324,10 @@ enum FnDeclKind { } impl FnDeclKind { - fn impl_trait_return_allowed(&self, tcx: TyCtxt<'_>) -> bool { + fn impl_trait_allowed(&self, tcx: TyCtxt<'_>) -> bool { match self { FnDeclKind::Fn | FnDeclKind::Inherent => true, FnDeclKind::Impl if tcx.features().return_position_impl_trait_in_trait => true, - _ => false, - } - } - - fn impl_trait_in_trait_allowed(&self, tcx: TyCtxt<'_>) -> bool { - match self { FnDeclKind::Trait if tcx.features().return_position_impl_trait_in_trait => true, _ => false, } @@ -1698,9 +1692,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { })); let output = if let Some((ret_id, span)) = make_ret_async { - match kind { - FnDeclKind::Trait => { - if !kind.impl_trait_in_trait_allowed(self.tcx) { + if !kind.impl_trait_allowed(self.tcx) { + match kind { + FnDeclKind::Trait | FnDeclKind::Impl => { self.tcx .sess .create_feature_err( @@ -1709,51 +1703,27 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { ) .emit(); } - self.lower_async_fn_ret_ty( - &decl.output, - fn_node_id.expect("`make_ret_async` but no `fn_def_id`"), - ret_id, - true, - ) - } - _ => { - if !kind.impl_trait_return_allowed(self.tcx) { - if kind == FnDeclKind::Impl { - self.tcx - .sess - .create_feature_err( - TraitFnAsync { fn_span, span }, - sym::return_position_impl_trait_in_trait, - ) - .emit(); - } else { - self.tcx.sess.emit_err(TraitFnAsync { fn_span, span }); - } + _ => { + self.tcx.sess.emit_err(TraitFnAsync { fn_span, span }); } - self.lower_async_fn_ret_ty( - &decl.output, - fn_node_id.expect("`make_ret_async` but no `fn_def_id`"), - ret_id, - false, - ) } } + + self.lower_async_fn_ret_ty( + &decl.output, + fn_node_id.expect("`make_ret_async` but no `fn_def_id`"), + ret_id, + matches!(kind, FnDeclKind::Trait), + ) } else { match decl.output { FnRetTy::Ty(ref ty) => { let mut context = match fn_node_id { - Some(fn_node_id) if kind.impl_trait_return_allowed(self.tcx) => { - let fn_def_id = self.local_def_id(fn_node_id); - ImplTraitContext::ReturnPositionOpaqueTy { - origin: hir::OpaqueTyOrigin::FnReturn(fn_def_id), - in_trait: false, - } - } - Some(fn_node_id) if kind.impl_trait_in_trait_allowed(self.tcx) => { + Some(fn_node_id) if kind.impl_trait_allowed(self.tcx) => { let fn_def_id = self.local_def_id(fn_node_id); ImplTraitContext::ReturnPositionOpaqueTy { origin: hir::OpaqueTyOrigin::FnReturn(fn_def_id), - in_trait: true, + in_trait: matches!(kind, FnDeclKind::Trait), } } _ => ImplTraitContext::Disallowed(match kind { diff --git a/compiler/rustc_builtin_macros/src/cfg_eval.rs b/compiler/rustc_builtin_macros/src/cfg_eval.rs index e673dff0dea8e..009f3c783d4c8 100644 --- a/compiler/rustc_builtin_macros/src/cfg_eval.rs +++ b/compiler/rustc_builtin_macros/src/cfg_eval.rs @@ -7,6 +7,7 @@ use rustc_ast::visit::Visitor; use rustc_ast::NodeId; use rustc_ast::{mut_visit, visit}; use rustc_ast::{Attribute, HasAttrs, HasTokens}; +use rustc_errors::PResult; use rustc_expand::base::{Annotatable, ExtCtxt}; use rustc_expand::config::StripUnconfigured; use rustc_expand::configure; @@ -144,33 +145,34 @@ impl CfgEval<'_, '_> { // the location of `#[cfg]` and `#[cfg_attr]` in the token stream. The tokenization // process is lossless, so this process is invisible to proc-macros. - let parse_annotatable_with: fn(&mut Parser<'_>) -> _ = match annotatable { - Annotatable::Item(_) => { - |parser| Annotatable::Item(parser.parse_item(ForceCollect::Yes).unwrap().unwrap()) - } - Annotatable::TraitItem(_) => |parser| { - Annotatable::TraitItem( - parser.parse_trait_item(ForceCollect::Yes).unwrap().unwrap().unwrap(), - ) - }, - Annotatable::ImplItem(_) => |parser| { - Annotatable::ImplItem( - parser.parse_impl_item(ForceCollect::Yes).unwrap().unwrap().unwrap(), - ) - }, - Annotatable::ForeignItem(_) => |parser| { - Annotatable::ForeignItem( - parser.parse_foreign_item(ForceCollect::Yes).unwrap().unwrap().unwrap(), - ) - }, - Annotatable::Stmt(_) => |parser| { - Annotatable::Stmt(P(parser.parse_stmt(ForceCollect::Yes).unwrap().unwrap())) - }, - Annotatable::Expr(_) => { - |parser| Annotatable::Expr(parser.parse_expr_force_collect().unwrap()) - } - _ => unreachable!(), - }; + let parse_annotatable_with: for<'a> fn(&mut Parser<'a>) -> PResult<'a, _> = + match annotatable { + Annotatable::Item(_) => { + |parser| Ok(Annotatable::Item(parser.parse_item(ForceCollect::Yes)?.unwrap())) + } + Annotatable::TraitItem(_) => |parser| { + Ok(Annotatable::TraitItem( + parser.parse_trait_item(ForceCollect::Yes)?.unwrap().unwrap(), + )) + }, + Annotatable::ImplItem(_) => |parser| { + Ok(Annotatable::ImplItem( + parser.parse_impl_item(ForceCollect::Yes)?.unwrap().unwrap(), + )) + }, + Annotatable::ForeignItem(_) => |parser| { + Ok(Annotatable::ForeignItem( + parser.parse_foreign_item(ForceCollect::Yes)?.unwrap().unwrap(), + )) + }, + Annotatable::Stmt(_) => |parser| { + Ok(Annotatable::Stmt(P(parser.parse_stmt(ForceCollect::Yes)?.unwrap()))) + }, + Annotatable::Expr(_) => { + |parser| Ok(Annotatable::Expr(parser.parse_expr_force_collect()?)) + } + _ => unreachable!(), + }; // 'Flatten' all nonterminals (i.e. `TokenKind::Interpolated`) // to `None`-delimited groups containing the corresponding tokens. This @@ -193,7 +195,13 @@ impl CfgEval<'_, '_> { let mut parser = rustc_parse::stream_to_parser(&self.cfg.sess.parse_sess, orig_tokens, None); parser.capture_cfg = true; - annotatable = parse_annotatable_with(&mut parser); + match parse_annotatable_with(&mut parser) { + Ok(a) => annotatable = a, + Err(mut err) => { + err.emit(); + return Some(annotatable); + } + } // Now that we have our re-parsed `AttrTokenStream`, recursively configuring // our attribute target will correctly the tokens as well. diff --git a/compiler/rustc_codegen_llvm/src/builder.rs b/compiler/rustc_codegen_llvm/src/builder.rs index 63b63c6a1fab5..59b1c7fb5dbd0 100644 --- a/compiler/rustc_codegen_llvm/src/builder.rs +++ b/compiler/rustc_codegen_llvm/src/builder.rs @@ -1,14 +1,13 @@ use crate::attributes; use crate::common::Funclet; use crate::context::CodegenCx; -use crate::llvm::{self, BasicBlock, False}; -use crate::llvm::{AtomicOrdering, AtomicRmwBinOp, SynchronizationScope}; +use crate::llvm::{self, AtomicOrdering, AtomicRmwBinOp, BasicBlock}; use crate::type_::Type; use crate::type_of::LayoutLlvmExt; use crate::value::Value; use cstr::cstr; use libc::{c_char, c_uint}; -use rustc_codegen_ssa::common::{IntPredicate, RealPredicate, TypeKind}; +use rustc_codegen_ssa::common::{IntPredicate, RealPredicate, SynchronizationScope, TypeKind}; use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue}; use rustc_codegen_ssa::mir::place::PlaceRef; use rustc_codegen_ssa::traits::*; @@ -1042,15 +1041,17 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> { ) -> &'ll Value { let weak = if weak { llvm::True } else { llvm::False }; unsafe { - llvm::LLVMRustBuildAtomicCmpXchg( + let value = llvm::LLVMBuildAtomicCmpXchg( self.llbuilder, dst, cmp, src, AtomicOrdering::from_generic(order), AtomicOrdering::from_generic(failure_order), - weak, - ) + llvm::False, // SingleThreaded + ); + llvm::LLVMSetWeak(value, weak); + value } } fn atomic_rmw( @@ -1067,7 +1068,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> { dst, src, AtomicOrdering::from_generic(order), - False, + llvm::False, // SingleThreaded ) } } @@ -1075,13 +1076,18 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> { fn atomic_fence( &mut self, order: rustc_codegen_ssa::common::AtomicOrdering, - scope: rustc_codegen_ssa::common::SynchronizationScope, + scope: SynchronizationScope, ) { + let single_threaded = match scope { + SynchronizationScope::SingleThread => llvm::True, + SynchronizationScope::CrossThread => llvm::False, + }; unsafe { - llvm::LLVMRustBuildAtomicFence( + llvm::LLVMBuildFence( self.llbuilder, AtomicOrdering::from_generic(order), - SynchronizationScope::from_generic(scope), + single_threaded, + UNNAMED, ); } } diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs index ce27dc5a5d1ea..09f2c35689765 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs @@ -400,27 +400,6 @@ impl AtomicOrdering { } } -/// LLVMRustSynchronizationScope -#[derive(Copy, Clone)] -#[repr(C)] -pub enum SynchronizationScope { - SingleThread, - CrossThread, -} - -impl SynchronizationScope { - pub fn from_generic(sc: rustc_codegen_ssa::common::SynchronizationScope) -> Self { - match sc { - rustc_codegen_ssa::common::SynchronizationScope::SingleThread => { - SynchronizationScope::SingleThread - } - rustc_codegen_ssa::common::SynchronizationScope::CrossThread => { - SynchronizationScope::CrossThread - } - } - } -} - /// LLVMRustFileType #[derive(Copy, Clone)] #[repr(C)] @@ -1782,16 +1761,18 @@ extern "C" { Order: AtomicOrdering, ) -> &'a Value; - pub fn LLVMRustBuildAtomicCmpXchg<'a>( + pub fn LLVMBuildAtomicCmpXchg<'a>( B: &Builder<'a>, LHS: &'a Value, CMP: &'a Value, RHS: &'a Value, Order: AtomicOrdering, FailureOrder: AtomicOrdering, - Weak: Bool, + SingleThreaded: Bool, ) -> &'a Value; + pub fn LLVMSetWeak(CmpXchgInst: &Value, IsWeak: Bool); + pub fn LLVMBuildAtomicRMW<'a>( B: &Builder<'a>, Op: AtomicRmwBinOp, @@ -1801,11 +1782,12 @@ extern "C" { SingleThreaded: Bool, ) -> &'a Value; - pub fn LLVMRustBuildAtomicFence( - B: &Builder<'_>, + pub fn LLVMBuildFence<'a>( + B: &Builder<'a>, Order: AtomicOrdering, - Scope: SynchronizationScope, - ); + SingleThreaded: Bool, + Name: *const c_char, + ) -> &'a Value; /// Writes a module to the specified path. Returns 0 on success. pub fn LLVMWriteBitcodeToFile(M: &Module, Path: *const c_char) -> c_int; diff --git a/compiler/rustc_const_eval/src/const_eval/mod.rs b/compiler/rustc_const_eval/src/const_eval/mod.rs index d9c4ae4d53f91..1c33e7845cb0b 100644 --- a/compiler/rustc_const_eval/src/const_eval/mod.rs +++ b/compiler/rustc_const_eval/src/const_eval/mod.rs @@ -100,10 +100,10 @@ pub(crate) fn try_destructure_mir_constant<'tcx>( tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, val: mir::ConstantKind<'tcx>, -) -> InterpResult<'tcx, mir::DestructuredMirConstant<'tcx>> { +) -> InterpResult<'tcx, mir::DestructuredConstant<'tcx>> { trace!("destructure_mir_constant: {:?}", val); let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false); - let op = ecx.mir_const_to_op(&val, None)?; + let op = ecx.const_to_op(&val, None)?; // We go to `usize` as we cannot allocate anything bigger anyway. let (field_count, variant, down) = match val.ty().kind() { @@ -129,7 +129,7 @@ pub(crate) fn try_destructure_mir_constant<'tcx>( .collect::<InterpResult<'tcx, Vec<_>>>()?; let fields = tcx.arena.alloc_from_iter(fields_iter); - Ok(mir::DestructuredMirConstant { variant, fields }) + Ok(mir::DestructuredConstant { variant, fields }) } #[instrument(skip(tcx), level = "debug")] @@ -139,7 +139,7 @@ pub(crate) fn deref_mir_constant<'tcx>( val: mir::ConstantKind<'tcx>, ) -> mir::ConstantKind<'tcx> { let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false); - let op = ecx.mir_const_to_op(&val, None).unwrap(); + let op = ecx.const_to_op(&val, None).unwrap(); let mplace = ecx.deref_operand(&op).unwrap(); if let Some(alloc_id) = mplace.ptr.provenance { assert_eq!( diff --git a/compiler/rustc_const_eval/src/interpret/eval_context.rs b/compiler/rustc_const_eval/src/interpret/eval_context.rs index 3177537fee25e..bdebfbb3ff510 100644 --- a/compiler/rustc_const_eval/src/interpret/eval_context.rs +++ b/compiler/rustc_const_eval/src/interpret/eval_context.rs @@ -683,11 +683,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { self.stack_mut().push(frame); // Make sure all the constants required by this frame evaluate successfully (post-monomorphization check). - for const_ in &body.required_consts { - let span = const_.span; - let const_ = - self.subst_from_current_frame_and_normalize_erasing_regions(const_.literal)?; - self.mir_const_to_op(&const_, None).map_err(|err| { + for ct in &body.required_consts { + let span = ct.span; + let ct = self.subst_from_current_frame_and_normalize_erasing_regions(ct.literal)?; + self.const_to_op(&ct, None).map_err(|err| { // If there was an error, set the span of the current frame to this constant. // Avoiding doing this when evaluation succeeds. self.frame_mut().loc = Err(span); diff --git a/compiler/rustc_const_eval/src/interpret/operand.rs b/compiler/rustc_const_eval/src/interpret/operand.rs index 29c745a0886f8..dc5305aabcf89 100644 --- a/compiler/rustc_const_eval/src/interpret/operand.rs +++ b/compiler/rustc_const_eval/src/interpret/operand.rs @@ -534,7 +534,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { // * During ConstProp, with `TooGeneric` or since the `required_consts` were not all // checked yet. // * During CTFE, since promoteds in `const`/`static` initializer bodies can fail. - self.mir_const_to_op(&val, layout)? + self.const_to_op(&val, layout)? } }; trace!("{:?}: {:?}", mir_op, *op); @@ -549,50 +549,42 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { ops.iter().map(|op| self.eval_operand(op, None)).collect() } - // Used when the miri-engine runs into a constant and for extracting information from constants - // in patterns via the `const_eval` module - /// The `val` and `layout` are assumed to already be in our interpreter - /// "universe" (param_env). pub fn const_to_op( - &self, - c: ty::Const<'tcx>, - layout: Option<TyAndLayout<'tcx>>, - ) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> { - match c.kind() { - ty::ConstKind::Param(_) | ty::ConstKind::Placeholder(..) => throw_inval!(TooGeneric), - ty::ConstKind::Error(DelaySpanBugEmitted { reported, .. }) => { - throw_inval!(AlreadyReported(reported)) - } - ty::ConstKind::Unevaluated(uv) => { - // NOTE: We evaluate to a `ValTree` here as a check to ensure - // we're working with valid constants, even though we never need it. - let instance = self.resolve(uv.def, uv.substs)?; - let cid = GlobalId { instance, promoted: None }; - let _valtree = self - .tcx - .eval_to_valtree(self.param_env.and(cid))? - .unwrap_or_else(|| bug!("unable to create ValTree for {:?}", uv)); - - Ok(self.eval_to_allocation(cid)?.into()) - } - ty::ConstKind::Bound(..) | ty::ConstKind::Infer(..) => { - span_bug!(self.cur_span(), "const_to_op: Unexpected ConstKind {:?}", c) - } - ty::ConstKind::Value(valtree) => { - let ty = c.ty(); - let const_val = self.tcx.valtree_to_const_val((ty, valtree)); - self.const_val_to_op(const_val, ty, layout) - } - } - } - - pub fn mir_const_to_op( &self, val: &mir::ConstantKind<'tcx>, layout: Option<TyAndLayout<'tcx>>, ) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> { match val { - mir::ConstantKind::Ty(ct) => self.const_to_op(*ct, layout), + mir::ConstantKind::Ty(ct) => { + match ct.kind() { + ty::ConstKind::Param(_) | ty::ConstKind::Placeholder(..) => { + throw_inval!(TooGeneric) + } + ty::ConstKind::Error(DelaySpanBugEmitted { reported, .. }) => { + throw_inval!(AlreadyReported(reported)) + } + ty::ConstKind::Unevaluated(uv) => { + // NOTE: We evaluate to a `ValTree` here as a check to ensure + // we're working with valid constants, even though we never need it. + let instance = self.resolve(uv.def, uv.substs)?; + let cid = GlobalId { instance, promoted: None }; + let _valtree = self + .tcx + .eval_to_valtree(self.param_env.and(cid))? + .unwrap_or_else(|| bug!("unable to create ValTree for {uv:?}")); + + Ok(self.eval_to_allocation(cid)?.into()) + } + ty::ConstKind::Bound(..) | ty::ConstKind::Infer(..) => { + span_bug!(self.cur_span(), "unexpected ConstKind in ctfe: {ct:?}") + } + ty::ConstKind::Value(valtree) => { + let ty = ct.ty(); + let const_val = self.tcx.valtree_to_const_val((ty, valtree)); + self.const_val_to_op(const_val, ty, layout) + } + } + } mir::ConstantKind::Val(val, ty) => self.const_val_to_op(*val, *ty, layout), mir::ConstantKind::Unevaluated(uv, _) => { let instance = self.resolve(uv.def, uv.substs)?; diff --git a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp index 6ee3c7d68213e..b2cfcf53c5993 100644 --- a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp +++ b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp @@ -406,45 +406,6 @@ extern "C" LLVMValueRef LLVMRustBuildAtomicStore(LLVMBuilderRef B, return wrap(SI); } -// FIXME: Use the C-API LLVMBuildAtomicCmpXchg and LLVMSetWeak -// once we raise our minimum support to LLVM 10. -extern "C" LLVMValueRef -LLVMRustBuildAtomicCmpXchg(LLVMBuilderRef B, LLVMValueRef Target, - LLVMValueRef Old, LLVMValueRef Source, - LLVMAtomicOrdering Order, - LLVMAtomicOrdering FailureOrder, LLVMBool Weak) { - // Rust probably knows the alignment of the target value and should be able to - // specify something more precise than MaybeAlign here. See also - // https://reviews.llvm.org/D97224 which may be a useful reference. - AtomicCmpXchgInst *ACXI = unwrap(B)->CreateAtomicCmpXchg( - unwrap(Target), unwrap(Old), unwrap(Source), llvm::MaybeAlign(), fromRust(Order), - fromRust(FailureOrder)); - ACXI->setWeak(Weak); - return wrap(ACXI); -} - -enum class LLVMRustSynchronizationScope { - SingleThread, - CrossThread, -}; - -static SyncScope::ID fromRust(LLVMRustSynchronizationScope Scope) { - switch (Scope) { - case LLVMRustSynchronizationScope::SingleThread: - return SyncScope::SingleThread; - case LLVMRustSynchronizationScope::CrossThread: - return SyncScope::System; - default: - report_fatal_error("bad SynchronizationScope."); - } -} - -extern "C" LLVMValueRef -LLVMRustBuildAtomicFence(LLVMBuilderRef B, LLVMAtomicOrdering Order, - LLVMRustSynchronizationScope Scope) { - return wrap(unwrap(B)->CreateFence(fromRust(Order), fromRust(Scope))); -} - enum class LLVMRustAsmDialect { Att, Intel, diff --git a/compiler/rustc_middle/src/mir/interpret/queries.rs b/compiler/rustc_middle/src/mir/interpret/queries.rs index 4207988d700d9..a8c7505329cda 100644 --- a/compiler/rustc_middle/src/mir/interpret/queries.rs +++ b/compiler/rustc_middle/src/mir/interpret/queries.rs @@ -211,7 +211,7 @@ impl<'tcx> TyCtxt<'tcx> { self, param_env: ty::ParamEnv<'tcx>, constant: mir::ConstantKind<'tcx>, - ) -> mir::DestructuredMirConstant<'tcx> { + ) -> mir::DestructuredConstant<'tcx> { self.try_destructure_mir_constant(param_env.and(constant)).unwrap() } } diff --git a/compiler/rustc_middle/src/mir/query.rs b/compiler/rustc_middle/src/mir/query.rs index d89efe2b3f024..efd7357afc46c 100644 --- a/compiler/rustc_middle/src/mir/query.rs +++ b/compiler/rustc_middle/src/mir/query.rs @@ -392,16 +392,9 @@ pub enum ClosureOutlivesSubject<'tcx> { Region(ty::RegionVid), } -/// The constituent parts of a type level constant of kind ADT or array. -#[derive(Copy, Clone, Debug, HashStable)] -pub struct DestructuredConst<'tcx> { - pub variant: Option<VariantIdx>, - pub fields: &'tcx [ty::Const<'tcx>], -} - /// The constituent parts of a mir constant of kind ADT or array. #[derive(Copy, Clone, Debug, HashStable)] -pub struct DestructuredMirConstant<'tcx> { +pub struct DestructuredConstant<'tcx> { pub variant: Option<VariantIdx>, pub fields: &'tcx [ConstantKind<'tcx>], } diff --git a/compiler/rustc_middle/src/mir/syntax.rs b/compiler/rustc_middle/src/mir/syntax.rs index d461b4e434fd7..568d63fb06217 100644 --- a/compiler/rustc_middle/src/mir/syntax.rs +++ b/compiler/rustc_middle/src/mir/syntax.rs @@ -82,9 +82,10 @@ pub enum MirPhase { /// access to. This occurs in generator bodies. Such locals do not behave like other locals, /// because they eg may be aliased in surprising ways. Runtime MIR has no such special locals - /// all generator bodies are lowered and so all places that look like locals really are locals. - /// - Const prop lints: The lint pass which reports eg `200_u8 + 200_u8` as an error is run as a - /// part of analysis to runtime MIR lowering. This means that transformations which may supress - /// such errors may not run on analysis MIR. + /// + /// Also note that the lint pass which reports eg `200_u8 + 200_u8` as an error is run as a part + /// of analysis to runtime MIR lowering. To ensure lints are reported reliably, this means that + /// transformations which may supress such errors should not run on analysis MIR. Runtime(RuntimePhase), } diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 53c254119f436..1afea4864b8dd 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -1007,7 +1007,9 @@ rustc_queries! { /// Tries to destructure an `mir::ConstantKind` ADT or array into its variant index /// and its field values. - query try_destructure_mir_constant(key: ty::ParamEnvAnd<'tcx, mir::ConstantKind<'tcx>>) -> Option<mir::DestructuredMirConstant<'tcx>> { + query try_destructure_mir_constant( + key: ty::ParamEnvAnd<'tcx, mir::ConstantKind<'tcx>> + ) -> Option<mir::DestructuredConstant<'tcx>> { desc { "destructuring mir constant"} remap_env_constness } diff --git a/compiler/rustc_middle/src/ty/consts.rs b/compiler/rustc_middle/src/ty/consts.rs index 339ff4d35930d..4bc821dcfa1fb 100644 --- a/compiler/rustc_middle/src/ty/consts.rs +++ b/compiler/rustc_middle/src/ty/consts.rs @@ -1,9 +1,6 @@ use crate::mir::interpret::LitToConstInput; use crate::mir::ConstantKind; -use crate::ty::{ - self, InlineConstSubsts, InlineConstSubstsParts, InternalSubsts, ParamEnv, ParamEnvAnd, Ty, - TyCtxt, TypeVisitable, -}; +use crate::ty::{self, InternalSubsts, ParamEnv, ParamEnvAnd, Ty, TyCtxt}; use rustc_data_structures::intern::Interned; use rustc_errors::ErrorGuaranteed; use rustc_hir as hir; @@ -151,46 +148,6 @@ impl<'tcx> Const<'tcx> { } } - pub fn from_inline_const(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Self { - debug!("Const::from_inline_const(def_id={:?})", def_id); - - let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); - - let body_id = match tcx.hir().get(hir_id) { - hir::Node::AnonConst(ac) => ac.body, - _ => span_bug!( - tcx.def_span(def_id.to_def_id()), - "from_inline_const can only process anonymous constants" - ), - }; - - let expr = &tcx.hir().body(body_id).value; - - let ty = tcx.typeck(def_id).node_type(hir_id); - - let ret = match Self::try_eval_lit_or_param(tcx, ty, expr) { - Some(v) => v, - None => { - let typeck_root_def_id = tcx.typeck_root_def_id(def_id.to_def_id()); - let parent_substs = - tcx.erase_regions(InternalSubsts::identity_for_item(tcx, typeck_root_def_id)); - let substs = - InlineConstSubsts::new(tcx, InlineConstSubstsParts { parent_substs, ty }) - .substs; - tcx.mk_const(ty::ConstS { - kind: ty::ConstKind::Unevaluated(ty::Unevaluated { - def: ty::WithOptConstParam::unknown(def_id).to_global(), - substs, - promoted: (), - }), - ty, - }) - } - }; - debug_assert!(!ret.has_free_regions()); - ret - } - /// Interns the given value as a constant. #[inline] pub fn from_value(tcx: TyCtxt<'tcx>, val: ty::ValTree<'tcx>, ty: Ty<'tcx>) -> Self { diff --git a/compiler/rustc_middle/src/ty/query.rs b/compiler/rustc_middle/src/ty/query.rs index 36ea7946e9ae4..be208a9fc7041 100644 --- a/compiler/rustc_middle/src/ty/query.rs +++ b/compiler/rustc_middle/src/ty/query.rs @@ -277,8 +277,9 @@ macro_rules! define_callbacks { fn default() -> Self { Providers { $($name: |_, key| bug!( - "`tcx.{}({:?})` unsupported by its crate; \ - perhaps the `{}` query was never assigned a provider function", + "`tcx.{}({:?})` is not supported for external or local crate;\n + hint: Queries can be either made to the local crate, or the external crate. This error means you tried to use it for one that's not supported (likely the local crate).\n + If that's not the case, {} was likely never assigned to a provider function.\n", stringify!($name), key, stringify!($name), diff --git a/compiler/rustc_mir_build/src/thir/pattern/mod.rs b/compiler/rustc_mir_build/src/thir/pattern/mod.rs index 6bb33b5890be8..11645f840c135 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/mod.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/mod.rs @@ -680,7 +680,7 @@ macro_rules! ClonePatternFoldableImpls { } ClonePatternFoldableImpls! { <'tcx> - Span, Field, Mutability, Symbol, LocalVarId, usize, ty::Const<'tcx>, + Span, Field, Mutability, Symbol, LocalVarId, usize, Region<'tcx>, Ty<'tcx>, BindingMode, AdtDef<'tcx>, SubstsRef<'tcx>, &'tcx GenericArg<'tcx>, UserType<'tcx>, UserTypeProjection, CanonicalUserTypeAnnotation<'tcx> diff --git a/compiler/rustc_mir_transform/src/const_prop.rs b/compiler/rustc_mir_transform/src/const_prop.rs index 08ba3f9941d93..cb134a20ea03e 100644 --- a/compiler/rustc_mir_transform/src/const_prop.rs +++ b/compiler/rustc_mir_transform/src/const_prop.rs @@ -471,7 +471,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { return None; } - self.ecx.mir_const_to_op(&c.literal, None).ok() + self.ecx.const_to_op(&c.literal, None).ok() } /// Returns the value, if any, of evaluating `place`. diff --git a/compiler/rustc_mir_transform/src/const_prop_lint.rs b/compiler/rustc_mir_transform/src/const_prop_lint.rs index f16efb365f3cd..2885f06b19cd6 100644 --- a/compiler/rustc_mir_transform/src/const_prop_lint.rs +++ b/compiler/rustc_mir_transform/src/const_prop_lint.rs @@ -292,7 +292,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { return None; } - match self.ecx.mir_const_to_op(&c.literal, None) { + match self.ecx.const_to_op(&c.literal, None) { Ok(op) => Some(op), Err(error) => { let tcx = self.ecx.tcx.at(c.span); diff --git a/compiler/rustc_parse/src/parser/attr_wrapper.rs b/compiler/rustc_parse/src/parser/attr_wrapper.rs index 5fdafd187c660..86c386b94c834 100644 --- a/compiler/rustc_parse/src/parser/attr_wrapper.rs +++ b/compiler/rustc_parse/src/parser/attr_wrapper.rs @@ -459,6 +459,5 @@ fn make_token_stream( panic!("Unexpected last token {:?}", last_token) } } - assert!(stack.is_empty(), "Stack should be empty: final_buf={:?} stack={:?}", final_buf, stack); AttrTokenStream::new(final_buf.inner) } diff --git a/compiler/rustc_serialize/src/opaque.rs b/compiler/rustc_serialize/src/opaque.rs index 5c17ef6ace2d5..f35cc08f4fbfe 100644 --- a/compiler/rustc_serialize/src/opaque.rs +++ b/compiler/rustc_serialize/src/opaque.rs @@ -193,7 +193,9 @@ impl FileEncoder { // shaves an instruction off those code paths (on x86 at least). assert!(capacity <= usize::MAX - max_leb128_len()); - let file = File::create(path)?; + // Create the file for reading and writing, because some encoders do both + // (e.g. the metadata encoder when -Zmeta-stats is enabled) + let file = File::options().read(true).write(true).create(true).truncate(true).open(path)?; Ok(FileEncoder { buf: Box::new_uninit_slice(capacity), diff --git a/compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs b/compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs index 70b9c732ff4bf..23e04ae6a5392 100644 --- a/compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs +++ b/compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs @@ -487,18 +487,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { pub fn array_length_to_const(&self, length: &hir::ArrayLen) -> ty::Const<'tcx> { match length { &hir::ArrayLen::Infer(_, span) => self.ct_infer(self.tcx.types.usize, None, span), - hir::ArrayLen::Body(anon_const) => self.to_const(anon_const), + hir::ArrayLen::Body(anon_const) => { + let const_def_id = self.tcx.hir().local_def_id(anon_const.hir_id); + let span = self.tcx.hir().span(anon_const.hir_id); + let c = ty::Const::from_anon_const(self.tcx, const_def_id); + self.register_wf_obligation(c.into(), span, ObligationCauseCode::WellFormed(None)); + self.normalize_associated_types_in(span, c) + } } } - pub fn to_const(&self, ast_c: &hir::AnonConst) -> ty::Const<'tcx> { - let const_def_id = self.tcx.hir().local_def_id(ast_c.hir_id); - let span = self.tcx.hir().span(ast_c.hir_id); - let c = ty::Const::from_anon_const(self.tcx, const_def_id); - self.register_wf_obligation(c.into(), span, ObligationCauseCode::WellFormed(None)); - self.normalize_associated_types_in(span, c) - } - pub fn const_arg_to_const( &self, ast_c: &hir::AnonConst, diff --git a/compiler/rustc_typeck/src/coherence/orphan.rs b/compiler/rustc_typeck/src/coherence/orphan.rs index 1608550aa6ae4..87961890f53e5 100644 --- a/compiler/rustc_typeck/src/coherence/orphan.rs +++ b/compiler/rustc_typeck/src/coherence/orphan.rs @@ -5,7 +5,6 @@ use rustc_data_structures::fx::FxHashSet; use rustc_errors::struct_span_err; use rustc_errors::{Diagnostic, ErrorGuaranteed}; use rustc_hir as hir; -use rustc_infer::infer::TyCtxtInferExt; use rustc_middle::ty::subst::GenericArgKind; use rustc_middle::ty::subst::InternalSubsts; use rustc_middle::ty::util::IgnoreRegions; @@ -229,12 +228,8 @@ fn emit_orphan_check_error<'tcx>( "only traits defined in the current crate {msg}" ); err.span_label(sp, "impl doesn't use only types from inside the current crate"); - for (ty, is_target_ty) in &tys { - let mut ty = *ty; - tcx.infer_ctxt().enter(|infcx| { - // Remove the lifetimes unnecessary for this error. - ty = infcx.freshen(ty); - }); + for &(mut ty, is_target_ty) in &tys { + ty = tcx.erase_regions(ty); ty = match ty.kind() { // Remove the type arguments from the output, as they are not relevant. // You can think of this as the reverse of `resolve_vars_if_possible`. @@ -264,7 +259,7 @@ fn emit_orphan_check_error<'tcx>( }; let msg = format!("{} is not defined in the current crate{}", ty, postfix); - if *is_target_ty { + if is_target_ty { // Point at `D<A>` in `impl<A, B> for C<B> in D<A>` err.span_label(self_ty_span, &msg); } else { diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index cfa4509428f10..3e324bbb069c6 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -514,7 +514,7 @@ fn item_function(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, f: &cle + name.as_str().len() + generics_len; - wrap_into_docblock(w, |w| { + wrap_into_item_decl(w, |w| { wrap_item(w, "fn", |w| { render_attributes_in_pre(w, it, ""); w.reserve(header_len); @@ -553,7 +553,7 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean: cx.tcx().trait_def(t.def_id).must_implement_one_of.clone(); // Output the trait definition - wrap_into_docblock(w, |w| { + wrap_into_item_decl(w, |w| { wrap_item(w, "trait", |w| { render_attributes_in_pre(w, it, ""); write!( @@ -1033,7 +1033,7 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean: } fn item_trait_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean::TraitAlias) { - wrap_into_docblock(w, |w| { + wrap_into_item_decl(w, |w| { wrap_item(w, "trait-alias", |w| { render_attributes_in_pre(w, it, ""); write!( @@ -1057,7 +1057,7 @@ fn item_trait_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: & } fn item_opaque_ty(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean::OpaqueTy) { - wrap_into_docblock(w, |w| { + wrap_into_item_decl(w, |w| { wrap_item(w, "opaque", |w| { render_attributes_in_pre(w, it, ""); write!( @@ -1096,7 +1096,7 @@ fn item_typedef(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clea }); } - wrap_into_docblock(w, |w| write_content(w, cx, it, t)); + wrap_into_item_decl(w, |w| write_content(w, cx, it, t)); document(w, cx, it, None, HeadingOffset::H2); @@ -1110,7 +1110,7 @@ fn item_typedef(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clea } fn item_union(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean::Union) { - wrap_into_docblock(w, |w| { + wrap_into_item_decl(w, |w| { wrap_item(w, "union", |w| { render_attributes_in_pre(w, it, ""); render_union(w, it, Some(&s.generics), &s.fields, "", cx); @@ -1174,7 +1174,7 @@ fn print_tuple_struct_fields(w: &mut Buffer, cx: &Context<'_>, s: &[clean::Item] fn item_enum(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, e: &clean::Enum) { let count_variants = e.variants().count(); - wrap_into_docblock(w, |w| { + wrap_into_item_decl(w, |w| { wrap_item(w, "enum", |w| { render_attributes_in_pre(w, it, ""); write!( @@ -1333,14 +1333,14 @@ fn item_enum(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, e: &clean:: } fn item_macro(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean::Macro) { - wrap_into_docblock(w, |w| { + wrap_into_item_decl(w, |w| { highlight::render_macro_with_highlighting(&t.source, w); }); document(w, cx, it, None, HeadingOffset::H2) } fn item_proc_macro(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, m: &clean::ProcMacro) { - wrap_into_docblock(w, |w| { + wrap_into_item_decl(w, |w| { let name = it.name.expect("proc-macros always have names"); match m.kind { MacroKind::Bang => { @@ -1387,7 +1387,7 @@ fn item_primitive(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item) { } fn item_constant(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, c: &clean::Constant) { - wrap_into_docblock(w, |w| { + wrap_into_item_decl(w, |w| { wrap_item(w, "const", |w| { render_attributes_in_code(w, it); @@ -1436,7 +1436,7 @@ fn item_constant(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, c: &cle } fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean::Struct) { - wrap_into_docblock(w, |w| { + wrap_into_item_decl(w, |w| { wrap_item(w, "struct", |w| { render_attributes_in_code(w, it); render_struct(w, it, Some(&s.generics), s.struct_type, &s.fields, "", true, cx); @@ -1489,7 +1489,7 @@ fn item_struct(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean } fn item_static(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean::Static) { - wrap_into_docblock(w, |w| { + wrap_into_item_decl(w, |w| { wrap_item(w, "static", |w| { render_attributes_in_code(w, it); write!( @@ -1506,7 +1506,7 @@ fn item_static(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean } fn item_foreign_type(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item) { - wrap_into_docblock(w, |w| { + wrap_into_item_decl(w, |w| { wrap_item(w, "foreigntype", |w| { w.write_str("extern {\n"); render_attributes_in_code(w, it); @@ -1595,11 +1595,11 @@ fn bounds(t_bounds: &[clean::GenericBound], trait_alias: bool, cx: &Context<'_>) bounds } -fn wrap_into_docblock<F>(w: &mut Buffer, f: F) +fn wrap_into_item_decl<F>(w: &mut Buffer, f: F) where F: FnOnce(&mut Buffer), { - w.write_str("<div class=\"docblock item-decl\">"); + w.write_str("<div class=\"item-decl\">"); f(w); w.write_str("</div>") } diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index b4f5bf933a6e3..e985e6c43ade9 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -141,10 +141,6 @@ h1, h2, h3, h4 { h1.fqn { margin: 0; padding: 0; - border-bottom-color: var(--headings-border-bottom-color); -} -h2, h3, h4 { - border-bottom-color: var(--headings-border-bottom-color); } .main-heading { display: flex; @@ -372,9 +368,6 @@ code, pre, a.test-arrow, .code-header { pre { padding: 14px; } -.docblock.item-decl { - margin-left: 0; -} .item-decl pre { overflow-x: auto; } @@ -662,9 +655,6 @@ h2.location a { .docblock h5 { font-size: 1rem; } .docblock h6 { font-size: 0.875rem; } -.docblock h1, .docblock h2, .docblock h3, .docblock h4, .docblock h5, .docblock h6 { - border-bottom-color: var(--headings-border-bottom-color); -} .docblock { margin-left: 24px; diff --git a/src/test/rustdoc-gui/check-code-blocks-margin.goml b/src/test/rustdoc-gui/check-code-blocks-margin.goml index f6266eba75de7..f2fc3e9afc2a2 100644 --- a/src/test/rustdoc-gui/check-code-blocks-margin.goml +++ b/src/test/rustdoc-gui/check-code-blocks-margin.goml @@ -1,6 +1,6 @@ // This test ensures that the docblock elements have the appropriate left margin. goto: file://|DOC_PATH|/test_docs/fn.foo.html // The top docblock elements shouldn't have left margin... -assert-css: ("#main-content .docblock.item-decl", {"margin-left": "0px"}) +assert-css: ("#main-content .item-decl", {"margin-left": "0px"}) // ... but all the others should! -assert-css: ("#main-content .docblock:not(.item-decl)", {"margin-left": "24px"}) +assert-css: ("#main-content .docblock", {"margin-left": "24px"}) diff --git a/src/test/rustdoc-gui/docblock-details.goml b/src/test/rustdoc-gui/docblock-details.goml index f6287ade2f295..c0c4d1b43aac1 100644 --- a/src/test/rustdoc-gui/docblock-details.goml +++ b/src/test/rustdoc-gui/docblock-details.goml @@ -14,7 +14,7 @@ assert-css: ( // We now check that the `<summary>` doesn't have a bottom border and has the correct display. assert-css: ( ".top-doc .docblock summary h4", - {"border-bottom": "0px none rgb(210, 210, 210)"}, + {"border-bottom-width": "0px"}, ) // This allows to ensure that summary is on one line only! assert-property: (".top-doc .docblock summary h4", {"offsetHeight": "33"}) diff --git a/src/test/rustdoc-gui/font-weight.goml b/src/test/rustdoc-gui/font-weight.goml index 5f29fde6689bf..13e8ec9fb16a7 100644 --- a/src/test/rustdoc-gui/font-weight.goml +++ b/src/test/rustdoc-gui/font-weight.goml @@ -1,6 +1,6 @@ // This test checks that the font weight is correctly applied. goto: file://|DOC_PATH|/lib2/struct.Foo.html -assert-css: ("//*[@class='docblock item-decl']//a[text()='Alias']", {"font-weight": "400"}) +assert-css: ("//*[@class='item-decl']//a[text()='Alias']", {"font-weight": "400"}) assert-css: ( "//*[@class='structfield small-section-header']//a[text()='Alias']", {"font-weight": "400"}, @@ -19,7 +19,7 @@ goto: file://|DOC_PATH|/lib2/trait.Trait.html // This is a complex selector, so here's how it works: // -// * //*[@class='docblock item-decl'] — selects element of any tag with classes docblock and item-decl +// * //*[@class='item-decl'] — selects element of any tag with classes docblock and item-decl // * /pre[@class='rust trait'] — selects immediate child with tag pre and classes rust and trait // * /code — selects immediate child with tag code // * /a[@class='constant'] — selects immediate child with tag a and class constant @@ -29,11 +29,11 @@ goto: file://|DOC_PATH|/lib2/trait.Trait.html // This uses '/parent::*' as a proxy for the style of the text node. // We can't just select the '<a>' because intermediate tags could be added. assert-count: ( - "//*[@class='docblock item-decl']/pre[@class='rust trait']/code/a[@class='constant']//text()/parent::*", + "//*[@class='item-decl']/pre[@class='rust trait']/code/a[@class='constant']//text()/parent::*", 1, ) assert-css: ( - "//*[@class='docblock item-decl']/pre[@class='rust trait']/code/a[@class='constant']//text()/parent::*", + "//*[@class='item-decl']/pre[@class='rust trait']/code/a[@class='constant']//text()/parent::*", {"font-weight": "400"}, ) diff --git a/src/test/rustdoc-gui/headings.goml b/src/test/rustdoc-gui/headings.goml index ed07e777b1880..53308e13480bf 100644 --- a/src/test/rustdoc-gui/headings.goml +++ b/src/test/rustdoc-gui/headings.goml @@ -168,19 +168,19 @@ assert-css: ( ) assert-css: ( ".top-doc .docblock h5", - {"color": "rgb(0, 0, 0)", "border-bottom": "0px none rgb(221, 221, 221)"}, + {"color": "rgb(0, 0, 0)", "border-bottom-width": "0px"}, ) assert-css: ( "#implementations-list .docblock h4", - {"color": "rgb(0, 0, 0)", "border-bottom": "0px none rgb(221, 221, 221)"}, + {"color": "rgb(0, 0, 0)", "border-bottom-width": "0px"}, ) assert-css: ( "#implementations-list .docblock h5", - {"color": "rgb(0, 0, 0)", "border-bottom": "0px none rgb(221, 221, 221)"}, + {"color": "rgb(0, 0, 0)", "border-bottom-width": "0px"}, ) assert-css: ( "#implementations-list .docblock h6", - {"color": "rgb(0, 0, 0)", "border-bottom": "0px none rgb(221, 221, 221)"}, + {"color": "rgb(0, 0, 0)", "border-bottom-width": "0px"}, ) local-storage: {"rustdoc-theme": "dark"} @@ -199,19 +199,19 @@ assert-css: ( ) assert-css: ( ".top-doc .docblock h5", - {"color": "rgb(221, 221, 221)", "border-bottom": "0px none rgb(210, 210, 210)"}, + {"color": "rgb(221, 221, 221)", "border-bottom-width": "0px"}, ) assert-css: ( "#implementations-list .docblock h4", - {"color": "rgb(221, 221, 221)", "border-bottom": "0px none rgb(210, 210, 210)"}, + {"color": "rgb(221, 221, 221)", "border-bottom-width": "0px"}, ) assert-css: ( "#implementations-list .docblock h5", - {"color": "rgb(221, 221, 221)", "border-bottom": "0px none rgb(210, 210, 210)"}, + {"color": "rgb(221, 221, 221)", "border-bottom-width": "0px"}, ) assert-css: ( "#implementations-list .docblock h6", - {"color": "rgb(221, 221, 221)", "border-bottom": "0px none rgb(210, 210, 210)"}, + {"color": "rgb(221, 221, 221)", "border-bottom-width": "0px"}, ) local-storage: {"rustdoc-theme": "ayu"} @@ -230,19 +230,19 @@ assert-css: ( ) assert-css: ( ".top-doc .docblock h5", - {"color": "rgb(197, 197, 197)", "border-bottom": "0px none rgb(92, 103, 115)"}, + {"color": "rgb(197, 197, 197)", "border-bottom-width": "0px"}, ) assert-css: ( "#implementations-list .docblock h4", - {"color": "rgb(255, 255, 255)", "border-bottom": "0px none rgb(92, 103, 115)"}, + {"color": "rgb(255, 255, 255)", "border-bottom-width": "0px"}, ) assert-css: ( "#implementations-list .docblock h5", - {"color": "rgb(197, 197, 197)", "border-bottom": "0px none rgb(92, 103, 115)"}, + {"color": "rgb(197, 197, 197)", "border-bottom-width": "0px"}, ) assert-css: ( "#implementations-list .docblock h6", - {"color": "rgb(197, 197, 197)", "border-bottom": "0px none rgb(92, 103, 115)"}, + {"color": "rgb(197, 197, 197)", "border-bottom-width": "0px"}, ) local-storage: {"rustdoc-theme": "light"} diff --git a/src/test/rustdoc-gui/item-info-overflow.goml b/src/test/rustdoc-gui/item-info-overflow.goml index b7095a3c5324d..17478da4fea29 100644 --- a/src/test/rustdoc-gui/item-info-overflow.goml +++ b/src/test/rustdoc-gui/item-info-overflow.goml @@ -3,7 +3,7 @@ goto: file://|DOC_PATH|/lib2/struct.LongItemInfo.html // We set a fixed size so there is no chance of "random" resize. size: (1200, 870) // Logically, the "item-decl" and the "item-info" should have the same scroll width. -compare-elements-property: (".docblock.item-decl", ".item-info", ["scrollWidth"]) +compare-elements-property: (".item-decl", ".item-info", ["scrollWidth"]) assert-property: (".item-info", {"scrollWidth": "890"}) // Just to be sure we're comparing the correct "item-info": assert-text: ( diff --git a/src/test/rustdoc-gui/notable-trait.goml b/src/test/rustdoc-gui/notable-trait.goml new file mode 100644 index 0000000000000..69088a0774fc4 --- /dev/null +++ b/src/test/rustdoc-gui/notable-trait.goml @@ -0,0 +1,91 @@ +// This test checks the position of the `i` for the notable traits. +goto: file://|DOC_PATH|/test_docs/struct.NotableStructWithLongName.html +show-text: true +// We start with a wide screen. +size: (1100, 600) +// Checking they have the same y position. +compare-elements-position: ( + "//*[@id='method.create_an_iterator_from_read']//a[text()='NotableStructWithLongName']", + "//*[@id='method.create_an_iterator_from_read']//*[@class='notable-traits']", + ("y"), +) +// Checking they don't have the same x position. +compare-elements-position-false: ( + "//*[@id='method.create_an_iterator_from_read']//a[text()='NotableStructWithLongName']", + "//*[@id='method.create_an_iterator_from_read']//*[@class='notable-traits']", + ("x"), +) +// The `i` should be *after* the type. +assert-position: ( + "//*[@id='method.create_an_iterator_from_read']//a[text()='NotableStructWithLongName']", + {"x": 692}, +) +assert-position: ( + "//*[@id='method.create_an_iterator_from_read']//*[@class='notable-traits']", + {"x": 966}, +) + + +// Now only the `i` should be on the next line. +size: (1055, 600) +compare-elements-position-false: ( + "//*[@id='method.create_an_iterator_from_read']//a[text()='NotableStructWithLongName']", + "//*[@id='method.create_an_iterator_from_read']//*[@class='notable-traits']", + ("y", "x"), +) + +// Now both the `i` and the struct name should be on the next line. +size: (980, 600) +// Checking they have the same y position. +compare-elements-position: ( + "//*[@id='method.create_an_iterator_from_read']//a[text()='NotableStructWithLongName']", + "//*[@id='method.create_an_iterator_from_read']//*[@class='notable-traits']", + ("y"), +) +// Checking they don't have the same x position. +compare-elements-position-false: ( + "//*[@id='method.create_an_iterator_from_read']//a[text()='NotableStructWithLongName']", + "//*[@id='method.create_an_iterator_from_read']//*[@class='notable-traits']", + ("x"), +) +// The `i` should be *after* the type. +assert-position: ( + "//*[@id='method.create_an_iterator_from_read']//a[text()='NotableStructWithLongName']", + {"x": 245}, +) +assert-position: ( + "//*[@id='method.create_an_iterator_from_read']//*[@class='notable-traits']", + {"x": 519}, +) + +// Checking on mobile now. +size: (650, 600) +// Checking they have the same y position. +compare-elements-position: ( + "//*[@id='method.create_an_iterator_from_read']//a[text()='NotableStructWithLongName']", + "//*[@id='method.create_an_iterator_from_read']//*[@class='notable-traits']", + ("y"), +) +// Checking they don't have the same x position. +compare-elements-position-false: ( + "//*[@id='method.create_an_iterator_from_read']//a[text()='NotableStructWithLongName']", + "//*[@id='method.create_an_iterator_from_read']//*[@class='notable-traits']", + ("x"), +) +// The `i` should be *after* the type. +assert-position: ( + "//*[@id='method.create_an_iterator_from_read']//a[text()='NotableStructWithLongName']", + {"x": 15}, +) +assert-position: ( + "//*[@id='method.create_an_iterator_from_read']//*[@class='notable-traits']", + {"x": 289}, +) + +// Checking on very small mobile. The `i` should be on its own line. +size: (410, 600) +compare-elements-position-false: ( + "//*[@id='method.create_an_iterator_from_read']//a[text()='NotableStructWithLongName']", + "//*[@id='method.create_an_iterator_from_read']//*[@class='notable-traits']", + ("y", "x"), +) diff --git a/src/test/rustdoc-gui/src/test_docs/lib.rs b/src/test/rustdoc-gui/src/test_docs/lib.rs index 4eedf7f15c3d4..e6177ff84b42e 100644 --- a/src/test/rustdoc-gui/src/test_docs/lib.rs +++ b/src/test/rustdoc-gui/src/test_docs/lib.rs @@ -325,3 +325,15 @@ pub mod doc_block_table { } } + +pub struct NotableStructWithLongName<R>(R); + +impl<R: std::io::Read> NotableStructWithLongName<R> { + pub fn create_an_iterator_from_read(r: R) -> NotableStructWithLongName<R> { Self(r) } +} + +impl<R: std::io::Read> std::iter::Iterator for NotableStructWithLongName<R> { + type Item = (); + + fn next(&mut self) -> Option<Self::Item> { () } +} diff --git a/src/test/rustdoc-ui/normalize-overflow.rs b/src/test/rustdoc-ui/normalize-overflow.rs index 0cdcc88e3141f..3698fe70e7f7e 100644 --- a/src/test/rustdoc-ui/normalize-overflow.rs +++ b/src/test/rustdoc-ui/normalize-overflow.rs @@ -1,3 +1,5 @@ // aux-crate:overflow=overflow.rs // check-pass // Regression test for <https://github.com/rust-lang/rust/issues/79506>. + +extern crate overflow; diff --git a/src/test/rustdoc/attribute-rendering.rs b/src/test/rustdoc/attribute-rendering.rs index 6777871846e2d..36e10923c8535 100644 --- a/src/test/rustdoc/attribute-rendering.rs +++ b/src/test/rustdoc/attribute-rendering.rs @@ -1,7 +1,7 @@ #![crate_name = "foo"] // @has 'foo/fn.f.html' -// @has - //*[@'class="docblock item-decl"]' '#[export_name = "f"] pub fn f()' +// @has - //*[@'class="item-decl"]' '#[export_name = "f"] pub fn f()' #[export_name = "\ f"] pub fn f() {} diff --git a/src/test/rustdoc/attributes.rs b/src/test/rustdoc/attributes.rs index 1c7f4b7241893..a36dadced87d7 100644 --- a/src/test/rustdoc/attributes.rs +++ b/src/test/rustdoc/attributes.rs @@ -8,6 +8,6 @@ pub extern "C" fn f() {} #[export_name = "bar"] pub extern "C" fn g() {} -// @has foo/struct.Repr.html '//*[@class="docblock item-decl"]' '#[repr(C, align(8))]' +// @has foo/struct.Repr.html '//*[@class="item-decl"]' '#[repr(C, align(8))]' #[repr(C, align(8))] pub struct Repr; diff --git a/src/test/rustdoc/const-value-display.rs b/src/test/rustdoc/const-value-display.rs index 5b2f3c48d57fa..8d95f0de9d098 100644 --- a/src/test/rustdoc/const-value-display.rs +++ b/src/test/rustdoc/const-value-display.rs @@ -1,9 +1,9 @@ #![crate_name = "foo"] // @has 'foo/constant.HOUR_IN_SECONDS.html' -// @has - '//*[@class="docblock item-decl"]//code' 'pub const HOUR_IN_SECONDS: u64 = _; // 3_600u64' +// @has - '//*[@class="item-decl"]//code' 'pub const HOUR_IN_SECONDS: u64 = _; // 3_600u64' pub const HOUR_IN_SECONDS: u64 = 60 * 60; // @has 'foo/constant.NEGATIVE.html' -// @has - '//*[@class="docblock item-decl"]//code' 'pub const NEGATIVE: i64 = _; // -3_600i64' +// @has - '//*[@class="item-decl"]//code' 'pub const NEGATIVE: i64 = _; // -3_600i64' pub const NEGATIVE: i64 = -60 * 60; diff --git a/src/test/rustdoc/decl-trailing-whitespace.rs b/src/test/rustdoc/decl-trailing-whitespace.rs index 46a2307abef02..e47edc1321851 100644 --- a/src/test/rustdoc/decl-trailing-whitespace.rs +++ b/src/test/rustdoc/decl-trailing-whitespace.rs @@ -7,7 +7,7 @@ pub struct Error; // @has 'foo/trait.Write.html' pub trait Write { - // @snapshot 'declaration' - '//*[@class="docblock item-decl"]//code' + // @snapshot 'declaration' - '//*[@class="item-decl"]//code' fn poll_write( self: Option<String>, cx: &mut Option<String>, diff --git a/src/test/rustdoc/macro-higher-kinded-function.rs b/src/test/rustdoc/macro-higher-kinded-function.rs index 02a4305644e7d..b8c52b7b791d6 100644 --- a/src/test/rustdoc/macro-higher-kinded-function.rs +++ b/src/test/rustdoc/macro-higher-kinded-function.rs @@ -11,8 +11,8 @@ macro_rules! gen { } // @has 'foo/struct.Providers.html' -// @has - '//*[@class="docblock item-decl"]//code' "pub a: for<'tcx> fn(_: TyCtxt<'tcx>, _: u8) -> i8," -// @has - '//*[@class="docblock item-decl"]//code' "pub b: for<'tcx> fn(_: TyCtxt<'tcx>, _: u16) -> i16," +// @has - '//*[@class="item-decl"]//code' "pub a: for<'tcx> fn(_: TyCtxt<'tcx>, _: u8) -> i8," +// @has - '//*[@class="item-decl"]//code' "pub b: for<'tcx> fn(_: TyCtxt<'tcx>, _: u16) -> i16," // @has - '//*[@id="structfield.a"]/code' "a: for<'tcx> fn(_: TyCtxt<'tcx>, _: u8) -> i8" // @has - '//*[@id="structfield.b"]/code' "b: for<'tcx> fn(_: TyCtxt<'tcx>, _: u16) -> i16" gen! { diff --git a/src/test/rustdoc/reexport-dep-foreign-fn.rs b/src/test/rustdoc/reexport-dep-foreign-fn.rs index 6e1dc453982d9..6694c91d10442 100644 --- a/src/test/rustdoc/reexport-dep-foreign-fn.rs +++ b/src/test/rustdoc/reexport-dep-foreign-fn.rs @@ -8,5 +8,5 @@ extern crate all_item_types; // @has 'foo/fn.foo_ffn.html' -// @has - '//*[@class="docblock item-decl"]//code' 'pub unsafe extern "C" fn foo_ffn()' +// @has - '//*[@class="item-decl"]//code' 'pub unsafe extern "C" fn foo_ffn()' pub use all_item_types::foo_ffn; diff --git a/src/test/rustdoc/reexports-priv.rs b/src/test/rustdoc/reexports-priv.rs index aea9b9f2b395d..11364e7f707ef 100644 --- a/src/test/rustdoc/reexports-priv.rs +++ b/src/test/rustdoc/reexports-priv.rs @@ -5,7 +5,7 @@ extern crate reexports; -// @has 'foo/macro.addr_of.html' '//*[@class="docblock item-decl"]' 'pub macro addr_of($place:expr) {' +// @has 'foo/macro.addr_of.html' '//*[@class="item-decl"]' 'pub macro addr_of($place:expr) {' pub use reexports::addr_of; // @!has 'foo/macro.addr_of_crate.html' pub(crate) use reexports::addr_of_crate; @@ -14,7 +14,7 @@ pub(self) use reexports::addr_of_self; // @!has 'foo/macro.addr_of_local.html' use reexports::addr_of_local; -// @has 'foo/struct.Foo.html' '//*[@class="docblock item-decl"]' 'pub struct Foo;' +// @has 'foo/struct.Foo.html' '//*[@class="item-decl"]' 'pub struct Foo;' pub use reexports::Foo; // @!has 'foo/struct.FooCrate.html' pub(crate) use reexports::FooCrate; @@ -23,7 +23,7 @@ pub(self) use reexports::FooSelf; // @!has 'foo/struct.FooLocal.html' use reexports::FooLocal; -// @has 'foo/enum.Bar.html' '//*[@class="docblock item-decl"]' 'pub enum Bar {' +// @has 'foo/enum.Bar.html' '//*[@class="item-decl"]' 'pub enum Bar {' pub use reexports::Bar; // @!has 'foo/enum.BarCrate.html' pub(crate) use reexports::BarCrate; @@ -50,7 +50,7 @@ pub(self) use reexports::TypeSelf; // @!has 'foo/type.TypeLocal.html' use reexports::TypeLocal; -// @has 'foo/union.Union.html' '//*[@class="docblock item-decl"]' 'pub union Union {' +// @has 'foo/union.Union.html' '//*[@class="item-decl"]' 'pub union Union {' pub use reexports::Union; // @!has 'foo/union.UnionCrate.html' pub(crate) use reexports::UnionCrate; @@ -61,33 +61,33 @@ use reexports::UnionLocal; pub mod outer { pub mod inner { - // @has 'foo/outer/inner/macro.addr_of.html' '//*[@class="docblock item-decl"]' 'pub macro addr_of($place:expr) {' + // @has 'foo/outer/inner/macro.addr_of.html' '//*[@class="item-decl"]' 'pub macro addr_of($place:expr) {' pub use reexports::addr_of; - // @has 'foo/outer/inner/macro.addr_of_crate.html' '//*[@class="docblock item-decl"]' 'pub(crate) macro addr_of_crate($place:expr) {' + // @has 'foo/outer/inner/macro.addr_of_crate.html' '//*[@class="item-decl"]' 'pub(crate) macro addr_of_crate($place:expr) {' pub(crate) use reexports::addr_of_crate; - // @has 'foo/outer/inner/macro.addr_of_super.html' '//*[@class="docblock item-decl"]' 'pub(in outer) macro addr_of_super($place:expr) {' + // @has 'foo/outer/inner/macro.addr_of_super.html' '//*[@class="item-decl"]' 'pub(in outer) macro addr_of_super($place:expr) {' pub(super) use reexports::addr_of_super; // @!has 'foo/outer/inner/macro.addr_of_self.html' pub(self) use reexports::addr_of_self; // @!has 'foo/outer/inner/macro.addr_of_local.html' use reexports::addr_of_local; - // @has 'foo/outer/inner/struct.Foo.html' '//*[@class="docblock item-decl"]' 'pub struct Foo;' + // @has 'foo/outer/inner/struct.Foo.html' '//*[@class="item-decl"]' 'pub struct Foo;' pub use reexports::Foo; - // @has 'foo/outer/inner/struct.FooCrate.html' '//*[@class="docblock item-decl"]' 'pub(crate) struct FooCrate;' + // @has 'foo/outer/inner/struct.FooCrate.html' '//*[@class="item-decl"]' 'pub(crate) struct FooCrate;' pub(crate) use reexports::FooCrate; - // @has 'foo/outer/inner/struct.FooSuper.html' '//*[@class="docblock item-decl"]' 'pub(in outer) struct FooSuper;' + // @has 'foo/outer/inner/struct.FooSuper.html' '//*[@class="item-decl"]' 'pub(in outer) struct FooSuper;' pub(super) use reexports::FooSuper; // @!has 'foo/outer/inner/struct.FooSelf.html' pub(self) use reexports::FooSelf; // @!has 'foo/outer/inner/struct.FooLocal.html' use reexports::FooLocal; - // @has 'foo/outer/inner/enum.Bar.html' '//*[@class="docblock item-decl"]' 'pub enum Bar {' + // @has 'foo/outer/inner/enum.Bar.html' '//*[@class="item-decl"]' 'pub enum Bar {' pub use reexports::Bar; - // @has 'foo/outer/inner/enum.BarCrate.html' '//*[@class="docblock item-decl"]' 'pub(crate) enum BarCrate {' + // @has 'foo/outer/inner/enum.BarCrate.html' '//*[@class="item-decl"]' 'pub(crate) enum BarCrate {' pub(crate) use reexports::BarCrate; - // @has 'foo/outer/inner/enum.BarSuper.html' '//*[@class="docblock item-decl"]' 'pub(in outer) enum BarSuper {' + // @has 'foo/outer/inner/enum.BarSuper.html' '//*[@class="item-decl"]' 'pub(in outer) enum BarSuper {' pub(super) use reexports::BarSuper; // @!has 'foo/outer/inner/enum.BarSelf.html' pub(self) use reexports::BarSelf; @@ -116,11 +116,11 @@ pub mod outer { // @!has 'foo/outer/inner/type.TypeLocal.html' use reexports::TypeLocal; - // @has 'foo/outer/inner/union.Union.html' '//*[@class="docblock item-decl"]' 'pub union Union {' + // @has 'foo/outer/inner/union.Union.html' '//*[@class="item-decl"]' 'pub union Union {' pub use reexports::Union; - // @has 'foo/outer/inner/union.UnionCrate.html' '//*[@class="docblock item-decl"]' 'pub(crate) union UnionCrate {' + // @has 'foo/outer/inner/union.UnionCrate.html' '//*[@class="item-decl"]' 'pub(crate) union UnionCrate {' pub(crate) use reexports::UnionCrate; - // @has 'foo/outer/inner/union.UnionSuper.html' '//*[@class="docblock item-decl"]' 'pub(in outer) union UnionSuper {' + // @has 'foo/outer/inner/union.UnionSuper.html' '//*[@class="item-decl"]' 'pub(in outer) union UnionSuper {' pub(super) use reexports::UnionSuper; // @!has 'foo/outer/inner/union.UnionSelf.html' pub(self) use reexports::UnionSelf; diff --git a/src/test/rustdoc/reexports.rs b/src/test/rustdoc/reexports.rs index 7abcbfb618122..9aa6d7224baca 100644 --- a/src/test/rustdoc/reexports.rs +++ b/src/test/rustdoc/reexports.rs @@ -4,7 +4,7 @@ extern crate reexports; -// @has 'foo/macro.addr_of.html' '//*[@class="docblock item-decl"]' 'pub macro addr_of($place:expr) {' +// @has 'foo/macro.addr_of.html' '//*[@class="item-decl"]' 'pub macro addr_of($place:expr) {' pub use reexports::addr_of; // @!has 'foo/macro.addr_of_crate.html' pub(crate) use reexports::addr_of_crate; @@ -13,7 +13,7 @@ pub(self) use reexports::addr_of_self; // @!has 'foo/macro.addr_of_local.html' use reexports::addr_of_local; -// @has 'foo/struct.Foo.html' '//*[@class="docblock item-decl"]' 'pub struct Foo;' +// @has 'foo/struct.Foo.html' '//*[@class="item-decl"]' 'pub struct Foo;' pub use reexports::Foo; // @!has 'foo/struct.FooCrate.html' pub(crate) use reexports::FooCrate; @@ -22,7 +22,7 @@ pub(self) use reexports::FooSelf; // @!has 'foo/struct.FooLocal.html' use reexports::FooLocal; -// @has 'foo/enum.Bar.html' '//*[@class="docblock item-decl"]' 'pub enum Bar {' +// @has 'foo/enum.Bar.html' '//*[@class="item-decl"]' 'pub enum Bar {' pub use reexports::Bar; // @!has 'foo/enum.BarCrate.html' pub(crate) use reexports::BarCrate; @@ -49,7 +49,7 @@ pub(self) use reexports::TypeSelf; // @!has 'foo/type.TypeLocal.html' use reexports::TypeLocal; -// @has 'foo/union.Union.html' '//*[@class="docblock item-decl"]' 'pub union Union {' +// @has 'foo/union.Union.html' '//*[@class="item-decl"]' 'pub union Union {' pub use reexports::Union; // @!has 'foo/union.UnionCrate.html' pub(crate) use reexports::UnionCrate; @@ -60,7 +60,7 @@ use reexports::UnionLocal; pub mod outer { pub mod inner { - // @has 'foo/outer/inner/macro.addr_of.html' '//*[@class="docblock item-decl"]' 'pub macro addr_of($place:expr) {' + // @has 'foo/outer/inner/macro.addr_of.html' '//*[@class="item-decl"]' 'pub macro addr_of($place:expr) {' pub use reexports::addr_of; // @!has 'foo/outer/inner/macro.addr_of_crate.html' pub(crate) use reexports::addr_of_crate; @@ -71,7 +71,7 @@ pub mod outer { // @!has 'foo/outer/inner/macro.addr_of_local.html' use reexports::addr_of_local; - // @has 'foo/outer/inner/struct.Foo.html' '//*[@class="docblock item-decl"]' 'pub struct Foo;' + // @has 'foo/outer/inner/struct.Foo.html' '//*[@class="item-decl"]' 'pub struct Foo;' pub use reexports::Foo; // @!has 'foo/outer/inner/struct.FooCrate.html' pub(crate) use reexports::FooCrate; @@ -82,7 +82,7 @@ pub mod outer { // @!has 'foo/outer/inner/struct.FooLocal.html' use reexports::FooLocal; - // @has 'foo/outer/inner/enum.Bar.html' '//*[@class="docblock item-decl"]' 'pub enum Bar {' + // @has 'foo/outer/inner/enum.Bar.html' '//*[@class="item-decl"]' 'pub enum Bar {' pub use reexports::Bar; // @!has 'foo/outer/inner/enum.BarCrate.html' pub(crate) use reexports::BarCrate; @@ -115,7 +115,7 @@ pub mod outer { // @!has 'foo/outer/inner/type.TypeLocal.html' use reexports::TypeLocal; - // @has 'foo/outer/inner/union.Union.html' '//*[@class="docblock item-decl"]' 'pub union Union {' + // @has 'foo/outer/inner/union.Union.html' '//*[@class="item-decl"]' 'pub union Union {' pub use reexports::Union; // @!has 'foo/outer/inner/union.UnionCrate.html' pub(crate) use reexports::UnionCrate; diff --git a/src/test/rustdoc/toggle-item-contents.rs b/src/test/rustdoc/toggle-item-contents.rs index dbaf195e1a963..47a1d62f5a7a3 100644 --- a/src/test/rustdoc/toggle-item-contents.rs +++ b/src/test/rustdoc/toggle-item-contents.rs @@ -55,7 +55,7 @@ pub union Union { // @has 'toggle_item_contents/struct.PrivStruct.html' // @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 0 -// @has - '//div[@class="docblock item-decl"]' '/* private fields */' +// @has - '//div[@class="item-decl"]' '/* private fields */' pub struct PrivStruct { a: usize, b: usize, diff --git a/src/test/rustdoc/trait_alias.rs b/src/test/rustdoc/trait_alias.rs index a0c657d9a054d..791c099cc5233 100644 --- a/src/test/rustdoc/trait_alias.rs +++ b/src/test/rustdoc/trait_alias.rs @@ -14,13 +14,13 @@ use std::fmt::Debug; // @has foo/index.html '//a[@class="traitalias"]' 'Foo' // @has foo/traitalias.CopyAlias.html -// @has - '//section[@id="main-content"]/div[@class="docblock item-decl"]/pre' 'trait CopyAlias = Copy;' +// @has - '//section[@id="main-content"]/div[@class="item-decl"]/pre' 'trait CopyAlias = Copy;' pub trait CopyAlias = Copy; // @has foo/traitalias.Alias2.html -// @has - '//section[@id="main-content"]/div[@class="docblock item-decl"]/pre' 'trait Alias2 = Copy + Debug;' +// @has - '//section[@id="main-content"]/div[@class="item-decl"]/pre' 'trait Alias2 = Copy + Debug;' pub trait Alias2 = Copy + Debug; // @has foo/traitalias.Foo.html -// @has - '//section[@id="main-content"]/div[@class="docblock item-decl"]/pre' 'trait Foo<T> = Into<T> + Debug;' +// @has - '//section[@id="main-content"]/div[@class="item-decl"]/pre' 'trait Foo<T> = Into<T> + Debug;' pub trait Foo<T> = Into<T> + Debug; // @has foo/fn.bar.html '//a[@href="traitalias.Alias2.html"]' 'Alias2' pub fn bar<T>() where T: Alias2 {} diff --git a/src/test/rustdoc/where.SWhere_Simd_item-decl.html b/src/test/rustdoc/where.SWhere_Simd_item-decl.html index 0133bcaeb6673..6c1b5d3151352 100644 --- a/src/test/rustdoc/where.SWhere_Simd_item-decl.html +++ b/src/test/rustdoc/where.SWhere_Simd_item-decl.html @@ -1 +1 @@ -<div class="docblock item-decl"><pre class="rust struct"><code>pub struct Simd<T>(_) <br /><span class="where">where<br />    T: <a class="trait" href="trait.MyTrait.html" title="trait foo::MyTrait">MyTrait</a></span>;</code></pre></div> \ No newline at end of file +<div class="item-decl"><pre class="rust struct"><code>pub struct Simd<T>(_)<br /><span class="where">where<br />    T: <a class="trait" href="trait.MyTrait.html" title="trait foo::MyTrait">MyTrait</a></span>;</code></pre></div> \ No newline at end of file diff --git a/src/test/rustdoc/where.SWhere_TraitWhere_item-decl.html b/src/test/rustdoc/where.SWhere_TraitWhere_item-decl.html index 54026ff034e00..0fbdc0c9cd1e8 100644 --- a/src/test/rustdoc/where.SWhere_TraitWhere_item-decl.html +++ b/src/test/rustdoc/where.SWhere_TraitWhere_item-decl.html @@ -1,3 +1,3 @@ -<div class="docblock item-decl"><pre class="rust trait"><code>pub trait TraitWhere { - type <a href="#associatedtype.Item" class="associatedtype">Item</a><'a><br />    <span class="where">where<br />        Self: 'a</span>; +<div class="item-decl"><pre class="rust trait"><code>pub trait TraitWhere { + type <a href="#associatedtype.Item" class="associatedtype">Item</a><'a><br />   <span class="where">where<br />        Self: 'a</span>; }</code></pre></div> \ No newline at end of file diff --git a/src/test/rustdoc/where.rs b/src/test/rustdoc/where.rs index b1034c707f55a..8818d74ddd087 100644 --- a/src/test/rustdoc/where.rs +++ b/src/test/rustdoc/where.rs @@ -20,13 +20,13 @@ impl<D> Delta<D> where D: MyTrait { pub struct Echo<E>(E); // @has 'foo/struct.Simd.html' -// @snapshot SWhere_Simd_item-decl - '//div[@class="docblock item-decl"]' +// @snapshot SWhere_Simd_item-decl - '//div[@class="item-decl"]' pub struct Simd<T>([T; 1]) where T: MyTrait; // @has 'foo/trait.TraitWhere.html' -// @snapshot SWhere_TraitWhere_item-decl - '//div[@class="docblock item-decl"]' +// @snapshot SWhere_TraitWhere_item-decl - '//div[@class="item-decl"]' pub trait TraitWhere { type Item<'a> where Self: 'a; } diff --git a/src/test/rustdoc/whitespace-after-where-clause.enum.html b/src/test/rustdoc/whitespace-after-where-clause.enum.html index 9e5bf45ae7d2f..c74866f4a10b8 100644 --- a/src/test/rustdoc/whitespace-after-where-clause.enum.html +++ b/src/test/rustdoc/whitespace-after-where-clause.enum.html @@ -1,4 +1,4 @@ -<div class="docblock item-decl"><pre class="rust enum"><code>pub enum Cow<'a, B: ?<a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + 'a> <span class="where fmt-newline">where<br />    B: <a class="trait" href="trait.ToOwned.html" title="trait foo::ToOwned">ToOwned</a><dyn <a class="trait" href="{{channel}}/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>>, </span>{ +<div class="item-decl"><pre class="rust enum"><code>pub enum Cow<'a, B: ?<a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + 'a><span class="where fmt-newline">where<br />    B: <a class="trait" href="trait.ToOwned.html" title="trait foo::ToOwned">ToOwned</a><dyn <a class="trait" href="{{channel}}/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>>,</span>{ Borrowed(<a class="primitive" href="{{channel}}/std/primitive.reference.html">&'a </a>B), Whatever(<a class="primitive" href="{{channel}}/std/primitive.u32.html">u32</a>), -}</code></pre></div> +}</code></pre></div> \ No newline at end of file diff --git a/src/test/rustdoc/whitespace-after-where-clause.enum2.html b/src/test/rustdoc/whitespace-after-where-clause.enum2.html index 6bc47beaed125..ac7d775982110 100644 --- a/src/test/rustdoc/whitespace-after-where-clause.enum2.html +++ b/src/test/rustdoc/whitespace-after-where-clause.enum2.html @@ -1,4 +1,4 @@ -<div class="docblock item-decl"><pre class="rust enum"><code>pub enum Cow2<'a, B: ?<a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + <a class="trait" href="trait.ToOwned.html" title="trait foo::ToOwned">ToOwned</a><dyn <a class="trait" href="{{channel}}/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>> + 'a> { +<div class="item-decl"><pre class="rust enum"><code>pub enum Cow2<'a, B: ?<a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + <a class="trait" href="trait.ToOwned.html" title="trait foo::ToOwned">ToOwned</a><dyn <a class="trait" href="{{channel}}/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>> + 'a> { Borrowed(<a class="primitive" href="{{channel}}/std/primitive.reference.html">&'a </a>B), Whatever(<a class="primitive" href="{{channel}}/std/primitive.u32.html">u32</a>), -}</code></pre></div> +}</code></pre></div> \ No newline at end of file diff --git a/src/test/rustdoc/whitespace-after-where-clause.rs b/src/test/rustdoc/whitespace-after-where-clause.rs index c36386a2aa2b5..4b740b970fc20 100644 --- a/src/test/rustdoc/whitespace-after-where-clause.rs +++ b/src/test/rustdoc/whitespace-after-where-clause.rs @@ -4,7 +4,7 @@ #![crate_name = "foo"] // @has 'foo/trait.ToOwned.html' -// @snapshot trait - '//*[@class="docblock item-decl"]' +// @snapshot trait - '//*[@class="item-decl"]' pub trait ToOwned<T> where T: Clone { @@ -14,7 +14,7 @@ where T: Clone } // @has 'foo/trait.ToOwned2.html' -// @snapshot trait2 - '//*[@class="docblock item-decl"]' +// @snapshot trait2 - '//*[@class="item-decl"]' // There should be a whitespace before `{` in this case! pub trait ToOwned2<T: Clone> { type Owned; @@ -23,7 +23,7 @@ pub trait ToOwned2<T: Clone> { } // @has 'foo/enum.Cow.html' -// @snapshot enum - '//*[@class="docblock item-decl"]' +// @snapshot enum - '//*[@class="item-decl"]' pub enum Cow<'a, B: ?Sized + 'a> where B: ToOwned<Clone>, @@ -33,7 +33,7 @@ where } // @has 'foo/enum.Cow2.html' -// @snapshot enum2 - '//*[@class="docblock item-decl"]' +// @snapshot enum2 - '//*[@class="item-decl"]' // There should be a whitespace before `{` in this case! pub enum Cow2<'a, B: ?Sized + ToOwned<Clone> + 'a> { Borrowed(&'a B), @@ -41,7 +41,7 @@ pub enum Cow2<'a, B: ?Sized + ToOwned<Clone> + 'a> { } // @has 'foo/struct.Struct.html' -// @snapshot struct - '//*[@class="docblock item-decl"]' +// @snapshot struct - '//*[@class="item-decl"]' pub struct Struct<'a, B: ?Sized + 'a> where B: ToOwned<Clone>, @@ -51,7 +51,7 @@ where } // @has 'foo/struct.Struct2.html' -// @snapshot struct2 - '//*[@class="docblock item-decl"]' +// @snapshot struct2 - '//*[@class="item-decl"]' // There should be a whitespace before `{` in this case! pub struct Struct2<'a, B: ?Sized + ToOwned<Clone> + 'a> { pub a: &'a B, @@ -59,7 +59,7 @@ pub struct Struct2<'a, B: ?Sized + ToOwned<Clone> + 'a> { } // @has 'foo/union.Union.html' -// @snapshot union - '//*[@class="docblock item-decl"]' +// @snapshot union - '//*[@class="item-decl"]' pub union Union<'a, B: ?Sized + 'a> where B: ToOwned<Clone>, @@ -69,7 +69,7 @@ where } // @has 'foo/union.Union2.html' -// @snapshot union2 - '//*[@class="docblock item-decl"]' +// @snapshot union2 - '//*[@class="item-decl"]' // There should be a whitespace before `{` in this case! pub union Union2<'a, B: ?Sized + ToOwned<Clone> + 'a> { a: &'a B, diff --git a/src/test/rustdoc/whitespace-after-where-clause.struct.html b/src/test/rustdoc/whitespace-after-where-clause.struct.html index 236cc3b30d088..1ba1367d20f72 100644 --- a/src/test/rustdoc/whitespace-after-where-clause.struct.html +++ b/src/test/rustdoc/whitespace-after-where-clause.struct.html @@ -1,4 +1,4 @@ -<div class="docblock item-decl"><pre class="rust struct"><code>pub struct Struct<'a, B: ?<a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + 'a> <span class="where fmt-newline">where<br />    B: <a class="trait" href="trait.ToOwned.html" title="trait foo::ToOwned">ToOwned</a><dyn <a class="trait" href="{{channel}}/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>>, </span>{ +<div class="item-decl"><pre class="rust struct"><code>pub struct Struct<'a, B: ?<a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + 'a><span class="where fmt-newline">where<br />    B: <a class="trait" href="trait.ToOwned.html" title="trait foo::ToOwned">ToOwned</a><dyn <a class="trait" href="{{channel}}/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>>,</span>{ pub a: <a class="primitive" href="{{channel}}/std/primitive.reference.html">&'a </a>B, pub b: <a class="primitive" href="{{channel}}/std/primitive.u32.html">u32</a>, -}</code></pre></div> +}</code></pre></div> \ No newline at end of file diff --git a/src/test/rustdoc/whitespace-after-where-clause.struct2.html b/src/test/rustdoc/whitespace-after-where-clause.struct2.html index 47f5c6ba9c846..fb06b0f77c5ce 100644 --- a/src/test/rustdoc/whitespace-after-where-clause.struct2.html +++ b/src/test/rustdoc/whitespace-after-where-clause.struct2.html @@ -1,4 +1,4 @@ -<div class="docblock item-decl"><pre class="rust struct"><code>pub struct Struct2<'a, B: ?<a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + <a class="trait" href="trait.ToOwned.html" title="trait foo::ToOwned">ToOwned</a><dyn <a class="trait" href="{{channel}}/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>> + 'a> { +<div class="item-decl"><pre class="rust struct"><code>pub struct Struct2<'a, B: ?<a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + <a class="trait" href="trait.ToOwned.html" title="trait foo::ToOwned">ToOwned</a><dyn <a class="trait" href="{{channel}}/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>> + 'a> { pub a: <a class="primitive" href="{{channel}}/std/primitive.reference.html">&'a </a>B, pub b: <a class="primitive" href="{{channel}}/std/primitive.u32.html">u32</a>, -}</code></pre></div> +}</code></pre></div> \ No newline at end of file diff --git a/src/test/rustdoc/whitespace-after-where-clause.trait.html b/src/test/rustdoc/whitespace-after-where-clause.trait.html index 98f03b837b528..16b5582370353 100644 --- a/src/test/rustdoc/whitespace-after-where-clause.trait.html +++ b/src/test/rustdoc/whitespace-after-where-clause.trait.html @@ -1,6 +1,6 @@ -<div class="docblock item-decl"><pre class="rust trait"><code>pub trait ToOwned<T> <span class="where fmt-newline">where<br />    T: <a class="trait" href="{{channel}}/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>, </span>{ +<div class="item-decl"><pre class="rust trait"><code>pub trait ToOwned<T><span class="where fmt-newline">where<br />    T: <a class="trait" href="{{channel}}/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>,</span>{ type <a href="#associatedtype.Owned" class="associatedtype">Owned</a>; fn <a href="#tymethod.to_owned" class="fnname">to_owned</a>(&self) -> Self::<a class="associatedtype" href="trait.ToOwned.html#associatedtype.Owned" title="type foo::ToOwned::Owned">Owned</a>; <span class="item-spacer" /> fn <a href="#tymethod.whatever" class="fnname">whatever</a>(&self) -> T; -}</code></pre></div> +}</code></pre></div> \ No newline at end of file diff --git a/src/test/rustdoc/whitespace-after-where-clause.trait2.html b/src/test/rustdoc/whitespace-after-where-clause.trait2.html index 35052869e762d..eeca6e1f50081 100644 --- a/src/test/rustdoc/whitespace-after-where-clause.trait2.html +++ b/src/test/rustdoc/whitespace-after-where-clause.trait2.html @@ -1,6 +1,6 @@ -<div class="docblock item-decl"><pre class="rust trait"><code>pub trait ToOwned2<T: <a class="trait" href="{{channel}}/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>> { +<div class="item-decl"><pre class="rust trait"><code>pub trait ToOwned2<T: <a class="trait" href="{{channel}}/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>> { type <a href="#associatedtype.Owned" class="associatedtype">Owned</a>; fn <a href="#tymethod.to_owned" class="fnname">to_owned</a>(&self) -> Self::<a class="associatedtype" href="trait.ToOwned2.html#associatedtype.Owned" title="type foo::ToOwned2::Owned">Owned</a>; <span class="item-spacer" /> fn <a href="#tymethod.whatever" class="fnname">whatever</a>(&self) -> T; -}</code></pre></div> +}</code></pre></div> \ No newline at end of file diff --git a/src/test/rustdoc/whitespace-after-where-clause.union.html b/src/test/rustdoc/whitespace-after-where-clause.union.html index 97e1bbcf339f8..0dfb6407d45f1 100644 --- a/src/test/rustdoc/whitespace-after-where-clause.union.html +++ b/src/test/rustdoc/whitespace-after-where-clause.union.html @@ -1,3 +1,3 @@ -<div class="docblock item-decl"><pre class="rust union"><code>pub union Union<'a, B: ?<a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + 'a> <span class="where fmt-newline">where<br />    B: <a class="trait" href="trait.ToOwned.html" title="trait foo::ToOwned">ToOwned</a><dyn <a class="trait" href="{{channel}}/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>>, </span>{ +<div class="item-decl"><pre class="rust union"><code>pub union Union<'a, B: ?<a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + 'a><span class="where fmt-newline">where<br />    B: <a class="trait" href="trait.ToOwned.html" title="trait foo::ToOwned">ToOwned</a><dyn <a class="trait" href="{{channel}}/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>>,</span>{ /* private fields */ -}</code></pre></div> +}</code></pre></div> \ No newline at end of file diff --git a/src/test/rustdoc/whitespace-after-where-clause.union2.html b/src/test/rustdoc/whitespace-after-where-clause.union2.html index 6c752a8b4c5ea..0d237df53c7f4 100644 --- a/src/test/rustdoc/whitespace-after-where-clause.union2.html +++ b/src/test/rustdoc/whitespace-after-where-clause.union2.html @@ -1,3 +1,3 @@ -<div class="docblock item-decl"><pre class="rust union"><code>pub union Union2<'a, B: ?<a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + <a class="trait" href="trait.ToOwned.html" title="trait foo::ToOwned">ToOwned</a><dyn <a class="trait" href="{{channel}}/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>> + 'a> { +<div class="item-decl"><pre class="rust union"><code>pub union Union2<'a, B: ?<a class="trait" href="{{channel}}/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + <a class="trait" href="trait.ToOwned.html" title="trait foo::ToOwned">ToOwned</a><dyn <a class="trait" href="{{channel}}/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>> + 'a> { /* private fields */ -}</code></pre></div> +}</code></pre></div> \ No newline at end of file diff --git a/src/test/ui/macros/syntax-error-recovery.rs b/src/test/ui/macros/syntax-error-recovery.rs new file mode 100644 index 0000000000000..ae6de3c5046cd --- /dev/null +++ b/src/test/ui/macros/syntax-error-recovery.rs @@ -0,0 +1,18 @@ +macro_rules! values { + ($($token:ident($value:literal) $(as $inner:ty)? => $attr:meta,)*) => { + #[derive(Debug)] + pub enum TokenKind { + $( + #[$attr] + $token $($inner)? = $value, + )* + } + }; +} +//~^^^^^ ERROR expected one of `(`, `,`, `=`, `{`, or `}`, found `(String)` +//~| ERROR macro expansion ignores token `(String)` and any following + +values!(STRING(1) as (String) => cfg(test),); +//~^ ERROR expected one of `!` or `::`, found `<eof>` + +fn main() {} diff --git a/src/test/ui/macros/syntax-error-recovery.stderr b/src/test/ui/macros/syntax-error-recovery.stderr new file mode 100644 index 0000000000000..c153b3b910bbe --- /dev/null +++ b/src/test/ui/macros/syntax-error-recovery.stderr @@ -0,0 +1,30 @@ +error: expected one of `(`, `,`, `=`, `{`, or `}`, found `(String)` + --> $DIR/syntax-error-recovery.rs:7:26 + | +LL | $token $($inner)? = $value, + | ^^^^^^ expected one of `(`, `,`, `=`, `{`, or `}` +... +LL | values!(STRING(1) as (String) => cfg(test),); + | -------------------------------------------- in this macro invocation + | + = note: this error originates in the macro `values` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: macro expansion ignores token `(String)` and any following + --> $DIR/syntax-error-recovery.rs:7:26 + | +LL | $token $($inner)? = $value, + | ^^^^^^ +... +LL | values!(STRING(1) as (String) => cfg(test),); + | -------------------------------------------- caused by the macro expansion here + | + = note: the usage of `values!` is likely invalid in item context + +error: expected one of `!` or `::`, found `<eof>` + --> $DIR/syntax-error-recovery.rs:15:9 + | +LL | values!(STRING(1) as (String) => cfg(test),); + | ^^^^^^ expected one of `!` or `::` + +error: aborting due to 3 previous errors + diff --git a/src/tools/rust-analyzer/Cargo.lock b/src/tools/rust-analyzer/Cargo.lock index 9f10d92c4e3ab..216cf51447fad 100644 --- a/src/tools/rust-analyzer/Cargo.lock +++ b/src/tools/rust-analyzer/Cargo.lock @@ -394,6 +394,7 @@ dependencies = [ "crossbeam-channel", "jod-thread", "paths", + "rustc-hash", "serde", "serde_json", "stdx", @@ -660,6 +661,7 @@ dependencies = [ "indexmap", "itertools", "limit", + "memchr", "once_cell", "parser", "profile", diff --git a/src/tools/rust-analyzer/crates/flycheck/Cargo.toml b/src/tools/rust-analyzer/crates/flycheck/Cargo.toml index d3d180ece512a..688e790c5368c 100644 --- a/src/tools/rust-analyzer/crates/flycheck/Cargo.toml +++ b/src/tools/rust-analyzer/crates/flycheck/Cargo.toml @@ -13,6 +13,7 @@ doctest = false crossbeam-channel = "0.5.5" tracing = "0.1.35" cargo_metadata = "0.15.0" +rustc-hash = "1.1.0" serde = { version = "1.0.137", features = ["derive"] } serde_json = "1.0.81" jod-thread = "0.1.2" diff --git a/src/tools/rust-analyzer/crates/flycheck/src/lib.rs b/src/tools/rust-analyzer/crates/flycheck/src/lib.rs index d9f4ef5b7ff57..fdc03f4053a27 100644 --- a/src/tools/rust-analyzer/crates/flycheck/src/lib.rs +++ b/src/tools/rust-analyzer/crates/flycheck/src/lib.rs @@ -12,6 +12,7 @@ use std::{ use crossbeam_channel::{never, select, unbounded, Receiver, Sender}; use paths::AbsPathBuf; +use rustc_hash::FxHashMap; use serde::Deserialize; use stdx::{process::streaming_output, JodChild}; @@ -30,10 +31,12 @@ pub enum FlycheckConfig { all_features: bool, features: Vec<String>, extra_args: Vec<String>, + extra_env: FxHashMap<String, String>, }, CustomCommand { command: String, args: Vec<String>, + extra_env: FxHashMap<String, String>, }, } @@ -41,7 +44,7 @@ impl fmt::Display for FlycheckConfig { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { FlycheckConfig::CargoCommand { command, .. } => write!(f, "cargo {}", command), - FlycheckConfig::CustomCommand { command, args } => { + FlycheckConfig::CustomCommand { command, args, .. } => { write!(f, "{} {}", command, args.join(" ")) } } @@ -256,6 +259,7 @@ impl FlycheckActor { all_features, extra_args, features, + extra_env, } => { let mut cmd = Command::new(toolchain::cargo()); cmd.arg(command); @@ -281,11 +285,13 @@ impl FlycheckActor { } } cmd.args(extra_args); + cmd.envs(extra_env); cmd } - FlycheckConfig::CustomCommand { command, args } => { + FlycheckConfig::CustomCommand { command, args, extra_env } => { let mut cmd = Command::new(command); cmd.args(args); + cmd.envs(extra_env); cmd } }; diff --git a/src/tools/rust-analyzer/crates/hir-def/src/find_path.rs b/src/tools/rust-analyzer/crates/hir-def/src/find_path.rs index 89e961f84fad2..b94b50004093c 100644 --- a/src/tools/rust-analyzer/crates/hir-def/src/find_path.rs +++ b/src/tools/rust-analyzer/crates/hir-def/src/find_path.rs @@ -1,6 +1,6 @@ //! An algorithm to find a path to refer to a certain item. -use std::iter; +use std::{cmp::Ordering, iter}; use hir_expand::name::{known, AsName, Name}; use rustc_hash::FxHashSet; @@ -16,9 +16,14 @@ use crate::{ /// Find a path that can be used to refer to a certain item. This can depend on /// *from where* you're referring to the item, hence the `from` parameter. -pub fn find_path(db: &dyn DefDatabase, item: ItemInNs, from: ModuleId) -> Option<ModPath> { +pub fn find_path( + db: &dyn DefDatabase, + item: ItemInNs, + from: ModuleId, + prefer_no_std: bool, +) -> Option<ModPath> { let _p = profile::span("find_path"); - find_path_inner(db, item, from, None) + find_path_inner(db, item, from, None, prefer_no_std) } pub fn find_path_prefixed( @@ -26,47 +31,14 @@ pub fn find_path_prefixed( item: ItemInNs, from: ModuleId, prefix_kind: PrefixKind, + prefer_no_std: bool, ) -> Option<ModPath> { let _p = profile::span("find_path_prefixed"); - find_path_inner(db, item, from, Some(prefix_kind)) + find_path_inner(db, item, from, Some(prefix_kind), prefer_no_std) } const MAX_PATH_LEN: usize = 15; -trait ModPathExt { - fn starts_with_std(&self) -> bool; - fn can_start_with_std(&self) -> bool; -} - -impl ModPathExt for ModPath { - fn starts_with_std(&self) -> bool { - self.segments().first() == Some(&known::std) - } - - // Can we replace the first segment with `std::` and still get a valid, identical path? - fn can_start_with_std(&self) -> bool { - let first_segment = self.segments().first(); - first_segment == Some(&known::alloc) || first_segment == Some(&known::core) - } -} - -fn check_self_super(def_map: &DefMap, item: ItemInNs, from: ModuleId) -> Option<ModPath> { - if item == ItemInNs::Types(from.into()) { - // - if the item is the module we're in, use `self` - Some(ModPath::from_segments(PathKind::Super(0), None)) - } else if let Some(parent_id) = def_map[from.local_id].parent { - // - if the item is the parent module, use `super` (this is not used recursively, since `super::super` is ugly) - let parent_id = def_map.module_id(parent_id); - if item == ItemInNs::Types(ModuleDefId::ModuleId(parent_id)) { - Some(ModPath::from_segments(PathKind::Super(1), None)) - } else { - None - } - } else { - None - } -} - #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum PrefixKind { /// Causes paths to always start with either `self`, `super`, `crate` or a crate-name. @@ -94,135 +66,247 @@ impl PrefixKind { self == &PrefixKind::ByCrate } } + /// Attempts to find a path to refer to the given `item` visible from the `from` ModuleId fn find_path_inner( db: &dyn DefDatabase, item: ItemInNs, from: ModuleId, prefixed: Option<PrefixKind>, + prefer_no_std: bool, ) -> Option<ModPath> { - // FIXME: Do fast path for std/core libs? + // - if the item is a builtin, it's in scope + if let ItemInNs::Types(ModuleDefId::BuiltinType(builtin)) = item { + return Some(ModPath::from_segments(PathKind::Plain, Some(builtin.as_name()))); + } - let mut visited_modules = FxHashSet::default(); let def_map = from.def_map(db); - find_path_inner_(db, &def_map, from, item, MAX_PATH_LEN, prefixed, &mut visited_modules) + let crate_root = def_map.crate_root(db); + // - if the item is a module, jump straight to module search + if let ItemInNs::Types(ModuleDefId::ModuleId(module_id)) = item { + let mut visited_modules = FxHashSet::default(); + return find_path_for_module( + db, + &def_map, + &mut visited_modules, + crate_root, + from, + module_id, + MAX_PATH_LEN, + prefixed, + prefer_no_std || db.crate_supports_no_std(crate_root.krate), + ); + } + + // - if the item is already in scope, return the name under which it is + let scope_name = find_in_scope(db, &def_map, from, item); + if prefixed.is_none() { + if let Some(scope_name) = scope_name { + return Some(ModPath::from_segments(PathKind::Plain, Some(scope_name))); + } + } + + // - if the item is in the prelude, return the name from there + if let Some(value) = find_in_prelude(db, &crate_root.def_map(db), item, from) { + return value; + } + + if let Some(ModuleDefId::EnumVariantId(variant)) = item.as_module_def_id() { + // - if the item is an enum variant, refer to it via the enum + if let Some(mut path) = find_path_inner( + db, + ItemInNs::Types(variant.parent.into()), + from, + prefixed, + prefer_no_std, + ) { + let data = db.enum_data(variant.parent); + path.push_segment(data.variants[variant.local_id].name.clone()); + return Some(path); + } + // If this doesn't work, it seems we have no way of referring to the + // enum; that's very weird, but there might still be a reexport of the + // variant somewhere + } + + let mut visited_modules = FxHashSet::default(); + + calculate_best_path( + db, + &def_map, + &mut visited_modules, + crate_root, + MAX_PATH_LEN, + item, + from, + prefixed, + prefer_no_std || db.crate_supports_no_std(crate_root.krate), + scope_name, + ) } -fn find_path_inner_( +fn find_path_for_module( db: &dyn DefDatabase, def_map: &DefMap, + visited_modules: &mut FxHashSet<ModuleId>, + crate_root: ModuleId, from: ModuleId, - item: ItemInNs, + module_id: ModuleId, max_len: usize, - mut prefixed: Option<PrefixKind>, - visited_modules: &mut FxHashSet<ModuleId>, + prefixed: Option<PrefixKind>, + prefer_no_std: bool, ) -> Option<ModPath> { if max_len == 0 { return None; } // Base cases: - // - if the item is already in scope, return the name under which it is - let scope_name = def_map.with_ancestor_maps(db, from.local_id, &mut |def_map, local_id| { - def_map[local_id].scope.name_of(item).map(|(name, _)| name.clone()) - }); + let scope_name = find_in_scope(db, def_map, from, ItemInNs::Types(module_id.into())); if prefixed.is_none() { if let Some(scope_name) = scope_name { return Some(ModPath::from_segments(PathKind::Plain, Some(scope_name))); } } - // - if the item is a builtin, it's in scope - if let ItemInNs::Types(ModuleDefId::BuiltinType(builtin)) = item { - return Some(ModPath::from_segments(PathKind::Plain, Some(builtin.as_name()))); - } - // - if the item is the crate root, return `crate` - let crate_root = def_map.crate_root(db); - if item == ItemInNs::Types(ModuleDefId::ModuleId(crate_root)) { + if module_id == crate_root { return Some(ModPath::from_segments(PathKind::Crate, None)); } + // - if relative paths are fine, check if we are searching for a parent if prefixed.filter(PrefixKind::is_absolute).is_none() { - if let modpath @ Some(_) = check_self_super(&def_map, item, from) { + if let modpath @ Some(_) = find_self_super(&def_map, module_id, from) { return modpath; } } // - if the item is the crate root of a dependency crate, return the name from the extern prelude let root_def_map = crate_root.def_map(db); - if let ItemInNs::Types(ModuleDefId::ModuleId(item)) = item { - for (name, &def_id) in root_def_map.extern_prelude() { - if item == def_id { - let name = scope_name.unwrap_or_else(|| name.clone()); - - let name_already_occupied_in_type_ns = def_map - .with_ancestor_maps(db, from.local_id, &mut |def_map, local_id| { - def_map[local_id] - .scope - .type_(&name) - .filter(|&(id, _)| id != ModuleDefId::ModuleId(def_id)) - }) - .is_some(); - let kind = if name_already_occupied_in_type_ns { - cov_mark::hit!(ambiguous_crate_start); - PathKind::Abs - } else { - PathKind::Plain - }; - return Some(ModPath::from_segments(kind, Some(name))); - } + for (name, &def_id) in root_def_map.extern_prelude() { + if module_id == def_id { + let name = scope_name.unwrap_or_else(|| name.clone()); + + let name_already_occupied_in_type_ns = def_map + .with_ancestor_maps(db, from.local_id, &mut |def_map, local_id| { + def_map[local_id] + .scope + .type_(&name) + .filter(|&(id, _)| id != ModuleDefId::ModuleId(def_id)) + }) + .is_some(); + let kind = if name_already_occupied_in_type_ns { + cov_mark::hit!(ambiguous_crate_start); + PathKind::Abs + } else { + PathKind::Plain + }; + return Some(ModPath::from_segments(kind, Some(name))); } } - // - if the item is in the prelude, return the name from there + if let Some(value) = find_in_prelude(db, &root_def_map, ItemInNs::Types(module_id.into()), from) + { + return value; + } + calculate_best_path( + db, + def_map, + visited_modules, + crate_root, + max_len, + ItemInNs::Types(module_id.into()), + from, + prefixed, + prefer_no_std, + scope_name, + ) +} + +fn find_in_scope( + db: &dyn DefDatabase, + def_map: &DefMap, + from: ModuleId, + item: ItemInNs, +) -> Option<Name> { + def_map.with_ancestor_maps(db, from.local_id, &mut |def_map, local_id| { + def_map[local_id].scope.name_of(item).map(|(name, _)| name.clone()) + }) +} + +fn find_in_prelude( + db: &dyn DefDatabase, + root_def_map: &DefMap, + item: ItemInNs, + from: ModuleId, +) -> Option<Option<ModPath>> { if let Some(prelude_module) = root_def_map.prelude() { // Preludes in block DefMaps are ignored, only the crate DefMap is searched let prelude_def_map = prelude_module.def_map(db); let prelude_scope = &prelude_def_map[prelude_module.local_id].scope; if let Some((name, vis)) = prelude_scope.name_of(item) { if vis.is_visible_from(db, from) { - return Some(ModPath::from_segments(PathKind::Plain, Some(name.clone()))); + return Some(Some(ModPath::from_segments(PathKind::Plain, Some(name.clone())))); } } } + None +} - // Recursive case: - // - if the item is an enum variant, refer to it via the enum - if let Some(ModuleDefId::EnumVariantId(variant)) = item.as_module_def_id() { - if let Some(mut path) = find_path(db, ItemInNs::Types(variant.parent.into()), from) { - let data = db.enum_data(variant.parent); - path.push_segment(data.variants[variant.local_id].name.clone()); - return Some(path); +fn find_self_super(def_map: &DefMap, item: ModuleId, from: ModuleId) -> Option<ModPath> { + if item == from { + // - if the item is the module we're in, use `self` + Some(ModPath::from_segments(PathKind::Super(0), None)) + } else if let Some(parent_id) = def_map[from.local_id].parent { + // - if the item is the parent module, use `super` (this is not used recursively, since `super::super` is ugly) + let parent_id = def_map.module_id(parent_id); + if item == parent_id { + Some(ModPath::from_segments(PathKind::Super(1), None)) + } else { + None } - // If this doesn't work, it seems we have no way of referring to the - // enum; that's very weird, but there might still be a reexport of the - // variant somewhere + } else { + None } +} - // - otherwise, look for modules containing (reexporting) it and import it from one of those - let prefer_no_std = db.crate_supports_no_std(crate_root.krate); +fn calculate_best_path( + db: &dyn DefDatabase, + def_map: &DefMap, + visited_modules: &mut FxHashSet<ModuleId>, + crate_root: ModuleId, + max_len: usize, + item: ItemInNs, + from: ModuleId, + mut prefixed: Option<PrefixKind>, + prefer_no_std: bool, + scope_name: Option<Name>, +) -> Option<ModPath> { + if max_len <= 1 { + return None; + } let mut best_path = None; - let mut best_path_len = max_len; - + // Recursive case: + // - otherwise, look for modules containing (reexporting) it and import it from one of those if item.krate(db) == Some(from.krate) { + let mut best_path_len = max_len; // Item was defined in the same crate that wants to import it. It cannot be found in any // dependency in this case. - // FIXME: this should have a fast path that doesn't look through the prelude again? for (module_id, name) in find_local_import_locations(db, item, from) { if !visited_modules.insert(module_id) { cov_mark::hit!(recursive_imports); continue; } - if let Some(mut path) = find_path_inner_( + if let Some(mut path) = find_path_for_module( db, def_map, + visited_modules, + crate_root, from, - ItemInNs::Types(ModuleDefId::ModuleId(module_id)), + module_id, best_path_len - 1, prefixed, - visited_modules, + prefer_no_std, ) { path.push_segment(name); @@ -245,14 +329,16 @@ fn find_path_inner_( import_map.import_info_for(item).and_then(|info| { // Determine best path for containing module and append last segment from `info`. // FIXME: we should guide this to look up the path locally, or from the same crate again? - let mut path = find_path_inner_( + let mut path = find_path_for_module( db, def_map, + visited_modules, from, - ItemInNs::Types(ModuleDefId::ModuleId(info.container)), - best_path_len - 1, + crate_root, + info.container, + max_len - 1, prefixed, - visited_modules, + prefer_no_std, )?; cov_mark::hit!(partially_imported); path.push_segment(info.path.segments.last()?.clone()); @@ -268,16 +354,12 @@ fn find_path_inner_( best_path = Some(new_path); } } - - // If the item is declared inside a block expression, don't use a prefix, as we don't handle - // that correctly (FIXME). - if let Some(item_module) = item.as_module_def_id().and_then(|did| did.module(db)) { - if item_module.def_map(db).block_id().is_some() && prefixed.is_some() { + if let Some(module) = item.module(db) { + if module.def_map(db).block_id().is_some() && prefixed.is_some() { cov_mark::hit!(prefixed_in_block_expression); prefixed = Some(PrefixKind::Plain); } } - match prefixed.map(PrefixKind::prefix) { Some(prefix) => best_path.or_else(|| { scope_name.map(|scope_name| ModPath::from_segments(prefix, Some(scope_name))) @@ -287,29 +369,48 @@ fn find_path_inner_( } fn select_best_path(old_path: ModPath, new_path: ModPath, prefer_no_std: bool) -> ModPath { - if old_path.starts_with_std() && new_path.can_start_with_std() { - if prefer_no_std { - cov_mark::hit!(prefer_no_std_paths); - new_path - } else { - cov_mark::hit!(prefer_std_paths); - old_path + const STD_CRATES: [Name; 3] = [known::std, known::core, known::alloc]; + match (old_path.segments().first(), new_path.segments().first()) { + (Some(old), Some(new)) if STD_CRATES.contains(old) && STD_CRATES.contains(new) => { + let rank = match prefer_no_std { + false => |name: &Name| match name { + name if name == &known::core => 0, + name if name == &known::alloc => 0, + name if name == &known::std => 1, + _ => unreachable!(), + }, + true => |name: &Name| match name { + name if name == &known::core => 2, + name if name == &known::alloc => 1, + name if name == &known::std => 0, + _ => unreachable!(), + }, + }; + let nrank = rank(new); + let orank = rank(old); + match nrank.cmp(&orank) { + Ordering::Less => old_path, + Ordering::Equal => { + if new_path.len() < old_path.len() { + new_path + } else { + old_path + } + } + Ordering::Greater => new_path, + } } - } else if new_path.starts_with_std() && old_path.can_start_with_std() { - if prefer_no_std { - cov_mark::hit!(prefer_no_std_paths); - old_path - } else { - cov_mark::hit!(prefer_std_paths); - new_path + _ => { + if new_path.len() < old_path.len() { + new_path + } else { + old_path + } } - } else if new_path.len() < old_path.len() { - new_path - } else { - old_path } } +// FIXME: Remove allocations /// Finds locations in `from.krate` from which `item` can be imported by `from`. fn find_local_import_locations( db: &dyn DefDatabase, @@ -428,7 +529,8 @@ mod tests { .take_types() .unwrap(); - let found_path = find_path_inner(&db, ItemInNs::Types(resolved), module, prefix_kind); + let found_path = + find_path_inner(&db, ItemInNs::Types(resolved), module, prefix_kind, false); assert_eq!(found_path, Some(mod_path), "{:?}", prefix_kind); } @@ -468,8 +570,8 @@ $0 "#, "E::A", "E::A", - "E::A", - "E::A", + "crate::E::A", + "self::E::A", ); } @@ -788,7 +890,6 @@ pub use super::foo; #[test] fn prefer_std_paths_over_alloc() { - cov_mark::check!(prefer_std_paths); check_found_path( r#" //- /main.rs crate:main deps:alloc,std @@ -813,7 +914,6 @@ pub mod sync { #[test] fn prefer_core_paths_over_std() { - cov_mark::check!(prefer_no_std_paths); check_found_path( r#" //- /main.rs crate:main deps:core,std diff --git a/src/tools/rust-analyzer/crates/hir-def/src/item_scope.rs b/src/tools/rust-analyzer/crates/hir-def/src/item_scope.rs index a11a92204c15c..7721221c44475 100644 --- a/src/tools/rust-analyzer/crates/hir-def/src/item_scope.rs +++ b/src/tools/rust-analyzer/crates/hir-def/src/item_scope.rs @@ -18,7 +18,7 @@ use crate::{ ConstId, HasModule, ImplId, LocalModuleId, MacroId, ModuleDefId, ModuleId, TraitId, }; -#[derive(Copy, Clone)] +#[derive(Copy, Clone, Debug)] pub(crate) enum ImportType { Glob, Named, @@ -302,13 +302,13 @@ impl ItemScope { $changed = true; } Entry::Occupied(mut entry) - if $glob_imports.$field.contains(&$lookup) - && matches!($def_import_type, ImportType::Named) => + if matches!($def_import_type, ImportType::Named) => { - cov_mark::hit!(import_shadowed); - $glob_imports.$field.remove(&$lookup); - entry.insert(fld); - $changed = true; + if $glob_imports.$field.remove(&$lookup) { + cov_mark::hit!(import_shadowed); + entry.insert(fld); + $changed = true; + } } _ => {} } @@ -457,8 +457,15 @@ impl ItemInNs { /// Returns the crate defining this item (or `None` if `self` is built-in). pub fn krate(&self, db: &dyn DefDatabase) -> Option<CrateId> { match self { - ItemInNs::Types(did) | ItemInNs::Values(did) => did.module(db).map(|m| m.krate), + ItemInNs::Types(id) | ItemInNs::Values(id) => id.module(db).map(|m| m.krate), ItemInNs::Macros(id) => Some(id.module(db).krate), } } + + pub fn module(&self, db: &dyn DefDatabase) -> Option<ModuleId> { + match self { + ItemInNs::Types(id) | ItemInNs::Values(id) => id.module(db), + ItemInNs::Macros(id) => Some(id.module(db)), + } + } } diff --git a/src/tools/rust-analyzer/crates/hir-def/src/nameres/collector.rs b/src/tools/rust-analyzer/crates/hir-def/src/nameres/collector.rs index 495bbe4579f00..9242b48c59319 100644 --- a/src/tools/rust-analyzer/crates/hir-def/src/nameres/collector.rs +++ b/src/tools/rust-analyzer/crates/hir-def/src/nameres/collector.rs @@ -534,6 +534,7 @@ impl DefCollector<'_> { match per_ns.types { Some((ModuleDefId::ModuleId(m), _)) => { self.def_map.prelude = Some(m); + break; } types => { tracing::debug!( diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/db.rs b/src/tools/rust-analyzer/crates/hir-ty/src/db.rs index b385b1cafaefd..69283e55a4c2c 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/db.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/db.rs @@ -150,6 +150,14 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> { id: chalk_db::AssociatedTyValueId, ) -> Arc<chalk_db::AssociatedTyValue>; + #[salsa::invoke(crate::traits::normalize_projection_query)] + #[salsa::transparent] + fn normalize_projection( + &self, + projection: crate::ProjectionTy, + env: Arc<crate::TraitEnvironment>, + ) -> Ty; + #[salsa::invoke(trait_solve_wait)] #[salsa::transparent] fn trait_solve( diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/display.rs b/src/tools/rust-analyzer/crates/hir-ty/src/display.rs index d2f9c2b8b1e1d..874abdaea8370 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/display.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/display.rs @@ -533,6 +533,7 @@ impl HirDisplay for Ty { f.db.upcast(), ItemInNs::Types((*def_id).into()), module_id, + false, ) { write!(f, "{}", path)?; } else { diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/infer.rs b/src/tools/rust-analyzer/crates/hir-ty/src/infer.rs index 10ffde87eef14..e37763e8ea7f0 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/infer.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/infer.rs @@ -673,10 +673,6 @@ impl<'a> InferenceContext<'a> { ) } - fn resolve_obligations_as_possible(&mut self) { - self.table.resolve_obligations_as_possible(); - } - fn push_obligation(&mut self, o: DomainGoal) { self.table.register_obligation(o.cast(Interner)); } @@ -696,7 +692,6 @@ impl<'a> InferenceContext<'a> { } fn resolve_ty_shallow(&mut self, ty: &Ty) -> Ty { - self.resolve_obligations_as_possible(); self.table.resolve_ty_shallow(ty) } diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/lib.rs b/src/tools/rust-analyzer/crates/hir-ty/src/lib.rs index a82a331d4b875..de4a5446e57f0 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/lib.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/lib.rs @@ -196,20 +196,6 @@ pub(crate) fn make_binders<T: HasInterner<Interner = Interner>>( make_binders_with_count(db, usize::MAX, generics, value) } -// FIXME: get rid of this -pub fn make_canonical<T: HasInterner<Interner = Interner>>( - value: T, - kinds: impl IntoIterator<Item = TyVariableKind>, -) -> Canonical<T> { - let kinds = kinds.into_iter().map(|tk| { - chalk_ir::CanonicalVarKind::new( - chalk_ir::VariableKind::Ty(tk), - chalk_ir::UniverseIndex::ROOT, - ) - }); - Canonical { value, binders: chalk_ir::CanonicalVarKinds::from_iter(Interner, kinds) } -} - // FIXME: get rid of this, just replace it by FnPointer /// A function signature as seen by type inference: Several parameter types and /// one return type. diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/method_resolution.rs b/src/tools/rust-analyzer/crates/hir-ty/src/method_resolution.rs index 9a63d5013b467..41fcef73d9be4 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/method_resolution.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/method_resolution.rs @@ -914,22 +914,10 @@ fn iterate_trait_method_candidates( let db = table.db; let env = table.trait_env.clone(); let self_is_array = matches!(self_ty.kind(Interner), chalk_ir::TyKind::Array(..)); - // if ty is `dyn Trait`, the trait doesn't need to be in scope - let inherent_trait = - self_ty.dyn_trait().into_iter().flat_map(|t| all_super_traits(db.upcast(), t)); - let env_traits = matches!(self_ty.kind(Interner), TyKind::Placeholder(_)) - // if we have `T: Trait` in the param env, the trait doesn't need to be in scope - .then(|| { - env.traits_in_scope_from_clauses(self_ty.clone()) - .flat_map(|t| all_super_traits(db.upcast(), t)) - }) - .into_iter() - .flatten(); - let traits = inherent_trait.chain(env_traits).chain(traits_in_scope.iter().copied()); let canonical_self_ty = table.canonicalize(self_ty.clone()).value; - 'traits: for t in traits { + 'traits: for &t in traits_in_scope { let data = db.trait_data(t); // Traits annotated with `#[rustc_skip_array_during_method_dispatch]` are skipped during @@ -979,6 +967,44 @@ fn iterate_inherent_methods( ) -> ControlFlow<()> { let db = table.db; let env = table.trait_env.clone(); + + // For trait object types and placeholder types with trait bounds, the methods of the trait and + // its super traits are considered inherent methods. This matters because these methods have + // higher priority than the other traits' methods, which would be considered in + // `iterate_trait_method_candidates()` only after this function. + match self_ty.kind(Interner) { + TyKind::Placeholder(_) => { + let env = table.trait_env.clone(); + let traits = env + .traits_in_scope_from_clauses(self_ty.clone()) + .flat_map(|t| all_super_traits(db.upcast(), t)); + iterate_inherent_trait_methods( + self_ty, + table, + name, + receiver_ty, + receiver_adjustments.clone(), + callback, + traits, + )?; + } + TyKind::Dyn(_) => { + if let Some(principal_trait) = self_ty.dyn_trait() { + let traits = all_super_traits(db.upcast(), principal_trait); + iterate_inherent_trait_methods( + self_ty, + table, + name, + receiver_ty, + receiver_adjustments.clone(), + callback, + traits.into_iter(), + )?; + } + } + _ => {} + } + let def_crates = match def_crates(db, self_ty, env.krate) { Some(k) => k, None => return ControlFlow::Continue(()), @@ -1020,6 +1046,28 @@ fn iterate_inherent_methods( } return ControlFlow::Continue(()); + fn iterate_inherent_trait_methods( + self_ty: &Ty, + table: &mut InferenceTable<'_>, + name: Option<&Name>, + receiver_ty: Option<&Ty>, + receiver_adjustments: Option<ReceiverAdjustments>, + callback: &mut dyn FnMut(ReceiverAdjustments, AssocItemId) -> ControlFlow<()>, + traits: impl Iterator<Item = TraitId>, + ) -> ControlFlow<()> { + let db = table.db; + for t in traits { + let data = db.trait_data(t); + for &(_, item) in data.items.iter() { + // We don't pass `visible_from_module` as all trait items should be visible. + if is_valid_candidate(table, name, receiver_ty, item, self_ty, None) { + callback(receiver_adjustments.clone().unwrap_or_default(), item)?; + } + } + } + ControlFlow::Continue(()) + } + fn impls_for_self_ty( impls: &InherentImpls, self_ty: &Ty, diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/tests/method_resolution.rs b/src/tools/rust-analyzer/crates/hir-ty/src/tests/method_resolution.rs index 81588a7c4ffd6..ac8edb841a580 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/tests/method_resolution.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/tests/method_resolution.rs @@ -1218,6 +1218,40 @@ fn main() { ); } +#[test] +fn dyn_trait_method_priority() { + check_types( + r#" +//- minicore: from +trait Trait { + fn into(&self) -> usize { 0 } +} + +fn foo(a: &dyn Trait) { + let _ = a.into(); + //^usize +} + "#, + ); +} + +#[test] +fn trait_method_priority_for_placeholder_type() { + check_types( + r#" +//- minicore: from +trait Trait { + fn into(&self) -> usize { 0 } +} + +fn foo<T: Trait>(a: &T) { + let _ = a.into(); + //^usize +} + "#, + ); +} + #[test] fn autoderef_visibility_field() { check( diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/traits.rs b/src/tools/rust-analyzer/crates/hir-ty/src/traits.rs index 77afeb3217dd0..372c3a3cca644 100644 --- a/src/tools/rust-analyzer/crates/hir-ty/src/traits.rs +++ b/src/tools/rust-analyzer/crates/hir-ty/src/traits.rs @@ -1,6 +1,6 @@ //! Trait solving using Chalk. -use std::env::var; +use std::{env::var, sync::Arc}; use chalk_ir::GoalData; use chalk_recursive::Cache; @@ -12,8 +12,9 @@ use stdx::panic_context; use syntax::SmolStr; use crate::{ - db::HirDatabase, AliasEq, AliasTy, Canonical, DomainGoal, Goal, Guidance, InEnvironment, - Interner, Solution, TraitRefExt, Ty, TyKind, WhereClause, + db::HirDatabase, infer::unify::InferenceTable, AliasEq, AliasTy, Canonical, DomainGoal, Goal, + Guidance, InEnvironment, Interner, ProjectionTy, Solution, TraitRefExt, Ty, TyKind, + WhereClause, }; /// This controls how much 'time' we give the Chalk solver before giving up. @@ -64,6 +65,16 @@ impl TraitEnvironment { } } +pub(crate) fn normalize_projection_query( + db: &dyn HirDatabase, + projection: ProjectionTy, + env: Arc<TraitEnvironment>, +) -> Ty { + let mut table = InferenceTable::new(db, env); + let ty = table.normalize_projection_ty(projection); + table.resolve_completely(ty) +} + /// Solve a trait goal using Chalk. pub(crate) fn trait_solve_query( db: &dyn HirDatabase, diff --git a/src/tools/rust-analyzer/crates/hir/src/lib.rs b/src/tools/rust-analyzer/crates/hir/src/lib.rs index e4bb63a864719..d2717c5665471 100644 --- a/src/tools/rust-analyzer/crates/hir/src/lib.rs +++ b/src/tools/rust-analyzer/crates/hir/src/lib.rs @@ -63,10 +63,9 @@ use hir_ty::{ primitive::UintTy, subst_prefix, traits::FnTrait, - AliasEq, AliasTy, BoundVar, CallableDefId, CallableSig, Canonical, CanonicalVarKinds, Cast, - ClosureId, DebruijnIndex, GenericArgData, InEnvironment, Interner, ParamKind, - QuantifiedWhereClause, Scalar, Solution, Substitution, TraitEnvironment, TraitRefExt, Ty, - TyBuilder, TyDefId, TyExt, TyKind, TyVariableKind, WhereClause, + AliasTy, CallableDefId, CallableSig, Canonical, CanonicalVarKinds, Cast, ClosureId, + GenericArgData, Interner, ParamKind, QuantifiedWhereClause, Scalar, Substitution, + TraitEnvironment, TraitRefExt, Ty, TyBuilder, TyDefId, TyExt, TyKind, WhereClause, }; use itertools::Itertools; use nameres::diagnostics::DefDiagnosticKind; @@ -582,8 +581,13 @@ impl Module { /// Finds a path that can be used to refer to the given item from within /// this module, if possible. - pub fn find_use_path(self, db: &dyn DefDatabase, item: impl Into<ItemInNs>) -> Option<ModPath> { - hir_def::find_path::find_path(db, item.into().into(), self.into()) + pub fn find_use_path( + self, + db: &dyn DefDatabase, + item: impl Into<ItemInNs>, + prefer_no_std: bool, + ) -> Option<ModPath> { + hir_def::find_path::find_path(db, item.into().into(), self.into(), prefer_no_std) } /// Finds a path that can be used to refer to the given item from within @@ -593,8 +597,15 @@ impl Module { db: &dyn DefDatabase, item: impl Into<ItemInNs>, prefix_kind: PrefixKind, + prefer_no_std: bool, ) -> Option<ModPath> { - hir_def::find_path::find_path_prefixed(db, item.into().into(), self.into(), prefix_kind) + hir_def::find_path::find_path_prefixed( + db, + item.into().into(), + self.into(), + prefix_kind, + prefer_no_std, + ) } } @@ -2880,27 +2891,12 @@ impl Type { } }) .build(); - let goal = hir_ty::make_canonical( - InEnvironment::new( - &self.env.env, - AliasEq { - alias: AliasTy::Projection(projection), - ty: TyKind::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)) - .intern(Interner), - } - .cast(Interner), - ), - [TyVariableKind::General].into_iter(), - ); - match db.trait_solve(self.env.krate, goal)? { - Solution::Unique(s) => s - .value - .subst - .as_slice(Interner) - .first() - .map(|ty| self.derived(ty.assert_ty_ref(Interner).clone())), - Solution::Ambig(_) => None, + let ty = db.normalize_projection(projection, self.env.clone()); + if ty.is_unknown() { + None + } else { + Some(self.derived(ty)) } } diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/assist_config.rs b/src/tools/rust-analyzer/crates/ide-assists/src/assist_config.rs index d4d148c774578..60d1588a44e54 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/assist_config.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/assist_config.rs @@ -13,4 +13,5 @@ pub struct AssistConfig { pub snippet_cap: Option<SnippetCap>, pub allowed: Option<Vec<AssistKind>>, pub insert_use: InsertUseConfig, + pub prefer_no_std: bool, } diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/add_missing_match_arms.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/add_missing_match_arms.rs index 1a7919a5a104d..73f4db4e5ff2b 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/add_missing_match_arms.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/add_missing_match_arms.rs @@ -87,7 +87,7 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>) .into_iter() .filter_map(|variant| { Some(( - build_pat(ctx.db(), module, variant)?, + build_pat(ctx.db(), module, variant, ctx.config.prefer_no_std)?, variant.should_be_hidden(ctx.db(), module.krate()), )) }) @@ -132,8 +132,9 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>) let is_hidden = variants .iter() .any(|variant| variant.should_be_hidden(ctx.db(), module.krate())); - let patterns = - variants.into_iter().filter_map(|variant| build_pat(ctx.db(), module, variant)); + let patterns = variants.into_iter().filter_map(|variant| { + build_pat(ctx.db(), module, variant, ctx.config.prefer_no_std) + }); (ast::Pat::from(make::tuple_pat(patterns)), is_hidden) }) @@ -349,10 +350,16 @@ fn resolve_tuple_of_enum_def( .collect() } -fn build_pat(db: &RootDatabase, module: hir::Module, var: ExtendedVariant) -> Option<ast::Pat> { +fn build_pat( + db: &RootDatabase, + module: hir::Module, + var: ExtendedVariant, + prefer_no_std: bool, +) -> Option<ast::Pat> { match var { ExtendedVariant::Variant(var) => { - let path = mod_path_to_ast(&module.find_use_path(db, ModuleDef::from(var))?); + let path = + mod_path_to_ast(&module.find_use_path(db, ModuleDef::from(var), prefer_no_std)?); // FIXME: use HIR for this; it doesn't currently expose struct vs. tuple vs. unit variants though let pat: ast::Pat = match var.source(db)?.value.kind() { diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/auto_import.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/auto_import.rs index 949cf3167a8a1..e257218ba9376 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/auto_import.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/auto_import.rs @@ -89,8 +89,11 @@ use crate::{AssistContext, AssistId, AssistKind, Assists, GroupLabel}; // ``` pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> { let (import_assets, syntax_under_caret) = find_importable_node(ctx)?; - let mut proposed_imports = - import_assets.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind); + let mut proposed_imports = import_assets.search_for_imports( + &ctx.sema, + ctx.config.insert_use.prefix_kind, + ctx.config.prefer_no_std, + ); if proposed_imports.is_empty() { return None; } diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_into_to_from.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_into_to_from.rs index 30f6dd41a1d6d..95d11abe8bc0f 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_into_to_from.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_into_to_from.rs @@ -50,7 +50,7 @@ pub(crate) fn convert_into_to_from(acc: &mut Assists, ctx: &AssistContext<'_>) - _ => return None, }; - mod_path_to_ast(&module.find_use_path(ctx.db(), src_type_def)?) + mod_path_to_ast(&module.find_use_path(ctx.db(), src_type_def, ctx.config.prefer_no_std)?) }; let dest_type = match &ast_trait { diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_function.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_function.rs index 52a55ead3af96..d6c8ea785f84a 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_function.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_function.rs @@ -152,6 +152,7 @@ pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op ctx.sema.db, ModuleDef::from(control_flow_enum), ctx.config.insert_use.prefix_kind, + ctx.config.prefer_no_std, ); if let Some(mod_path) = mod_path { diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_struct_from_enum_variant.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_struct_from_enum_variant.rs index ddc2052e7aa24..8d5cab283d061 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_struct_from_enum_variant.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_struct_from_enum_variant.rs @@ -409,6 +409,7 @@ fn process_references( ctx.sema.db, *enum_module_def, ctx.config.insert_use.prefix_kind, + ctx.config.prefer_no_std, ); if let Some(mut mod_path) = mod_path { mod_path.pop_segment(); diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/generate_deref.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/generate_deref.rs index b484635121eb2..8f4405a8c869c 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/generate_deref.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/generate_deref.rs @@ -58,7 +58,8 @@ fn generate_record_deref(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<( let module = ctx.sema.to_def(&strukt)?.module(ctx.db()); let trait_ = deref_type_to_generate.to_trait(&ctx.sema, module.krate())?; - let trait_path = module.find_use_path(ctx.db(), ModuleDef::Trait(trait_))?; + let trait_path = + module.find_use_path(ctx.db(), ModuleDef::Trait(trait_), ctx.config.prefer_no_std)?; let field_type = field.ty()?; let field_name = field.name()?; @@ -98,7 +99,8 @@ fn generate_tuple_deref(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<() let module = ctx.sema.to_def(&strukt)?.module(ctx.db()); let trait_ = deref_type_to_generate.to_trait(&ctx.sema, module.krate())?; - let trait_path = module.find_use_path(ctx.db(), ModuleDef::Trait(trait_))?; + let trait_path = + module.find_use_path(ctx.db(), ModuleDef::Trait(trait_), ctx.config.prefer_no_std)?; let field_type = field.ty()?; let target = field.syntax().text_range(); diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/generate_new.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/generate_new.rs index 6c93875e9eb62..9cda74d9e0d31 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/generate_new.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/generate_new.rs @@ -60,8 +60,11 @@ pub(crate) fn generate_new(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option let item_in_ns = hir::ItemInNs::from(hir::ModuleDef::from(ty.as_adt()?)); - let type_path = current_module - .find_use_path(ctx.sema.db, item_for_path_search(ctx.sema.db, item_in_ns)?)?; + let type_path = current_module.find_use_path( + ctx.sema.db, + item_for_path_search(ctx.sema.db, item_in_ns)?, + ctx.config.prefer_no_std, + )?; let expr = use_trivial_constructor( &ctx.sema.db, diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/move_format_string_arg.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/move_format_string_arg.rs new file mode 100644 index 0000000000000..92b2fa79d717b --- /dev/null +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/move_format_string_arg.rs @@ -0,0 +1,268 @@ +use crate::{AssistContext, Assists}; +use ide_db::{ + assists::{AssistId, AssistKind}, + syntax_helpers::{ + format_string::is_format_string, + format_string_exprs::{parse_format_exprs, Arg}, + }, +}; +use itertools::Itertools; +use syntax::{ast, AstNode, AstToken, NodeOrToken, SyntaxKind::COMMA, TextRange}; + +// Assist: move_format_string_arg +// +// Move an expression out of a format string. +// +// ``` +// macro_rules! format_args { +// ($lit:literal $(tt:tt)*) => { 0 }, +// } +// macro_rules! print { +// ($($arg:tt)*) => (std::io::_print(format_args!($($arg)*))); +// } +// +// fn main() { +// print!("{x + 1}$0"); +// } +// ``` +// -> +// ``` +// macro_rules! format_args { +// ($lit:literal $(tt:tt)*) => { 0 }, +// } +// macro_rules! print { +// ($($arg:tt)*) => (std::io::_print(format_args!($($arg)*))); +// } +// +// fn main() { +// print!("{}"$0, x + 1); +// } +// ``` + +pub(crate) fn move_format_string_arg(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> { + let fmt_string = ctx.find_token_at_offset::<ast::String>()?; + let tt = fmt_string.syntax().parent().and_then(ast::TokenTree::cast)?; + + let expanded_t = ast::String::cast( + ctx.sema.descend_into_macros_with_kind_preference(fmt_string.syntax().clone()), + )?; + if !is_format_string(&expanded_t) { + return None; + } + + let (new_fmt, extracted_args) = parse_format_exprs(fmt_string.text()).ok()?; + if extracted_args.is_empty() { + return None; + } + + acc.add( + AssistId( + "move_format_string_arg", + // if there aren't any expressions, then make the assist a RefactorExtract + if extracted_args.iter().filter(|f| matches!(f, Arg::Expr(_))).count() == 0 { + AssistKind::RefactorExtract + } else { + AssistKind::QuickFix + }, + ), + "Extract format args", + tt.syntax().text_range(), + |edit| { + let fmt_range = fmt_string.syntax().text_range(); + + // Replace old format string with new format string whose arguments have been extracted + edit.replace(fmt_range, new_fmt); + + // Insert cursor at end of format string + edit.insert(fmt_range.end(), "$0"); + + // Extract existing arguments in macro + let tokens = + tt.token_trees_and_tokens().filter_map(NodeOrToken::into_token).collect_vec(); + + let mut existing_args: Vec<String> = vec![]; + + let mut current_arg = String::new(); + if let [_opening_bracket, format_string, _args_start_comma, tokens @ .., end_bracket] = + tokens.as_slice() + { + for t in tokens { + if t.kind() == COMMA { + existing_args.push(current_arg.trim().into()); + current_arg.clear(); + } else { + current_arg.push_str(t.text()); + } + } + existing_args.push(current_arg.trim().into()); + + // delete everything after the format string till end bracket + // we're going to insert the new arguments later + edit.delete(TextRange::new( + format_string.text_range().end(), + end_bracket.text_range().start(), + )); + } + + // Start building the new args + let mut existing_args = existing_args.into_iter(); + let mut args = String::new(); + + let mut placeholder_idx = 1; + + for extracted_args in extracted_args { + // remove expr from format string + args.push_str(", "); + + match extracted_args { + Arg::Ident(s) | Arg::Expr(s) => { + // insert arg + args.push_str(&s); + } + Arg::Placeholder => { + // try matching with existing argument + match existing_args.next() { + Some(ea) => { + args.push_str(&ea); + } + None => { + // insert placeholder + args.push_str(&format!("${placeholder_idx}")); + placeholder_idx += 1; + } + } + } + } + } + + // Insert new args + edit.insert(fmt_range.end(), args); + }, + ); + + Some(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::tests::check_assist; + + const MACRO_DECL: &'static str = r#" +macro_rules! format_args { + ($lit:literal $(tt:tt)*) => { 0 }, +} +macro_rules! print { + ($($arg:tt)*) => (std::io::_print(format_args!($($arg)*))); +} +"#; + + fn add_macro_decl(s: &'static str) -> String { + MACRO_DECL.to_string() + s + } + + #[test] + fn multiple_middle_arg() { + check_assist( + move_format_string_arg, + &add_macro_decl( + r#" +fn main() { + print!("{} {x + 1:b} {}$0", y + 2, 2); +} +"#, + ), + &add_macro_decl( + r#" +fn main() { + print!("{} {:b} {}"$0, y + 2, x + 1, 2); +} +"#, + ), + ); + } + + #[test] + fn single_arg() { + check_assist( + move_format_string_arg, + &add_macro_decl( + r#" +fn main() { + print!("{obj.value:b}$0",); +} +"#, + ), + &add_macro_decl( + r#" +fn main() { + print!("{:b}"$0, obj.value); +} +"#, + ), + ); + } + + #[test] + fn multiple_middle_placeholders_arg() { + check_assist( + move_format_string_arg, + &add_macro_decl( + r#" +fn main() { + print!("{} {x + 1:b} {} {}$0", y + 2, 2); +} +"#, + ), + &add_macro_decl( + r#" +fn main() { + print!("{} {:b} {} {}"$0, y + 2, x + 1, 2, $1); +} +"#, + ), + ); + } + + #[test] + fn multiple_trailing_args() { + check_assist( + move_format_string_arg, + &add_macro_decl( + r#" +fn main() { + print!("{} {x + 1:b} {Struct(1, 2)}$0", 1); +} +"#, + ), + &add_macro_decl( + r#" +fn main() { + print!("{} {:b} {}"$0, 1, x + 1, Struct(1, 2)); +} +"#, + ), + ); + } + + #[test] + fn improper_commas() { + check_assist( + move_format_string_arg, + &add_macro_decl( + r#" +fn main() { + print!("{} {x + 1:b} {Struct(1, 2)}$0", 1,); +} +"#, + ), + &add_macro_decl( + r#" +fn main() { + print!("{} {:b} {}"$0, 1, x + 1, Struct(1, 2)); +} +"#, + ), + ); + } +} diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/qualify_method_call.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/qualify_method_call.rs index 121f8b4a13683..e57d1d065d622 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/qualify_method_call.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/qualify_method_call.rs @@ -44,8 +44,11 @@ pub(crate) fn qualify_method_call(acc: &mut Assists, ctx: &AssistContext<'_>) -> let current_module = ctx.sema.scope(call.syntax())?.module(); let target_module_def = ModuleDef::from(resolved_call); let item_in_ns = ItemInNs::from(target_module_def); - let receiver_path = current_module - .find_use_path(ctx.sema.db, item_for_path_search(ctx.sema.db, item_in_ns)?)?; + let receiver_path = current_module.find_use_path( + ctx.sema.db, + item_for_path_search(ctx.sema.db, item_in_ns)?, + ctx.config.prefer_no_std, + )?; let qualify_candidate = QualifyCandidate::ImplMethod(ctx.sema.db, call, resolved_call); diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/qualify_path.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/qualify_path.rs index 0c2e9da38639f..4b2af550bc5e4 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/qualify_path.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/qualify_path.rs @@ -37,7 +37,8 @@ use crate::{ // ``` pub(crate) fn qualify_path(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> { let (import_assets, syntax_under_caret) = find_importable_node(ctx)?; - let mut proposed_imports = import_assets.search_for_relative_paths(&ctx.sema); + let mut proposed_imports = + import_assets.search_for_relative_paths(&ctx.sema, ctx.config.prefer_no_std); if proposed_imports.is_empty() { return None; } diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs index d139f78a6f36b..9fd5e1886d206 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs @@ -85,7 +85,7 @@ pub(crate) fn replace_derive_with_manual_impl( }) .flat_map(|trait_| { current_module - .find_use_path(ctx.sema.db, hir::ModuleDef::Trait(trait_)) + .find_use_path(ctx.sema.db, hir::ModuleDef::Trait(trait_), ctx.config.prefer_no_std) .as_ref() .map(mod_path_to_ast) .zip(Some(trait_)) diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/replace_qualified_name_with_use.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/replace_qualified_name_with_use.rs index 2419fa11c1554..dbbc56958f1e2 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/replace_qualified_name_with_use.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/replace_qualified_name_with_use.rs @@ -67,6 +67,7 @@ pub(crate) fn replace_qualified_name_with_use( ctx.sema.db, module, ctx.config.insert_use.prefix_kind, + ctx.config.prefer_no_std, ) }) .flatten(); diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/lib.rs b/src/tools/rust-analyzer/crates/ide-assists/src/lib.rs index e52544db5f530..812d22efbd797 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/lib.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/lib.rs @@ -136,6 +136,7 @@ mod handlers { mod flip_binexpr; mod flip_comma; mod flip_trait_bound; + mod move_format_string_arg; mod generate_constant; mod generate_default_from_enum_variant; mod generate_default_from_new; @@ -254,6 +255,7 @@ mod handlers { merge_imports::merge_imports, merge_match_arms::merge_match_arms, move_bounds::move_bounds_to_where_clause, + move_format_string_arg::move_format_string_arg, move_guard::move_arm_cond_to_match_guard, move_guard::move_guard_to_arm_body, move_module_to_file::move_module_to_file, diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/tests.rs b/src/tools/rust-analyzer/crates/ide-assists/src/tests.rs index 9cd66c6b3b07b..258144bae3d08 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/tests.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/tests.rs @@ -29,6 +29,7 @@ pub(crate) const TEST_CONFIG: AssistConfig = AssistConfig { group: true, skip_glob_imports: true, }, + prefer_no_std: false, }; pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) { diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/tests/generated.rs b/src/tools/rust-analyzer/crates/ide-assists/src/tests/generated.rs index 227e2300f92a0..3a696635afd27 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/tests/generated.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/tests/generated.rs @@ -1591,6 +1591,37 @@ fn apply<T, U, F>(f: F, x: T) -> U where F: FnOnce(T) -> U { ) } +#[test] +fn doctest_move_format_string_arg() { + check_doc_test( + "move_format_string_arg", + r#####" +macro_rules! format_args { + ($lit:literal $(tt:tt)*) => { 0 }, +} +macro_rules! print { + ($($arg:tt)*) => (std::io::_print(format_args!($($arg)*))); +} + +fn main() { + print!("{x + 1}$0"); +} +"#####, + r#####" +macro_rules! format_args { + ($lit:literal $(tt:tt)*) => { 0 }, +} +macro_rules! print { + ($($arg:tt)*) => (std::io::_print(format_args!($($arg)*))); +} + +fn main() { + print!("{}"$0, x + 1); +} +"#####, + ) +} + #[test] fn doctest_move_from_mod_rs() { check_doc_test( diff --git a/src/tools/rust-analyzer/crates/ide-completion/src/completions.rs b/src/tools/rust-analyzer/crates/ide-completion/src/completions.rs index 55c3e28392a4d..97b90c62dd777 100644 --- a/src/tools/rust-analyzer/crates/ide-completion/src/completions.rs +++ b/src/tools/rust-analyzer/crates/ide-completion/src/completions.rs @@ -551,7 +551,11 @@ fn enum_variants_with_paths( } for variant in variants { - if let Some(path) = ctx.module.find_use_path(ctx.db, hir::ModuleDef::from(variant)) { + if let Some(path) = ctx.module.find_use_path( + ctx.db, + hir::ModuleDef::from(variant), + ctx.config.prefer_no_std, + ) { // Variants with trivial paths are already added by the existing completion logic, // so we should avoid adding these twice if path.segments().len() > 1 { diff --git a/src/tools/rust-analyzer/crates/ide-completion/src/completions/expr.rs b/src/tools/rust-analyzer/crates/ide-completion/src/completions/expr.rs index 588b52cc1ee3a..3192b21cfb2e2 100644 --- a/src/tools/rust-analyzer/crates/ide-completion/src/completions/expr.rs +++ b/src/tools/rust-analyzer/crates/ide-completion/src/completions/expr.rs @@ -165,7 +165,11 @@ pub(crate) fn complete_expr_path( hir::Adt::Struct(strukt) => { let path = ctx .module - .find_use_path(ctx.db, hir::ModuleDef::from(strukt)) + .find_use_path( + ctx.db, + hir::ModuleDef::from(strukt), + ctx.config.prefer_no_std, + ) .filter(|it| it.len() > 1); acc.add_struct_literal(ctx, path_ctx, strukt, path, None); @@ -183,7 +187,11 @@ pub(crate) fn complete_expr_path( hir::Adt::Union(un) => { let path = ctx .module - .find_use_path(ctx.db, hir::ModuleDef::from(un)) + .find_use_path( + ctx.db, + hir::ModuleDef::from(un), + ctx.config.prefer_no_std, + ) .filter(|it| it.len() > 1); acc.add_union_literal(ctx, un, path, None); diff --git a/src/tools/rust-analyzer/crates/ide-completion/src/completions/flyimport.rs b/src/tools/rust-analyzer/crates/ide-completion/src/completions/flyimport.rs index f04cc15d7fabd..364969af9c9ab 100644 --- a/src/tools/rust-analyzer/crates/ide-completion/src/completions/flyimport.rs +++ b/src/tools/rust-analyzer/crates/ide-completion/src/completions/flyimport.rs @@ -262,7 +262,11 @@ fn import_on_the_fly( acc.add_all( import_assets - .search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind) + .search_for_imports( + &ctx.sema, + ctx.config.insert_use.prefix_kind, + ctx.config.prefer_no_std, + ) .into_iter() .filter(ns_filter) .filter(|import| { @@ -306,7 +310,11 @@ fn import_on_the_fly_pat_( acc.add_all( import_assets - .search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind) + .search_for_imports( + &ctx.sema, + ctx.config.insert_use.prefix_kind, + ctx.config.prefer_no_std, + ) .into_iter() .filter(ns_filter) .filter(|import| { @@ -344,7 +352,7 @@ fn import_on_the_fly_method( let user_input_lowercased = potential_import_name.to_lowercase(); import_assets - .search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind) + .search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind, ctx.config.prefer_no_std) .into_iter() .filter(|import| { !ctx.is_item_hidden(&import.item_to_import) diff --git a/src/tools/rust-analyzer/crates/ide-completion/src/completions/pattern.rs b/src/tools/rust-analyzer/crates/ide-completion/src/completions/pattern.rs index 71d2d9d434b44..58d5bf114cca4 100644 --- a/src/tools/rust-analyzer/crates/ide-completion/src/completions/pattern.rs +++ b/src/tools/rust-analyzer/crates/ide-completion/src/completions/pattern.rs @@ -145,6 +145,7 @@ pub(crate) fn complete_pattern_path( u.ty(ctx.db) } hir::PathResolution::Def(hir::ModuleDef::BuiltinType(ty)) => ty.ty(ctx.db), + hir::PathResolution::Def(hir::ModuleDef::TypeAlias(ty)) => ty.ty(ctx.db), _ => return, }; diff --git a/src/tools/rust-analyzer/crates/ide-completion/src/completions/postfix/format_like.rs b/src/tools/rust-analyzer/crates/ide-completion/src/completions/postfix/format_like.rs index b273a4cb53ba1..b43bdb9ab9d1a 100644 --- a/src/tools/rust-analyzer/crates/ide-completion/src/completions/postfix/format_like.rs +++ b/src/tools/rust-analyzer/crates/ide-completion/src/completions/postfix/format_like.rs @@ -16,8 +16,11 @@ // // image::https://user-images.githubusercontent.com/48062697/113020656-b560f500-917a-11eb-87de-02991f61beb8.gif[] -use ide_db::SnippetCap; -use syntax::ast::{self, AstToken}; +use ide_db::{ + syntax_helpers::format_string_exprs::{parse_format_exprs, with_placeholders}, + SnippetCap, +}; +use syntax::{ast, AstToken}; use crate::{ completions::postfix::build_postfix_snippet_builder, context::CompletionContext, Completions, @@ -43,250 +46,24 @@ pub(crate) fn add_format_like_completions( cap: SnippetCap, receiver_text: &ast::String, ) { - let input = match string_literal_contents(receiver_text) { - // It's not a string literal, do not parse input. - Some(input) => input, - None => return, - }; - let postfix_snippet = match build_postfix_snippet_builder(ctx, cap, dot_receiver) { Some(it) => it, None => return, }; - let mut parser = FormatStrParser::new(input); - if parser.parse().is_ok() { + if let Ok((out, exprs)) = parse_format_exprs(receiver_text.text()) { + let exprs = with_placeholders(exprs); for (label, macro_name) in KINDS { - let snippet = parser.to_suggestion(macro_name); + let snippet = format!(r#"{}({}, {})"#, macro_name, out, exprs.join(", ")); postfix_snippet(label, macro_name, &snippet).add_to(acc); } } } -/// Checks whether provided item is a string literal. -fn string_literal_contents(item: &ast::String) -> Option<String> { - let item = item.text(); - if item.len() >= 2 && item.starts_with('\"') && item.ends_with('\"') { - return Some(item[1..item.len() - 1].to_owned()); - } - - None -} - -/// Parser for a format-like string. It is more allowing in terms of string contents, -/// as we expect variable placeholders to be filled with expressions. -#[derive(Debug)] -pub(crate) struct FormatStrParser { - input: String, - output: String, - extracted_expressions: Vec<String>, - state: State, - parsed: bool, -} - -#[derive(Debug, Clone, Copy, PartialEq)] -enum State { - NotExpr, - MaybeExpr, - Expr, - MaybeIncorrect, - FormatOpts, -} - -impl FormatStrParser { - pub(crate) fn new(input: String) -> Self { - Self { - input, - output: String::new(), - extracted_expressions: Vec::new(), - state: State::NotExpr, - parsed: false, - } - } - - pub(crate) fn parse(&mut self) -> Result<(), ()> { - let mut current_expr = String::new(); - - let mut placeholder_id = 1; - - // Count of open braces inside of an expression. - // We assume that user knows what they're doing, thus we treat it like a correct pattern, e.g. - // "{MyStruct { val_a: 0, val_b: 1 }}". - let mut inexpr_open_count = 0; - - // We need to escape '\' and '$'. See the comments on `get_receiver_text()` for detail. - let mut chars = self.input.chars().peekable(); - while let Some(chr) = chars.next() { - match (self.state, chr) { - (State::NotExpr, '{') => { - self.output.push(chr); - self.state = State::MaybeExpr; - } - (State::NotExpr, '}') => { - self.output.push(chr); - self.state = State::MaybeIncorrect; - } - (State::NotExpr, _) => { - if matches!(chr, '\\' | '$') { - self.output.push('\\'); - } - self.output.push(chr); - } - (State::MaybeIncorrect, '}') => { - // It's okay, we met "}}". - self.output.push(chr); - self.state = State::NotExpr; - } - (State::MaybeIncorrect, _) => { - // Error in the string. - return Err(()); - } - (State::MaybeExpr, '{') => { - self.output.push(chr); - self.state = State::NotExpr; - } - (State::MaybeExpr, '}') => { - // This is an empty sequence '{}'. Replace it with placeholder. - self.output.push(chr); - self.extracted_expressions.push(format!("${}", placeholder_id)); - placeholder_id += 1; - self.state = State::NotExpr; - } - (State::MaybeExpr, _) => { - if matches!(chr, '\\' | '$') { - current_expr.push('\\'); - } - current_expr.push(chr); - self.state = State::Expr; - } - (State::Expr, '}') => { - if inexpr_open_count == 0 { - self.output.push(chr); - self.extracted_expressions.push(current_expr.trim().into()); - current_expr = String::new(); - self.state = State::NotExpr; - } else { - // We're closing one brace met before inside of the expression. - current_expr.push(chr); - inexpr_open_count -= 1; - } - } - (State::Expr, ':') if chars.peek().copied() == Some(':') => { - // path separator - current_expr.push_str("::"); - chars.next(); - } - (State::Expr, ':') => { - if inexpr_open_count == 0 { - // We're outside of braces, thus assume that it's a specifier, like "{Some(value):?}" - self.output.push(chr); - self.extracted_expressions.push(current_expr.trim().into()); - current_expr = String::new(); - self.state = State::FormatOpts; - } else { - // We're inside of braced expression, assume that it's a struct field name/value delimiter. - current_expr.push(chr); - } - } - (State::Expr, '{') => { - current_expr.push(chr); - inexpr_open_count += 1; - } - (State::Expr, _) => { - if matches!(chr, '\\' | '$') { - current_expr.push('\\'); - } - current_expr.push(chr); - } - (State::FormatOpts, '}') => { - self.output.push(chr); - self.state = State::NotExpr; - } - (State::FormatOpts, _) => { - if matches!(chr, '\\' | '$') { - self.output.push('\\'); - } - self.output.push(chr); - } - } - } - - if self.state != State::NotExpr { - return Err(()); - } - - self.parsed = true; - Ok(()) - } - - pub(crate) fn to_suggestion(&self, macro_name: &str) -> String { - assert!(self.parsed, "Attempt to get a suggestion from not parsed expression"); - - let expressions_as_string = self.extracted_expressions.join(", "); - format!(r#"{}("{}", {})"#, macro_name, self.output, expressions_as_string) - } -} - #[cfg(test)] mod tests { use super::*; - use expect_test::{expect, Expect}; - - fn check(input: &str, expect: &Expect) { - let mut parser = FormatStrParser::new((*input).to_owned()); - let outcome_repr = if parser.parse().is_ok() { - // Parsing should be OK, expected repr is "string; expr_1, expr_2". - if parser.extracted_expressions.is_empty() { - parser.output - } else { - format!("{}; {}", parser.output, parser.extracted_expressions.join(", ")) - } - } else { - // Parsing should fail, expected repr is "-". - "-".to_owned() - }; - - expect.assert_eq(&outcome_repr); - } - - #[test] - fn format_str_parser() { - let test_vector = &[ - ("no expressions", expect![["no expressions"]]), - (r"no expressions with \$0$1", expect![r"no expressions with \\\$0\$1"]), - ("{expr} is {2 + 2}", expect![["{} is {}; expr, 2 + 2"]]), - ("{expr:?}", expect![["{:?}; expr"]]), - ("{expr:1$}", expect![[r"{:1\$}; expr"]]), - ("{$0}", expect![[r"{}; \$0"]]), - ("{malformed", expect![["-"]]), - ("malformed}", expect![["-"]]), - ("{{correct", expect![["{{correct"]]), - ("correct}}", expect![["correct}}"]]), - ("{correct}}}", expect![["{}}}; correct"]]), - ("{correct}}}}}", expect![["{}}}}}; correct"]]), - ("{incorrect}}", expect![["-"]]), - ("placeholders {} {}", expect![["placeholders {} {}; $1, $2"]]), - ("mixed {} {2 + 2} {}", expect![["mixed {} {} {}; $1, 2 + 2, $2"]]), - ( - "{SomeStruct { val_a: 0, val_b: 1 }}", - expect![["{}; SomeStruct { val_a: 0, val_b: 1 }"]], - ), - ("{expr:?} is {2.32f64:.5}", expect![["{:?} is {:.5}; expr, 2.32f64"]]), - ( - "{SomeStruct { val_a: 0, val_b: 1 }:?}", - expect![["{:?}; SomeStruct { val_a: 0, val_b: 1 }"]], - ), - ("{ 2 + 2 }", expect![["{}; 2 + 2"]]), - ("{strsim::jaro_winkle(a)}", expect![["{}; strsim::jaro_winkle(a)"]]), - ("{foo::bar::baz()}", expect![["{}; foo::bar::baz()"]]), - ("{foo::bar():?}", expect![["{:?}; foo::bar()"]]), - ]; - - for (input, output) in test_vector { - check(input, output) - } - } #[test] fn test_into_suggestion() { @@ -302,10 +79,10 @@ mod tests { ]; for (kind, input, output) in test_vector { - let mut parser = FormatStrParser::new((*input).to_owned()); - parser.parse().expect("Parsing must succeed"); - - assert_eq!(&parser.to_suggestion(*kind), output); + let (parsed_string, exprs) = parse_format_exprs(input).unwrap(); + let exprs = with_placeholders(exprs); + let snippet = format!(r#"{}("{}", {})"#, kind, parsed_string, exprs.join(", ")); + assert_eq!(&snippet, output); } } } diff --git a/src/tools/rust-analyzer/crates/ide-completion/src/config.rs b/src/tools/rust-analyzer/crates/ide-completion/src/config.rs index 80d6af28168f5..a0f5e81b4fb6c 100644 --- a/src/tools/rust-analyzer/crates/ide-completion/src/config.rs +++ b/src/tools/rust-analyzer/crates/ide-completion/src/config.rs @@ -17,6 +17,7 @@ pub struct CompletionConfig { pub callable: Option<CallableSnippets>, pub snippet_cap: Option<SnippetCap>, pub insert_use: InsertUseConfig, + pub prefer_no_std: bool, pub snippets: Vec<Snippet>, } diff --git a/src/tools/rust-analyzer/crates/ide-completion/src/lib.rs b/src/tools/rust-analyzer/crates/ide-completion/src/lib.rs index ae1a440d06da7..8d21f4fce0a2b 100644 --- a/src/tools/rust-analyzer/crates/ide-completion/src/lib.rs +++ b/src/tools/rust-analyzer/crates/ide-completion/src/lib.rs @@ -234,7 +234,12 @@ pub fn resolve_completion_edits( ); let import = items_with_name .filter_map(|candidate| { - current_module.find_use_path_prefixed(db, candidate, config.insert_use.prefix_kind) + current_module.find_use_path_prefixed( + db, + candidate, + config.insert_use.prefix_kind, + config.prefer_no_std, + ) }) .find(|mod_path| mod_path.to_string() == full_import_path); if let Some(import_path) = import { diff --git a/src/tools/rust-analyzer/crates/ide-completion/src/snippet.rs b/src/tools/rust-analyzer/crates/ide-completion/src/snippet.rs index dc1039fa623e8..f3b8eae4fe8cd 100644 --- a/src/tools/rust-analyzer/crates/ide-completion/src/snippet.rs +++ b/src/tools/rust-analyzer/crates/ide-completion/src/snippet.rs @@ -174,8 +174,12 @@ fn import_edits(ctx: &CompletionContext<'_>, requires: &[GreenNode]) -> Option<V hir::PathResolution::Def(def) => def.into(), _ => return None, }; - let path = - ctx.module.find_use_path_prefixed(ctx.db, item, ctx.config.insert_use.prefix_kind)?; + let path = ctx.module.find_use_path_prefixed( + ctx.db, + item, + ctx.config.insert_use.prefix_kind, + ctx.config.prefer_no_std, + )?; Some((path.len() > 1).then(|| LocatedImport::new(path.clone(), item, item, None))) }; let mut res = Vec::with_capacity(requires.len()); diff --git a/src/tools/rust-analyzer/crates/ide-completion/src/tests.rs b/src/tools/rust-analyzer/crates/ide-completion/src/tests.rs index cf826648dcf7b..9e2beb9ee3288 100644 --- a/src/tools/rust-analyzer/crates/ide-completion/src/tests.rs +++ b/src/tools/rust-analyzer/crates/ide-completion/src/tests.rs @@ -66,6 +66,7 @@ pub(crate) const TEST_CONFIG: CompletionConfig = CompletionConfig { enable_private_editable: false, callable: Some(CallableSnippets::FillArguments), snippet_cap: SnippetCap::new(true), + prefer_no_std: false, insert_use: InsertUseConfig { granularity: ImportGranularity::Crate, prefix_kind: PrefixKind::Plain, diff --git a/src/tools/rust-analyzer/crates/ide-completion/src/tests/pattern.rs b/src/tools/rust-analyzer/crates/ide-completion/src/tests/pattern.rs index 85c4dbd6625df..db8bef66405ef 100644 --- a/src/tools/rust-analyzer/crates/ide-completion/src/tests/pattern.rs +++ b/src/tools/rust-analyzer/crates/ide-completion/src/tests/pattern.rs @@ -714,3 +714,30 @@ impl Ty { "#]], ); } + +#[test] +fn through_alias() { + check_empty( + r#" +enum Enum<T> { + Unit, + Tuple(T), +} + +type EnumAlias<T> = Enum<T>; + +fn f(x: EnumAlias<u8>) { + match x { + EnumAlias::$0 => (), + _ => (), + } + +} + +"#, + expect![[r#" + bn Tuple(…) Tuple($1)$0 + bn Unit Unit$0 + "#]], + ); +} diff --git a/src/tools/rust-analyzer/crates/ide-db/Cargo.toml b/src/tools/rust-analyzer/crates/ide-db/Cargo.toml index a1b0bd6cb3044..30272bc16f636 100644 --- a/src/tools/rust-analyzer/crates/ide-db/Cargo.toml +++ b/src/tools/rust-analyzer/crates/ide-db/Cargo.toml @@ -20,6 +20,7 @@ either = "1.7.0" itertools = "0.10.3" arrayvec = "0.7.2" indexmap = "1.9.1" +memchr = "2.5.0" stdx = { path = "../stdx", version = "0.0.0" } parser = { path = "../parser", version = "0.0.0" } diff --git a/src/tools/rust-analyzer/crates/ide-db/src/imports/import_assets.rs b/src/tools/rust-analyzer/crates/ide-db/src/imports/import_assets.rs index 26ef86155e534..40a6a3e8970fb 100644 --- a/src/tools/rust-analyzer/crates/ide-db/src/imports/import_assets.rs +++ b/src/tools/rust-analyzer/crates/ide-db/src/imports/import_assets.rs @@ -212,18 +212,20 @@ impl ImportAssets { &self, sema: &Semantics<'_, RootDatabase>, prefix_kind: PrefixKind, + prefer_no_std: bool, ) -> Vec<LocatedImport> { let _p = profile::span("import_assets::search_for_imports"); - self.search_for(sema, Some(prefix_kind)) + self.search_for(sema, Some(prefix_kind), prefer_no_std) } /// This may return non-absolute paths if a part of the returned path is already imported into scope. pub fn search_for_relative_paths( &self, sema: &Semantics<'_, RootDatabase>, + prefer_no_std: bool, ) -> Vec<LocatedImport> { let _p = profile::span("import_assets::search_for_relative_paths"); - self.search_for(sema, None) + self.search_for(sema, None, prefer_no_std) } pub fn path_fuzzy_name_to_exact(&mut self, case_sensitive: bool) { @@ -242,6 +244,7 @@ impl ImportAssets { &self, sema: &Semantics<'_, RootDatabase>, prefixed: Option<PrefixKind>, + prefer_no_std: bool, ) -> Vec<LocatedImport> { let _p = profile::span("import_assets::search_for"); @@ -252,6 +255,7 @@ impl ImportAssets { item_for_path_search(sema.db, item)?, &self.module_with_candidate, prefixed, + prefer_no_std, ) }; @@ -564,11 +568,12 @@ fn get_mod_path( item_to_search: ItemInNs, module_with_candidate: &Module, prefixed: Option<PrefixKind>, + prefer_no_std: bool, ) -> Option<ModPath> { if let Some(prefix_kind) = prefixed { - module_with_candidate.find_use_path_prefixed(db, item_to_search, prefix_kind) + module_with_candidate.find_use_path_prefixed(db, item_to_search, prefix_kind, prefer_no_std) } else { - module_with_candidate.find_use_path(db, item_to_search) + module_with_candidate.find_use_path(db, item_to_search, prefer_no_std) } } diff --git a/src/tools/rust-analyzer/crates/ide-db/src/lib.rs b/src/tools/rust-analyzer/crates/ide-db/src/lib.rs index 1ec62a8425a39..e0bc0f89f0a1d 100644 --- a/src/tools/rust-analyzer/crates/ide-db/src/lib.rs +++ b/src/tools/rust-analyzer/crates/ide-db/src/lib.rs @@ -38,6 +38,7 @@ pub mod syntax_helpers { pub mod node_ext; pub mod insert_whitespace_into_node; pub mod format_string; + pub mod format_string_exprs; pub use parser::LexedStr; } diff --git a/src/tools/rust-analyzer/crates/ide-db/src/path_transform.rs b/src/tools/rust-analyzer/crates/ide-db/src/path_transform.rs index 40af9e6fe2ad8..12d873b4a0aa8 100644 --- a/src/tools/rust-analyzer/crates/ide-db/src/path_transform.rs +++ b/src/tools/rust-analyzer/crates/ide-db/src/path_transform.rs @@ -173,6 +173,7 @@ impl<'a> Ctx<'a> { let found_path = self.target_module.find_use_path( self.source_scope.db.upcast(), hir::ModuleDef::Trait(trait_ref), + false, )?; match ast::make::ty_path(mod_path_to_ast(&found_path)) { ast::Type::PathType(path_ty) => Some(path_ty), @@ -209,7 +210,7 @@ impl<'a> Ctx<'a> { } let found_path = - self.target_module.find_use_path(self.source_scope.db.upcast(), def)?; + self.target_module.find_use_path(self.source_scope.db.upcast(), def, false)?; let res = mod_path_to_ast(&found_path).clone_for_update(); if let Some(args) = path.segment().and_then(|it| it.generic_arg_list()) { if let Some(segment) = res.segment() { diff --git a/src/tools/rust-analyzer/crates/ide-db/src/search.rs b/src/tools/rust-analyzer/crates/ide-db/src/search.rs index 7deffe8e0f637..7cabdb55e8d28 100644 --- a/src/tools/rust-analyzer/crates/ide-db/src/search.rs +++ b/src/tools/rust-analyzer/crates/ide-db/src/search.rs @@ -8,7 +8,9 @@ use std::{mem, sync::Arc}; use base_db::{FileId, FileRange, SourceDatabase, SourceDatabaseExt}; use hir::{DefWithBody, HasAttrs, HasSource, InFile, ModuleSource, Semantics, Visibility}; +use memchr::memmem::Finder; use once_cell::unsync::Lazy; +use parser::SyntaxKind; use stdx::hash::NoHashHashMap; use syntax::{ast, match_ast, AstNode, TextRange, TextSize}; @@ -67,6 +69,7 @@ pub enum ReferenceCategory { // Create Write, Read, + Import, // FIXME: Some day should be able to search in doc comments. Would probably // need to switch from enum to bitflags then? // DocComment @@ -409,14 +412,17 @@ impl<'a> FindUsages<'a> { Some(s) => s.as_str(), None => return, }; + let finder = &Finder::new(name); + let include_self_kw_refs = + self.include_self_kw_refs.as_ref().map(|ty| (ty, Finder::new("Self"))); - // these can't be closures because rust infers the lifetimes wrong ... + // for<'a> |text: &'a str, name: &'a str, search_range: TextRange| -> impl Iterator<Item = TextSize> + 'a { ... } fn match_indices<'a>( text: &'a str, - name: &'a str, + finder: &'a Finder<'a>, search_range: TextRange, ) -> impl Iterator<Item = TextSize> + 'a { - text.match_indices(name).filter_map(move |(idx, _)| { + finder.find_iter(text.as_bytes()).filter_map(move |idx| { let offset: TextSize = idx.try_into().unwrap(); if !search_range.contains_inclusive(offset) { return None; @@ -425,6 +431,7 @@ impl<'a> FindUsages<'a> { }) } + // for<'a> |scope: &'a SearchScope| -> impl Iterator<Item = (Arc<String>, FileId, TextRange)> + 'a { ... } fn scope_files<'a>( sema: &'a Semantics<'_, RootDatabase>, scope: &'a SearchScope, @@ -448,7 +455,7 @@ impl<'a> FindUsages<'a> { let tree = Lazy::new(move || sema.parse(file_id).syntax().clone()); // Search for occurrences of the items name - for offset in match_indices(&text, name, search_range) { + for offset in match_indices(&text, finder, search_range) { for name in sema.find_nodes_at_offset_with_descend(&tree, offset) { if match name { ast::NameLike::NameRef(name_ref) => self.found_name_ref(&name_ref, sink), @@ -460,8 +467,8 @@ impl<'a> FindUsages<'a> { } } // Search for occurrences of the `Self` referring to our type - if let Some(self_ty) = &self.include_self_kw_refs { - for offset in match_indices(&text, "Self", search_range) { + if let Some((self_ty, finder)) = &include_self_kw_refs { + for offset in match_indices(&text, finder, search_range) { for name_ref in sema.find_nodes_at_offset_with_descend(&tree, offset) { if self.found_self_ty_name_ref(self_ty, &name_ref, sink) { return; @@ -477,20 +484,22 @@ impl<'a> FindUsages<'a> { let scope = search_scope .intersection(&SearchScope::module_and_children(self.sema.db, module)); - let is_crate_root = module.is_crate_root(self.sema.db); + let is_crate_root = + module.is_crate_root(self.sema.db).then(|| Finder::new("crate")); + let finder = &Finder::new("super"); for (text, file_id, search_range) in scope_files(sema, &scope) { let tree = Lazy::new(move || sema.parse(file_id).syntax().clone()); - for offset in match_indices(&text, "super", search_range) { + for offset in match_indices(&text, finder, search_range) { for name_ref in sema.find_nodes_at_offset_with_descend(&tree, offset) { if self.found_name_ref(&name_ref, sink) { return; } } } - if is_crate_root { - for offset in match_indices(&text, "crate", search_range) { + if let Some(finder) = &is_crate_root { + for offset in match_indices(&text, finder, search_range) { for name_ref in sema.find_nodes_at_offset_with_descend(&tree, offset) { if self.found_name_ref(&name_ref, sink) { return; @@ -531,8 +540,9 @@ impl<'a> FindUsages<'a> { search_range.unwrap_or_else(|| TextRange::up_to(TextSize::of(text.as_str()))); let tree = Lazy::new(|| sema.parse(file_id).syntax().clone()); + let finder = &Finder::new("self"); - for offset in match_indices(&text, "self", search_range) { + for offset in match_indices(&text, finder, search_range) { for name_ref in sema.find_nodes_at_offset_with_descend(&tree, offset) { if self.found_self_module_name_ref(&name_ref, sink) { return; @@ -577,7 +587,7 @@ impl<'a> FindUsages<'a> { let reference = FileReference { range, name: ast::NameLike::NameRef(name_ref.clone()), - category: None, + category: is_name_ref_in_import(name_ref).then(|| ReferenceCategory::Import), }; sink(file_id, reference) } @@ -756,7 +766,7 @@ impl ReferenceCategory { fn new(def: &Definition, r: &ast::NameRef) -> Option<ReferenceCategory> { // Only Locals and Fields have accesses for now. if !matches!(def, Definition::Local(_) | Definition::Field(_)) { - return None; + return is_name_ref_in_import(r).then(|| ReferenceCategory::Import); } let mode = r.syntax().ancestors().find_map(|node| { @@ -783,3 +793,12 @@ impl ReferenceCategory { mode.or(Some(ReferenceCategory::Read)) } } + +fn is_name_ref_in_import(name_ref: &ast::NameRef) -> bool { + name_ref + .syntax() + .parent() + .and_then(ast::PathSegment::cast) + .and_then(|it| it.parent_path().top_path().syntax().parent()) + .map_or(false, |it| it.kind() == SyntaxKind::USE_TREE) +} diff --git a/src/tools/rust-analyzer/crates/ide-db/src/syntax_helpers/format_string_exprs.rs b/src/tools/rust-analyzer/crates/ide-db/src/syntax_helpers/format_string_exprs.rs new file mode 100644 index 0000000000000..ac6c6e8feeea0 --- /dev/null +++ b/src/tools/rust-analyzer/crates/ide-db/src/syntax_helpers/format_string_exprs.rs @@ -0,0 +1,267 @@ +//! Tools to work with expressions present in format string literals for the `format_args!` family of macros. +//! Primarily meant for assists and completions. + +/// Enum for represenging extraced format string args. +/// Can either be extracted expressions (which includes identifiers), +/// or placeholders `{}`. +#[derive(Debug, PartialEq, Eq)] +pub enum Arg { + Placeholder, + Ident(String), + Expr(String), +} + +/** + Add placeholders like `$1` and `$2` in place of [`Arg::Placeholder`], + and unwraps the [`Arg::Ident`] and [`Arg::Expr`] enums. + ```rust + # use ide_db::syntax_helpers::format_string_exprs::*; + assert_eq!(with_placeholders(vec![Arg::Ident("ident".to_owned()), Arg::Placeholder, Arg::Expr("expr + 2".to_owned())]), vec!["ident".to_owned(), "$1".to_owned(), "expr + 2".to_owned()]) + ``` +*/ + +pub fn with_placeholders(args: Vec<Arg>) -> Vec<String> { + let mut placeholder_id = 1; + args.into_iter() + .map(move |a| match a { + Arg::Expr(s) | Arg::Ident(s) => s, + Arg::Placeholder => { + let s = format!("${placeholder_id}"); + placeholder_id += 1; + s + } + }) + .collect() +} + +/** + Parser for a format-like string. It is more allowing in terms of string contents, + as we expect variable placeholders to be filled with expressions. + + Built for completions and assists, and escapes `\` and `$` in output. + (See the comments on `get_receiver_text()` for detail.) + Splits a format string that may contain expressions + like + ```rust + assert_eq!(parse("{ident} {} {expr + 42} ").unwrap(), ("{} {} {}", vec![Arg::Ident("ident"), Arg::Placeholder, Arg::Expr("expr + 42")])); + ``` +*/ +pub fn parse_format_exprs(input: &str) -> Result<(String, Vec<Arg>), ()> { + #[derive(Debug, Clone, Copy, PartialEq)] + enum State { + NotArg, + MaybeArg, + Expr, + Ident, + MaybeIncorrect, + FormatOpts, + } + + let mut state = State::NotArg; + let mut current_expr = String::new(); + let mut extracted_expressions = Vec::new(); + let mut output = String::new(); + + // Count of open braces inside of an expression. + // We assume that user knows what they're doing, thus we treat it like a correct pattern, e.g. + // "{MyStruct { val_a: 0, val_b: 1 }}". + let mut inexpr_open_count = 0; + + let mut chars = input.chars().peekable(); + while let Some(chr) = chars.next() { + match (state, chr) { + (State::NotArg, '{') => { + output.push(chr); + state = State::MaybeArg; + } + (State::NotArg, '}') => { + output.push(chr); + state = State::MaybeIncorrect; + } + (State::NotArg, _) => { + if matches!(chr, '\\' | '$') { + output.push('\\'); + } + output.push(chr); + } + (State::MaybeIncorrect, '}') => { + // It's okay, we met "}}". + output.push(chr); + state = State::NotArg; + } + (State::MaybeIncorrect, _) => { + // Error in the string. + return Err(()); + } + // Escaped braces `{{` + (State::MaybeArg, '{') => { + output.push(chr); + state = State::NotArg; + } + (State::MaybeArg, '}') => { + // This is an empty sequence '{}'. + output.push(chr); + extracted_expressions.push(Arg::Placeholder); + state = State::NotArg; + } + (State::MaybeArg, _) => { + if matches!(chr, '\\' | '$') { + current_expr.push('\\'); + } + current_expr.push(chr); + + // While Rust uses the unicode sets of XID_start and XID_continue for Identifiers + // this is probably the best we can do to avoid a false positive + if chr.is_alphabetic() || chr == '_' { + state = State::Ident; + } else { + state = State::Expr; + } + } + (State::Ident | State::Expr, '}') => { + if inexpr_open_count == 0 { + output.push(chr); + + if matches!(state, State::Expr) { + extracted_expressions.push(Arg::Expr(current_expr.trim().into())); + } else { + extracted_expressions.push(Arg::Ident(current_expr.trim().into())); + } + + current_expr = String::new(); + state = State::NotArg; + } else { + // We're closing one brace met before inside of the expression. + current_expr.push(chr); + inexpr_open_count -= 1; + } + } + (State::Ident | State::Expr, ':') if matches!(chars.peek(), Some(':')) => { + // path separator + state = State::Expr; + current_expr.push_str("::"); + chars.next(); + } + (State::Ident | State::Expr, ':') => { + if inexpr_open_count == 0 { + // We're outside of braces, thus assume that it's a specifier, like "{Some(value):?}" + output.push(chr); + + if matches!(state, State::Expr) { + extracted_expressions.push(Arg::Expr(current_expr.trim().into())); + } else { + extracted_expressions.push(Arg::Ident(current_expr.trim().into())); + } + + current_expr = String::new(); + state = State::FormatOpts; + } else { + // We're inside of braced expression, assume that it's a struct field name/value delimiter. + current_expr.push(chr); + } + } + (State::Ident | State::Expr, '{') => { + state = State::Expr; + current_expr.push(chr); + inexpr_open_count += 1; + } + (State::Ident | State::Expr, _) => { + if !(chr.is_alphanumeric() || chr == '_' || chr == '#') { + state = State::Expr; + } + + if matches!(chr, '\\' | '$') { + current_expr.push('\\'); + } + current_expr.push(chr); + } + (State::FormatOpts, '}') => { + output.push(chr); + state = State::NotArg; + } + (State::FormatOpts, _) => { + if matches!(chr, '\\' | '$') { + output.push('\\'); + } + output.push(chr); + } + } + } + + if state != State::NotArg { + return Err(()); + } + + Ok((output, extracted_expressions)) +} + +#[cfg(test)] +mod tests { + use super::*; + use expect_test::{expect, Expect}; + + fn check(input: &str, expect: &Expect) { + let (output, exprs) = parse_format_exprs(input).unwrap_or(("-".to_string(), vec![])); + let outcome_repr = if !exprs.is_empty() { + format!("{}; {}", output, with_placeholders(exprs).join(", ")) + } else { + output + }; + + expect.assert_eq(&outcome_repr); + } + + #[test] + fn format_str_parser() { + let test_vector = &[ + ("no expressions", expect![["no expressions"]]), + (r"no expressions with \$0$1", expect![r"no expressions with \\\$0\$1"]), + ("{expr} is {2 + 2}", expect![["{} is {}; expr, 2 + 2"]]), + ("{expr:?}", expect![["{:?}; expr"]]), + ("{expr:1$}", expect![[r"{:1\$}; expr"]]), + ("{$0}", expect![[r"{}; \$0"]]), + ("{malformed", expect![["-"]]), + ("malformed}", expect![["-"]]), + ("{{correct", expect![["{{correct"]]), + ("correct}}", expect![["correct}}"]]), + ("{correct}}}", expect![["{}}}; correct"]]), + ("{correct}}}}}", expect![["{}}}}}; correct"]]), + ("{incorrect}}", expect![["-"]]), + ("placeholders {} {}", expect![["placeholders {} {}; $1, $2"]]), + ("mixed {} {2 + 2} {}", expect![["mixed {} {} {}; $1, 2 + 2, $2"]]), + ( + "{SomeStruct { val_a: 0, val_b: 1 }}", + expect![["{}; SomeStruct { val_a: 0, val_b: 1 }"]], + ), + ("{expr:?} is {2.32f64:.5}", expect![["{:?} is {:.5}; expr, 2.32f64"]]), + ( + "{SomeStruct { val_a: 0, val_b: 1 }:?}", + expect![["{:?}; SomeStruct { val_a: 0, val_b: 1 }"]], + ), + ("{ 2 + 2 }", expect![["{}; 2 + 2"]]), + ("{strsim::jaro_winkle(a)}", expect![["{}; strsim::jaro_winkle(a)"]]), + ("{foo::bar::baz()}", expect![["{}; foo::bar::baz()"]]), + ("{foo::bar():?}", expect![["{:?}; foo::bar()"]]), + ]; + + for (input, output) in test_vector { + check(input, output) + } + } + + #[test] + fn arg_type() { + assert_eq!( + parse_format_exprs("{_ident} {r#raw_ident} {expr.obj} {name {thing: 42} } {}") + .unwrap() + .1, + vec![ + Arg::Ident("_ident".to_owned()), + Arg::Ident("r#raw_ident".to_owned()), + Arg::Expr("expr.obj".to_owned()), + Arg::Expr("name {thing: 42}".to_owned()), + Arg::Placeholder + ] + ); + } +} diff --git a/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/json_is_not_rust.rs b/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/json_is_not_rust.rs index a21db5b2cec9d..3034295196b42 100644 --- a/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/json_is_not_rust.rs +++ b/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/json_is_not_rust.rs @@ -137,6 +137,7 @@ pub(crate) fn json_in_items( sema.db, it, config.insert_use.prefix_kind, + config.prefer_no_std, ) { insert_use( &scope, @@ -152,6 +153,7 @@ pub(crate) fn json_in_items( sema.db, it, config.insert_use.prefix_kind, + config.prefer_no_std, ) { insert_use( &scope, diff --git a/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/missing_fields.rs b/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/missing_fields.rs index edb1fc0919c24..7f140eb6a74a6 100644 --- a/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/missing_fields.rs +++ b/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/missing_fields.rs @@ -124,6 +124,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass let type_path = current_module?.find_use_path( ctx.sema.db, item_for_path_search(ctx.sema.db, item_in_ns)?, + ctx.config.prefer_no_std, )?; use_trivial_constructor( diff --git a/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/type_mismatch.rs b/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/type_mismatch.rs index 6bf90e645b49a..62c69f90baa4f 100644 --- a/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/type_mismatch.rs +++ b/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/type_mismatch.rs @@ -59,9 +59,6 @@ fn add_reference( d: &hir::TypeMismatch, acc: &mut Vec<Assist>, ) -> Option<()> { - let root = ctx.sema.db.parse_or_expand(d.expr.file_id)?; - let expr_node = d.expr.value.to_node(&root); - let range = ctx.sema.diagnostics_display_range(d.expr.clone().map(|it| it.into())).range; let (_, mutability) = d.expected.as_reference()?; @@ -72,7 +69,7 @@ fn add_reference( let ampersands = format!("&{}", mutability.as_keyword_for_ref()); - let edit = TextEdit::insert(expr_node.syntax().text_range().start(), ampersands); + let edit = TextEdit::insert(range.start(), ampersands); let source_change = SourceChange::from_text_edit(d.expr.file_id.original_file(ctx.sema.db), edit); acc.push(fix("add_reference_here", "Add reference here", source_change, range)); @@ -314,6 +311,34 @@ fn main() { ); } + #[test] + fn test_add_reference_to_macro_call() { + check_fix( + r#" +macro_rules! thousand { + () => { + 1000_u64 + }; +} +fn test(foo: &u64) {} +fn main() { + test($0thousand!()); +} + "#, + r#" +macro_rules! thousand { + () => { + 1000_u64 + }; +} +fn test(foo: &u64) {} +fn main() { + test(&thousand!()); +} + "#, + ); + } + #[test] fn test_add_mutable_reference_to_let_stmt() { check_fix( diff --git a/src/tools/rust-analyzer/crates/ide-diagnostics/src/lib.rs b/src/tools/rust-analyzer/crates/ide-diagnostics/src/lib.rs index 61e63ea7a93ca..ae299f0584148 100644 --- a/src/tools/rust-analyzer/crates/ide-diagnostics/src/lib.rs +++ b/src/tools/rust-analyzer/crates/ide-diagnostics/src/lib.rs @@ -150,6 +150,7 @@ pub struct DiagnosticsConfig { pub expr_fill_default: ExprFillDefaultMode, // FIXME: We may want to include a whole `AssistConfig` here pub insert_use: InsertUseConfig, + pub prefer_no_std: bool, } impl DiagnosticsConfig { @@ -170,6 +171,7 @@ impl DiagnosticsConfig { group: false, skip_glob_imports: false, }, + prefer_no_std: false, } } } diff --git a/src/tools/rust-analyzer/crates/ide-ssr/src/matching.rs b/src/tools/rust-analyzer/crates/ide-ssr/src/matching.rs index e3a837ddcdb05..57b5ab6abda69 100644 --- a/src/tools/rust-analyzer/crates/ide-ssr/src/matching.rs +++ b/src/tools/rust-analyzer/crates/ide-ssr/src/matching.rs @@ -648,9 +648,10 @@ impl Match { .module(); for (path, resolved_path) in &template.resolved_paths { if let hir::PathResolution::Def(module_def) = resolved_path.resolution { - let mod_path = module.find_use_path(sema.db, module_def).ok_or_else(|| { - match_error!("Failed to render template path `{}` at match location") - })?; + let mod_path = + module.find_use_path(sema.db, module_def, false).ok_or_else(|| { + match_error!("Failed to render template path `{}` at match location") + })?; self.rendered_template_paths.insert(path.clone(), mod_path); } } diff --git a/src/tools/rust-analyzer/crates/ide/src/annotations.rs b/src/tools/rust-analyzer/crates/ide/src/annotations.rs index 210c5c7facd2d..bfbe0db6e4b84 100644 --- a/src/tools/rust-analyzer/crates/ide/src/annotations.rs +++ b/src/tools/rust-analyzer/crates/ide/src/annotations.rs @@ -41,6 +41,12 @@ pub struct AnnotationConfig { pub annotate_references: bool, pub annotate_method_references: bool, pub annotate_enum_variant_references: bool, + pub location: AnnotationLocation, +} + +pub enum AnnotationLocation { + AboveName, + AboveWholeItem, } pub(crate) fn annotations( @@ -65,10 +71,10 @@ pub(crate) fn annotations( visit_file_defs(&Semantics::new(db), file_id, &mut |def| { let range = match def { Definition::Const(konst) if config.annotate_references => { - konst.source(db).and_then(|node| name_range(db, node, file_id)) + konst.source(db).and_then(|node| name_range(db, config, node, file_id)) } Definition::Trait(trait_) if config.annotate_references || config.annotate_impls => { - trait_.source(db).and_then(|node| name_range(db, node, file_id)) + trait_.source(db).and_then(|node| name_range(db, config, node, file_id)) } Definition::Adt(adt) => match adt { hir::Adt::Enum(enum_) => { @@ -77,7 +83,9 @@ pub(crate) fn annotations( .variants(db) .into_iter() .map(|variant| { - variant.source(db).and_then(|node| name_range(db, node, file_id)) + variant + .source(db) + .and_then(|node| name_range(db, config, node, file_id)) }) .flatten() .for_each(|range| { @@ -88,14 +96,14 @@ pub(crate) fn annotations( }) } if config.annotate_references || config.annotate_impls { - enum_.source(db).and_then(|node| name_range(db, node, file_id)) + enum_.source(db).and_then(|node| name_range(db, config, node, file_id)) } else { None } } _ => { if config.annotate_references || config.annotate_impls { - adt.source(db).and_then(|node| name_range(db, node, file_id)) + adt.source(db).and_then(|node| name_range(db, config, node, file_id)) } else { None } @@ -113,6 +121,7 @@ pub(crate) fn annotations( annotations .push(Annotation { range, kind: AnnotationKind::HasImpls { file_id, data: None } }); } + if config.annotate_references { annotations.push(Annotation { range, @@ -122,12 +131,18 @@ pub(crate) fn annotations( fn name_range<T: HasName>( db: &RootDatabase, + config: &AnnotationConfig, node: InFile<T>, source_file_id: FileId, ) -> Option<TextRange> { if let Some(InFile { file_id, value }) = node.original_ast_node(db) { if file_id == source_file_id.into() { - return value.name().map(|it| it.syntax().text_range()); + return match config.location { + AnnotationLocation::AboveName => { + value.name().map(|name| name.syntax().text_range()) + } + AnnotationLocation::AboveWholeItem => Some(value.syntax().text_range()), + }; } } None @@ -188,21 +203,23 @@ mod tests { use crate::{fixture, Annotation, AnnotationConfig}; - fn check(ra_fixture: &str, expect: Expect) { + use super::AnnotationLocation; + + const DEFAULT_CONFIG: AnnotationConfig = AnnotationConfig { + binary_target: true, + annotate_runnables: true, + annotate_impls: true, + annotate_references: true, + annotate_method_references: true, + annotate_enum_variant_references: true, + location: AnnotationLocation::AboveName, + }; + + fn check_with_config(ra_fixture: &str, expect: Expect, config: &AnnotationConfig) { let (analysis, file_id) = fixture::file(ra_fixture); let annotations: Vec<Annotation> = analysis - .annotations( - &AnnotationConfig { - binary_target: true, - annotate_runnables: true, - annotate_impls: true, - annotate_references: true, - annotate_method_references: true, - annotate_enum_variant_references: true, - }, - file_id, - ) + .annotations(config, file_id) .unwrap() .into_iter() .map(|annotation| analysis.resolve_annotation(annotation).unwrap()) @@ -211,6 +228,10 @@ mod tests { expect.assert_debug_eq(&annotations); } + fn check(ra_fixture: &str, expect: Expect) { + check_with_config(ra_fixture, expect, &DEFAULT_CONFIG); + } + #[test] fn const_annotations() { check( @@ -786,4 +807,40 @@ m!(); "#]], ); } + + #[test] + fn test_annotations_appear_above_whole_item_when_configured_to_do_so() { + check_with_config( + r#" +/// This is a struct named Foo, obviously. +#[derive(Clone)] +struct Foo; +"#, + expect![[r#" + [ + Annotation { + range: 0..71, + kind: HasImpls { + file_id: FileId( + 0, + ), + data: Some( + [], + ), + }, + }, + Annotation { + range: 0..71, + kind: HasReferences { + file_id: FileId( + 0, + ), + data: None, + }, + }, + ] + "#]], + &AnnotationConfig { location: AnnotationLocation::AboveWholeItem, ..DEFAULT_CONFIG }, + ); + } } diff --git a/src/tools/rust-analyzer/crates/ide/src/highlight_related.rs b/src/tools/rust-analyzer/crates/ide/src/highlight_related.rs index f190da326e455..1bdd626f1e834 100644 --- a/src/tools/rust-analyzer/crates/ide/src/highlight_related.rs +++ b/src/tools/rust-analyzer/crates/ide/src/highlight_related.rs @@ -377,6 +377,7 @@ mod tests { match it { ReferenceCategory::Read => "read", ReferenceCategory::Write => "write", + ReferenceCategory::Import => "import", } .to_string() }), @@ -423,12 +424,12 @@ struct Foo; check( r#" use crate$0; - //^^^^^ + //^^^^^ import use self; - //^^^^ + //^^^^ import mod __ { use super; - //^^^^^ + //^^^^^ import } "#, ); @@ -436,7 +437,7 @@ mod __ { r#" //- /main.rs crate:main deps:lib use lib$0; - //^^^ + //^^^ import //- /lib.rs crate:lib "#, ); @@ -450,7 +451,7 @@ use lib$0; mod foo; //- /foo.rs use self$0; - // ^^^^ + // ^^^^ import "#, ); } diff --git a/src/tools/rust-analyzer/crates/ide/src/inlay_hints.rs b/src/tools/rust-analyzer/crates/ide/src/inlay_hints.rs index d1b1d2c331a51..93fcd7cad7a18 100644 --- a/src/tools/rust-analyzer/crates/ide/src/inlay_hints.rs +++ b/src/tools/rust-analyzer/crates/ide/src/inlay_hints.rs @@ -1687,6 +1687,74 @@ fn main() { ); } + #[test] + fn iterator_hint_regression_issue_12674() { + // Ensure we don't crash while solving the projection type of iterators. + check_expect( + InlayHintsConfig { chaining_hints: true, ..DISABLED_CONFIG }, + r#" +//- minicore: iterators +struct S<T>(T); +impl<T> S<T> { + fn iter(&self) -> Iter<'_, T> { loop {} } +} +struct Iter<'a, T: 'a>(&'a T); +impl<'a, T> Iterator for Iter<'a, T> { + type Item = &'a T; + fn next(&mut self) -> Option<Self::Item> { loop {} } +} +struct Container<'a> { + elements: S<&'a str>, +} +struct SliceIter<'a, T>(&'a T); +impl<'a, T> Iterator for SliceIter<'a, T> { + type Item = &'a T; + fn next(&mut self) -> Option<Self::Item> { loop {} } +} + +fn main(a: SliceIter<'_, Container>) { + a + .filter_map(|c| Some(c.elements.iter().filter_map(|v| Some(v)))) + .map(|e| e); +} + "#, + expect![[r#" + [ + InlayHint { + range: 484..554, + kind: ChainingHint, + label: [ + "impl Iterator<Item = impl Iterator<Item = &&str>>", + ], + tooltip: Some( + HoverRanged( + FileId( + 0, + ), + 484..554, + ), + ), + }, + InlayHint { + range: 484..485, + kind: ChainingHint, + label: [ + "SliceIter<Container>", + ], + tooltip: Some( + HoverRanged( + FileId( + 0, + ), + 484..485, + ), + ), + }, + ] + "#]], + ); + } + #[test] fn infer_call_method_return_associated_types_with_generic() { check_types( diff --git a/src/tools/rust-analyzer/crates/ide/src/lib.rs b/src/tools/rust-analyzer/crates/ide/src/lib.rs index 0552330814aa4..c1ef25b592b1b 100644 --- a/src/tools/rust-analyzer/crates/ide/src/lib.rs +++ b/src/tools/rust-analyzer/crates/ide/src/lib.rs @@ -74,7 +74,7 @@ use syntax::SourceFile; use crate::navigation_target::{ToNav, TryToNav}; pub use crate::{ - annotations::{Annotation, AnnotationConfig, AnnotationKind}, + annotations::{Annotation, AnnotationConfig, AnnotationKind, AnnotationLocation}, call_hierarchy::CallItem, expand_macro::ExpandedMacro, file_structure::{StructureNode, StructureNodeKind}, diff --git a/src/tools/rust-analyzer/crates/ide/src/references.rs b/src/tools/rust-analyzer/crates/ide/src/references.rs index 99614b645e48f..e942413c11057 100644 --- a/src/tools/rust-analyzer/crates/ide/src/references.rs +++ b/src/tools/rust-analyzer/crates/ide/src/references.rs @@ -742,7 +742,7 @@ pub struct Foo { expect![[r#" foo Module FileId(0) 0..8 4..7 - FileId(0) 14..17 + FileId(0) 14..17 Import "#]], ); } @@ -760,7 +760,7 @@ use self$0; expect![[r#" foo Module FileId(0) 0..8 4..7 - FileId(1) 4..8 + FileId(1) 4..8 Import "#]], ); } @@ -775,7 +775,7 @@ use self$0; expect![[r#" Module FileId(0) 0..10 - FileId(0) 4..8 + FileId(0) 4..8 Import "#]], ); } @@ -803,7 +803,7 @@ pub(super) struct Foo$0 { expect![[r#" Foo Struct FileId(2) 0..41 18..21 - FileId(1) 20..23 + FileId(1) 20..23 Import FileId(1) 47..50 "#]], ); @@ -966,7 +966,7 @@ fn g() { f(); } expect![[r#" f Function FileId(0) 22..31 25..26 - FileId(1) 11..12 + FileId(1) 11..12 Import FileId(1) 24..25 "#]], ); @@ -1424,9 +1424,9 @@ pub use level1::Foo; expect![[r#" Foo Struct FileId(0) 0..15 11..14 - FileId(1) 16..19 - FileId(2) 16..19 - FileId(3) 16..19 + FileId(1) 16..19 Import + FileId(2) 16..19 Import + FileId(3) 16..19 Import "#]], ); } @@ -1454,7 +1454,7 @@ lib::foo!(); expect![[r#" foo Macro FileId(1) 0..61 29..32 - FileId(0) 46..49 + FileId(0) 46..49 Import FileId(2) 0..3 FileId(3) 5..8 "#]], @@ -1617,7 +1617,7 @@ struct Foo; expect![[r#" derive_identity Derive FileId(2) 1..107 45..60 - FileId(0) 17..31 + FileId(0) 17..31 Import FileId(0) 56..70 "#]], ); diff --git a/src/tools/rust-analyzer/crates/mbe/src/expander/matcher.rs b/src/tools/rust-analyzer/crates/mbe/src/expander/matcher.rs index c1aa14d6b7e74..139a8cb8cbe58 100644 --- a/src/tools/rust-analyzer/crates/mbe/src/expander/matcher.rs +++ b/src/tools/rust-analyzer/crates/mbe/src/expander/matcher.rs @@ -203,17 +203,16 @@ impl BindingsBuilder { } fn build(self, idx: &BindingsIdx) -> Bindings { - let mut bindings = Bindings::default(); - self.build_inner(&mut bindings, &self.nodes[idx.0]); - bindings + self.build_inner(&self.nodes[idx.0]) } - fn build_inner(&self, bindings: &mut Bindings, link_nodes: &[LinkNode<Rc<BindingKind>>]) { + fn build_inner(&self, link_nodes: &[LinkNode<Rc<BindingKind>>]) -> Bindings { + let mut bindings = Bindings::default(); let mut nodes = Vec::new(); self.collect_nodes(link_nodes, &mut nodes); for cmd in nodes { - match &**cmd { + match cmd { BindingKind::Empty(name) => { bindings.push_empty(name); } @@ -246,13 +245,15 @@ impl BindingsBuilder { } } } + + bindings } fn collect_nested_ref<'a>( &'a self, id: usize, len: usize, - nested_refs: &mut Vec<&'a Vec<LinkNode<Rc<BindingKind>>>>, + nested_refs: &mut Vec<&'a [LinkNode<Rc<BindingKind>>]>, ) { self.nested[id].iter().take(len).for_each(|it| match it { LinkNode::Node(id) => nested_refs.push(&self.nodes[*id]), @@ -262,26 +263,16 @@ impl BindingsBuilder { fn collect_nested(&self, idx: usize, nested_idx: usize, nested: &mut Vec<Bindings>) { let last = &self.nodes[idx]; - let mut nested_refs = Vec::new(); + let mut nested_refs: Vec<&[_]> = Vec::new(); self.nested[nested_idx].iter().for_each(|it| match *it { LinkNode::Node(idx) => nested_refs.push(&self.nodes[idx]), LinkNode::Parent { idx, len } => self.collect_nested_ref(idx, len, &mut nested_refs), }); nested_refs.push(last); - - nested_refs.into_iter().for_each(|iter| { - let mut child_bindings = Bindings::default(); - self.build_inner(&mut child_bindings, iter); - nested.push(child_bindings) - }) + nested.extend(nested_refs.into_iter().map(|iter| self.build_inner(iter))); } - fn collect_nodes_ref<'a>( - &'a self, - id: usize, - len: usize, - nodes: &mut Vec<&'a Rc<BindingKind>>, - ) { + fn collect_nodes_ref<'a>(&'a self, id: usize, len: usize, nodes: &mut Vec<&'a BindingKind>) { self.nodes[id].iter().take(len).for_each(|it| match it { LinkNode::Node(it) => nodes.push(it), LinkNode::Parent { idx, len } => self.collect_nodes_ref(*idx, *len, nodes), @@ -291,7 +282,7 @@ impl BindingsBuilder { fn collect_nodes<'a>( &'a self, link_nodes: &'a [LinkNode<Rc<BindingKind>>], - nodes: &mut Vec<&'a Rc<BindingKind>>, + nodes: &mut Vec<&'a BindingKind>, ) { link_nodes.iter().for_each(|it| match it { LinkNode::Node(it) => nodes.push(it), @@ -386,10 +377,10 @@ fn match_loop_inner<'t>( let op = match item.dot.peek() { None => { // We are at or past the end of the matcher of `item`. - if item.up.is_some() { + if let Some(up) = &item.up { if item.sep_parsed.is_none() { // Get the `up` matcher - let mut new_pos = *item.up.clone().unwrap(); + let mut new_pos = (**up).clone(); new_pos.bindings = bindings_builder.copy(&new_pos.bindings); // Add matches from this repetition to the `matches` of `up` bindings_builder.push_nested(&mut new_pos.bindings, &item.bindings); @@ -402,7 +393,7 @@ fn match_loop_inner<'t>( // Check if we need a separator. // We check the separator one by one - let sep_idx = *item.sep_parsed.as_ref().unwrap_or(&0); + let sep_idx = item.sep_parsed.unwrap_or(0); let sep_len = item.sep.as_ref().map_or(0, Separator::tt_count); if item.sep.is_some() && sep_idx != sep_len { let sep = item.sep.as_ref().unwrap(); diff --git a/src/tools/rust-analyzer/crates/project-model/src/build_scripts.rs b/src/tools/rust-analyzer/crates/project-model/src/build_scripts.rs index 84e772d1684a1..837ea016193cd 100644 --- a/src/tools/rust-analyzer/crates/project-model/src/build_scripts.rs +++ b/src/tools/rust-analyzer/crates/project-model/src/build_scripts.rs @@ -43,10 +43,12 @@ impl WorkspaceBuildScripts { if let Some([program, args @ ..]) = config.run_build_script_command.as_deref() { let mut cmd = Command::new(program); cmd.args(args); + cmd.envs(&config.extra_env); return cmd; } let mut cmd = Command::new(toolchain::cargo()); + cmd.envs(&config.extra_env); cmd.args(&["check", "--quiet", "--workspace", "--message-format=json"]); diff --git a/src/tools/rust-analyzer/crates/project-model/src/cargo_workspace.rs b/src/tools/rust-analyzer/crates/project-model/src/cargo_workspace.rs index eed955b42daae..736d80041bd51 100644 --- a/src/tools/rust-analyzer/crates/project-model/src/cargo_workspace.rs +++ b/src/tools/rust-analyzer/crates/project-model/src/cargo_workspace.rs @@ -2,6 +2,7 @@ use std::iter; use std::path::PathBuf; +use std::str::from_utf8; use std::{ops, process::Command}; use anyhow::{Context, Result}; @@ -98,6 +99,8 @@ pub struct CargoConfig { pub wrap_rustc_in_build_scripts: bool, pub run_build_script_command: Option<Vec<String>>, + + pub extra_env: FxHashMap<String, String>, } impl CargoConfig { @@ -263,8 +266,8 @@ impl CargoWorkspace { let target = config .target .clone() - .or_else(|| cargo_config_build_target(cargo_toml)) - .or_else(|| rustc_discover_host_triple(cargo_toml)); + .or_else(|| cargo_config_build_target(cargo_toml, config)) + .or_else(|| rustc_discover_host_triple(cargo_toml, config)); let mut meta = MetadataCommand::new(); meta.cargo_path(toolchain::cargo()); @@ -292,8 +295,27 @@ impl CargoWorkspace { // unclear whether cargo itself supports it. progress("metadata".to_string()); - let meta = - meta.exec().with_context(|| format!("Failed to run `{:?}`", meta.cargo_command()))?; + fn exec_with_env( + command: &cargo_metadata::MetadataCommand, + extra_env: &FxHashMap<String, String>, + ) -> Result<cargo_metadata::Metadata, cargo_metadata::Error> { + let mut command = command.cargo_command(); + command.envs(extra_env); + let output = command.output()?; + if !output.status.success() { + return Err(cargo_metadata::Error::CargoMetadata { + stderr: String::from_utf8(output.stderr)?, + }); + } + let stdout = from_utf8(&output.stdout)? + .lines() + .find(|line| line.starts_with('{')) + .ok_or(cargo_metadata::Error::NoJson)?; + cargo_metadata::MetadataCommand::parse(stdout) + } + + let meta = exec_with_env(&meta, &config.extra_env) + .with_context(|| format!("Failed to run `{:?}`", meta.cargo_command()))?; Ok(meta) } @@ -463,8 +485,9 @@ impl CargoWorkspace { } } -fn rustc_discover_host_triple(cargo_toml: &ManifestPath) -> Option<String> { +fn rustc_discover_host_triple(cargo_toml: &ManifestPath, config: &CargoConfig) -> Option<String> { let mut rustc = Command::new(toolchain::rustc()); + rustc.envs(&config.extra_env); rustc.current_dir(cargo_toml.parent()).arg("-vV"); tracing::debug!("Discovering host platform by {:?}", rustc); match utf8_stdout(rustc) { @@ -486,8 +509,9 @@ fn rustc_discover_host_triple(cargo_toml: &ManifestPath) -> Option<String> { } } -fn cargo_config_build_target(cargo_toml: &ManifestPath) -> Option<String> { +fn cargo_config_build_target(cargo_toml: &ManifestPath, config: &CargoConfig) -> Option<String> { let mut cargo_config = Command::new(toolchain::cargo()); + cargo_config.envs(&config.extra_env); cargo_config .current_dir(cargo_toml.parent()) .args(&["-Z", "unstable-options", "config", "get", "build.target"]) diff --git a/src/tools/rust-analyzer/crates/project-model/src/rustc_cfg.rs b/src/tools/rust-analyzer/crates/project-model/src/rustc_cfg.rs index 17e244d0649ed..486cb143b80bd 100644 --- a/src/tools/rust-analyzer/crates/project-model/src/rustc_cfg.rs +++ b/src/tools/rust-analyzer/crates/project-model/src/rustc_cfg.rs @@ -4,9 +4,13 @@ use std::process::Command; use anyhow::Result; -use crate::{cfg_flag::CfgFlag, utf8_stdout, ManifestPath}; +use crate::{cfg_flag::CfgFlag, utf8_stdout, CargoConfig, ManifestPath}; -pub(crate) fn get(cargo_toml: Option<&ManifestPath>, target: Option<&str>) -> Vec<CfgFlag> { +pub(crate) fn get( + cargo_toml: Option<&ManifestPath>, + target: Option<&str>, + config: &CargoConfig, +) -> Vec<CfgFlag> { let _p = profile::span("rustc_cfg::get"); let mut res = Vec::with_capacity(6 * 2 + 1); @@ -18,7 +22,7 @@ pub(crate) fn get(cargo_toml: Option<&ManifestPath>, target: Option<&str>) -> Ve } } - match get_rust_cfgs(cargo_toml, target) { + match get_rust_cfgs(cargo_toml, target, config) { Ok(rustc_cfgs) => { tracing::debug!( "rustc cfgs found: {:?}", @@ -35,9 +39,14 @@ pub(crate) fn get(cargo_toml: Option<&ManifestPath>, target: Option<&str>) -> Ve res } -fn get_rust_cfgs(cargo_toml: Option<&ManifestPath>, target: Option<&str>) -> Result<String> { +fn get_rust_cfgs( + cargo_toml: Option<&ManifestPath>, + target: Option<&str>, + config: &CargoConfig, +) -> Result<String> { if let Some(cargo_toml) = cargo_toml { let mut cargo_config = Command::new(toolchain::cargo()); + cargo_config.envs(&config.extra_env); cargo_config .current_dir(cargo_toml.parent()) .args(&["-Z", "unstable-options", "rustc", "--print", "cfg"]) @@ -52,6 +61,7 @@ fn get_rust_cfgs(cargo_toml: Option<&ManifestPath>, target: Option<&str>) -> Res } // using unstable cargo features failed, fall back to using plain rustc let mut cmd = Command::new(toolchain::rustc()); + cmd.envs(&config.extra_env); cmd.args(&["--print", "cfg", "-O"]); if let Some(target) = target { cmd.args(&["--target", target]); diff --git a/src/tools/rust-analyzer/crates/project-model/src/sysroot.rs b/src/tools/rust-analyzer/crates/project-model/src/sysroot.rs index 362bb0f5e79cd..3282719fef3d7 100644 --- a/src/tools/rust-analyzer/crates/project-model/src/sysroot.rs +++ b/src/tools/rust-analyzer/crates/project-model/src/sysroot.rs @@ -10,7 +10,7 @@ use anyhow::{format_err, Result}; use la_arena::{Arena, Idx}; use paths::{AbsPath, AbsPathBuf}; -use crate::{utf8_stdout, ManifestPath}; +use crate::{utf8_stdout, CargoConfig, ManifestPath}; #[derive(Debug, Clone, Eq, PartialEq)] pub struct Sysroot { @@ -67,18 +67,20 @@ impl Sysroot { self.crates.iter().map(|(id, _data)| id) } - pub fn discover(dir: &AbsPath) -> Result<Sysroot> { + pub fn discover(dir: &AbsPath, config: &CargoConfig) -> Result<Sysroot> { tracing::debug!("Discovering sysroot for {}", dir.display()); - let sysroot_dir = discover_sysroot_dir(dir)?; - let sysroot_src_dir = discover_sysroot_src_dir(&sysroot_dir, dir)?; + let sysroot_dir = discover_sysroot_dir(dir, config)?; + let sysroot_src_dir = discover_sysroot_src_dir(&sysroot_dir, dir, config)?; let res = Sysroot::load(sysroot_dir, sysroot_src_dir)?; Ok(res) } - pub fn discover_rustc(cargo_toml: &ManifestPath) -> Option<ManifestPath> { + pub fn discover_rustc(cargo_toml: &ManifestPath, config: &CargoConfig) -> Option<ManifestPath> { tracing::debug!("Discovering rustc source for {}", cargo_toml.display()); let current_dir = cargo_toml.parent(); - discover_sysroot_dir(current_dir).ok().and_then(|sysroot_dir| get_rustc_src(&sysroot_dir)) + discover_sysroot_dir(current_dir, config) + .ok() + .and_then(|sysroot_dir| get_rustc_src(&sysroot_dir)) } pub fn load(sysroot_dir: AbsPathBuf, sysroot_src_dir: AbsPathBuf) -> Result<Sysroot> { @@ -144,8 +146,9 @@ impl Sysroot { } } -fn discover_sysroot_dir(current_dir: &AbsPath) -> Result<AbsPathBuf> { +fn discover_sysroot_dir(current_dir: &AbsPath, config: &CargoConfig) -> Result<AbsPathBuf> { let mut rustc = Command::new(toolchain::rustc()); + rustc.envs(&config.extra_env); rustc.current_dir(current_dir).args(&["--print", "sysroot"]); tracing::debug!("Discovering sysroot by {:?}", rustc); let stdout = utf8_stdout(rustc)?; @@ -155,6 +158,7 @@ fn discover_sysroot_dir(current_dir: &AbsPath) -> Result<AbsPathBuf> { fn discover_sysroot_src_dir( sysroot_path: &AbsPathBuf, current_dir: &AbsPath, + config: &CargoConfig, ) -> Result<AbsPathBuf> { if let Ok(path) = env::var("RUST_SRC_PATH") { let path = AbsPathBuf::try_from(path.as_str()) @@ -170,6 +174,7 @@ fn discover_sysroot_src_dir( get_rust_src(sysroot_path) .or_else(|| { let mut rustup = Command::new(toolchain::rustup()); + rustup.envs(&config.extra_env); rustup.current_dir(current_dir).args(&["component", "add", "rust-src"]); utf8_stdout(rustup).ok()?; get_rust_src(sysroot_path) diff --git a/src/tools/rust-analyzer/crates/project-model/src/tests.rs b/src/tools/rust-analyzer/crates/project-model/src/tests.rs index 9ccb6e9101ef4..bea624bd54195 100644 --- a/src/tools/rust-analyzer/crates/project-model/src/tests.rs +++ b/src/tools/rust-analyzer/crates/project-model/src/tests.rs @@ -10,8 +10,8 @@ use paths::{AbsPath, AbsPathBuf}; use serde::de::DeserializeOwned; use crate::{ - CargoWorkspace, CfgOverrides, ProjectJson, ProjectJsonData, ProjectWorkspace, Sysroot, - WorkspaceBuildScripts, + CargoConfig, CargoWorkspace, CfgOverrides, ProjectJson, ProjectJsonData, ProjectWorkspace, + Sysroot, WorkspaceBuildScripts, }; fn load_cargo(file: &str) -> CrateGraph { @@ -92,13 +92,17 @@ fn rooted_project_json(data: ProjectJsonData) -> ProjectJson { } fn to_crate_graph(project_workspace: ProjectWorkspace) -> CrateGraph { - project_workspace.to_crate_graph(&mut |_, _| Ok(Vec::new()), &mut { - let mut counter = 0; - move |_path| { - counter += 1; - Some(FileId(counter)) - } - }) + project_workspace.to_crate_graph( + &mut |_, _| Ok(Vec::new()), + &mut { + let mut counter = 0; + move |_path| { + counter += 1; + Some(FileId(counter)) + } + }, + &CargoConfig::default(), + ) } fn check_crate_graph(crate_graph: CrateGraph, expect: Expect) { diff --git a/src/tools/rust-analyzer/crates/project-model/src/workspace.rs b/src/tools/rust-analyzer/crates/project-model/src/workspace.rs index 818bbed6af2ec..bc4ab45daeffc 100644 --- a/src/tools/rust-analyzer/crates/project-model/src/workspace.rs +++ b/src/tools/rust-analyzer/crates/project-model/src/workspace.rs @@ -156,11 +156,12 @@ impl ProjectWorkspace { })?; let project_location = project_json.parent().to_path_buf(); let project_json = ProjectJson::new(&project_location, data); - ProjectWorkspace::load_inline(project_json, config.target.as_deref())? + ProjectWorkspace::load_inline(project_json, config.target.as_deref(), config)? } ProjectManifest::CargoToml(cargo_toml) => { let cargo_version = utf8_stdout({ let mut cmd = Command::new(toolchain::cargo()); + cmd.envs(&config.extra_env); cmd.arg("--version"); cmd })?; @@ -186,7 +187,7 @@ impl ProjectWorkspace { let sysroot = if config.no_sysroot { None } else { - Some(Sysroot::discover(cargo_toml.parent()).with_context(|| { + Some(Sysroot::discover(cargo_toml.parent(), config).with_context(|| { format!( "Failed to find sysroot for Cargo.toml file {}. Is rust-src installed?", cargo_toml.display() @@ -196,7 +197,7 @@ impl ProjectWorkspace { let rustc_dir = match &config.rustc_source { Some(RustcSource::Path(path)) => ManifestPath::try_from(path.clone()).ok(), - Some(RustcSource::Discover) => Sysroot::discover_rustc(&cargo_toml), + Some(RustcSource::Discover) => Sysroot::discover_rustc(&cargo_toml, config), None => None, }; @@ -216,7 +217,7 @@ impl ProjectWorkspace { None => None, }; - let rustc_cfg = rustc_cfg::get(Some(&cargo_toml), config.target.as_deref()); + let rustc_cfg = rustc_cfg::get(Some(&cargo_toml), config.target.as_deref(), config); let cfg_overrides = config.cfg_overrides(); ProjectWorkspace::Cargo { @@ -237,6 +238,7 @@ impl ProjectWorkspace { pub fn load_inline( project_json: ProjectJson, target: Option<&str>, + config: &CargoConfig, ) -> Result<ProjectWorkspace> { let sysroot = match (project_json.sysroot.clone(), project_json.sysroot_src.clone()) { (Some(sysroot), Some(sysroot_src)) => Some(Sysroot::load(sysroot, sysroot_src)?), @@ -258,7 +260,7 @@ impl ProjectWorkspace { (None, None) => None, }; - let rustc_cfg = rustc_cfg::get(None, target); + let rustc_cfg = rustc_cfg::get(None, target, config); Ok(ProjectWorkspace::Json { project: project_json, sysroot, rustc_cfg }) } @@ -268,8 +270,9 @@ impl ProjectWorkspace { .first() .and_then(|it| it.parent()) .ok_or_else(|| format_err!("No detached files to load"))?, + &CargoConfig::default(), )?; - let rustc_cfg = rustc_cfg::get(None, None); + let rustc_cfg = rustc_cfg::get(None, None, &CargoConfig::default()); Ok(ProjectWorkspace::DetachedFiles { files: detached_files, sysroot, rustc_cfg }) } @@ -416,6 +419,7 @@ impl ProjectWorkspace { &self, load_proc_macro: &mut dyn FnMut(&str, &AbsPath) -> ProcMacroLoadResult, load: &mut dyn FnMut(&AbsPath) -> Option<FileId>, + config: &CargoConfig, ) -> CrateGraph { let _p = profile::span("ProjectWorkspace::to_crate_graph"); @@ -426,6 +430,7 @@ impl ProjectWorkspace { load, project, sysroot, + config, ), ProjectWorkspace::Cargo { cargo, @@ -464,6 +469,7 @@ fn project_json_to_crate_graph( load: &mut dyn FnMut(&AbsPath) -> Option<FileId>, project: &ProjectJson, sysroot: &Option<Sysroot>, + config: &CargoConfig, ) -> CrateGraph { let mut crate_graph = CrateGraph::default(); let sysroot_deps = sysroot @@ -489,9 +495,9 @@ fn project_json_to_crate_graph( }; let target_cfgs = match krate.target.as_deref() { - Some(target) => { - cfg_cache.entry(target).or_insert_with(|| rustc_cfg::get(None, Some(target))) - } + Some(target) => cfg_cache + .entry(target) + .or_insert_with(|| rustc_cfg::get(None, Some(target), config)), None => &rustc_cfg, }; diff --git a/src/tools/rust-analyzer/crates/rust-analyzer/src/cli/analysis_stats.rs b/src/tools/rust-analyzer/crates/rust-analyzer/src/cli/analysis_stats.rs index f52e1e7512788..80128e43fd3c5 100644 --- a/src/tools/rust-analyzer/crates/rust-analyzer/src/cli/analysis_stats.rs +++ b/src/tools/rust-analyzer/crates/rust-analyzer/src/cli/analysis_stats.rs @@ -80,7 +80,8 @@ impl flags::AnalysisStats { Some(build_scripts_sw.elapsed()) }; - let (host, vfs, _proc_macro) = load_workspace(workspace, &load_cargo_config)?; + let (host, vfs, _proc_macro) = + load_workspace(workspace, &cargo_config, &load_cargo_config)?; let db = host.raw_database(); eprint!("{:<20} {}", "Database loaded:", db_load_sw.elapsed()); eprint!(" (metadata {}", metadata_time); diff --git a/src/tools/rust-analyzer/crates/rust-analyzer/src/cli/load_cargo.rs b/src/tools/rust-analyzer/crates/rust-analyzer/src/cli/load_cargo.rs index 5d1c013c3275b..88953096e2bcd 100644 --- a/src/tools/rust-analyzer/crates/rust-analyzer/src/cli/load_cargo.rs +++ b/src/tools/rust-analyzer/crates/rust-analyzer/src/cli/load_cargo.rs @@ -38,7 +38,7 @@ pub fn load_workspace_at( workspace.set_build_scripts(build_scripts) } - load_workspace(workspace, load_config) + load_workspace(workspace, cargo_config, load_config) } // Note: Since this function is used by external tools that use rust-analyzer as a library @@ -48,6 +48,7 @@ pub fn load_workspace_at( // these tools need access to `ProjectWorkspace`, too, which `load_workspace_at` hides. pub fn load_workspace( ws: ProjectWorkspace, + cargo_config: &CargoConfig, load_config: &LoadCargoConfig, ) -> Result<(AnalysisHost, vfs::Vfs, Option<ProcMacroServer>)> { let (sender, receiver) = unbounded(); @@ -75,6 +76,7 @@ pub fn load_workspace( vfs.set_file_contents(path.clone(), contents); vfs.file_id(&path) }, + cargo_config, ); let project_folders = ProjectFolders::new(&[ws], &[]); diff --git a/src/tools/rust-analyzer/crates/rust-analyzer/src/cli/lsif.rs b/src/tools/rust-analyzer/crates/rust-analyzer/src/cli/lsif.rs index 491c55a04f8c0..79577bf78c8f9 100644 --- a/src/tools/rust-analyzer/crates/rust-analyzer/src/cli/lsif.rs +++ b/src/tools/rust-analyzer/crates/rust-analyzer/src/cli/lsif.rs @@ -299,7 +299,8 @@ impl flags::Lsif { let workspace = ProjectWorkspace::load(manifest, &cargo_config, no_progress)?; - let (host, vfs, _proc_macro) = load_workspace(workspace, &load_cargo_config)?; + let (host, vfs, _proc_macro) = + load_workspace(workspace, &cargo_config, &load_cargo_config)?; let db = host.raw_database(); let analysis = host.analysis(); diff --git a/src/tools/rust-analyzer/crates/rust-analyzer/src/cli/scip.rs b/src/tools/rust-analyzer/crates/rust-analyzer/src/cli/scip.rs index 65cc993c45e71..05c16bb39e351 100644 --- a/src/tools/rust-analyzer/crates/rust-analyzer/src/cli/scip.rs +++ b/src/tools/rust-analyzer/crates/rust-analyzer/src/cli/scip.rs @@ -40,7 +40,7 @@ impl flags::Scip { let workspace = ProjectWorkspace::load(manifest, &cargo_config, no_progress)?; - let (host, vfs, _) = load_workspace(workspace, &load_cargo_config)?; + let (host, vfs, _) = load_workspace(workspace, &cargo_config, &load_cargo_config)?; let db = host.raw_database(); let analysis = host.analysis(); diff --git a/src/tools/rust-analyzer/crates/rust-analyzer/src/config.rs b/src/tools/rust-analyzer/crates/rust-analyzer/src/config.rs index 54dcb42d99c78..9ef79e6f38120 100644 --- a/src/tools/rust-analyzer/crates/rust-analyzer/src/config.rs +++ b/src/tools/rust-analyzer/crates/rust-analyzer/src/config.rs @@ -84,6 +84,9 @@ config_data! { /// Use `RUSTC_WRAPPER=rust-analyzer` when running build scripts to /// avoid checking unnecessary things. cargo_buildScripts_useRustcWrapper: bool = "true", + /// Extra environment variables that will be set when running cargo, rustc + /// or other commands within the workspace. Useful for setting RUSTFLAGS. + cargo_extraEnv: FxHashMap<String, String> = "{}", /// List of features to activate. /// /// Set this to `"all"` to pass `--all-features` to cargo. @@ -105,6 +108,8 @@ config_data! { checkOnSave_enable: bool = "true", /// Extra arguments for `cargo check`. checkOnSave_extraArgs: Vec<String> = "[]", + /// Extra environment variables that will be set when running `cargo check`. + checkOnSave_extraEnv: FxHashMap<String, String> = "{}", /// List of features to activate. Defaults to /// `#rust-analyzer.cargo.features#`. /// @@ -219,7 +224,6 @@ config_data! { files_excludeDirs: Vec<PathBuf> = "[]", /// Controls file watching implementation. files_watcher: FilesWatcherDef = "\"client\"", - /// Enables highlighting of related references while the cursor is on `break`, `loop`, `while`, or `for` keywords. highlightRelated_breakPoints_enable: bool = "true", /// Enables highlighting of all exit points while the cursor is on any `return`, `?`, `fn`, or return type arrow (`->`). @@ -263,6 +267,8 @@ config_data! { imports_group_enable: bool = "true", /// Whether to allow import insertion to merge new imports into single path glob imports like `use std::fmt::*;`. imports_merge_glob: bool = "true", + /// Prefer to unconditionally use imports of the core and alloc crate, over the std crate. + imports_prefer_no_std: bool = "false", /// The path structure for newly inserted paths to use. imports_prefix: ImportPrefixDef = "\"plain\"", @@ -307,6 +313,7 @@ config_data! { /// Join lines unwraps trivial blocks. joinLines_unwrapTrivialBlock: bool = "true", + /// Whether to show `Debug` lens. Only applies when /// `#rust-analyzer.lens.enable#` is set. lens_debug_enable: bool = "true", @@ -318,6 +325,8 @@ config_data! { /// Whether to show `Implementations` lens. Only applies when /// `#rust-analyzer.lens.enable#` is set. lens_implementations_enable: bool = "true", + /// Where to render annotations. + lens_location: AnnotationLocation = "\"above_name\"", /// Whether to show `References` lens for Struct, Enum, and Union. /// Only applies when `#rust-analyzer.lens.enable#` is set. lens_references_adt_enable: bool = "false", @@ -359,6 +368,9 @@ config_data! { /// this is rust-analyzer itself, but we override this in tests). procMacro_server: Option<PathBuf> = "null", + /// Exclude imports from find-all-references. + references_excludeImports: bool = "false", + /// Command to be executed instead of 'cargo' for runnables. runnables_command: Option<String> = "null", /// Additional arguments to be passed to cargo for runnables such as @@ -494,6 +506,25 @@ pub struct LensConfig { pub refs_adt: bool, // for Struct, Enum, Union and Trait pub refs_trait: bool, // for Struct, Enum, Union and Trait pub enum_variant_refs: bool, + + // annotations + pub location: AnnotationLocation, +} + +#[derive(Copy, Clone, Debug, PartialEq, Eq, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum AnnotationLocation { + AboveName, + AboveWholeItem, +} + +impl From<AnnotationLocation> for ide::AnnotationLocation { + fn from(location: AnnotationLocation) -> Self { + match location { + AnnotationLocation::AboveName => ide::AnnotationLocation::AboveName, + AnnotationLocation::AboveWholeItem => ide::AnnotationLocation::AboveWholeItem, + } + } } impl LensConfig { @@ -918,6 +949,7 @@ impl Config { ExprFillDefaultDef::Default => ExprFillDefaultMode::Default, }, insert_use: self.insert_use_config(), + prefer_no_std: self.data.imports_prefer_no_std, } } @@ -929,6 +961,16 @@ impl Config { } } + pub fn extra_env(&self) -> &FxHashMap<String, String> { + &self.data.cargo_extraEnv + } + + pub fn check_on_save_extra_env(&self) -> FxHashMap<String, String> { + let mut extra_env = self.data.cargo_extraEnv.clone(); + extra_env.extend(self.data.checkOnSave_extraEnv.clone()); + extra_env + } + pub fn lru_capacity(&self) -> Option<usize> { self.data.lru_capacity } @@ -998,6 +1040,7 @@ impl Config { unset_test_crates: UnsetTestCrates::Only(self.data.cargo_unsetTest.clone()), wrap_rustc_in_build_scripts: self.data.cargo_buildScripts_useRustcWrapper, run_build_script_command: self.data.cargo_buildScripts_overrideCommand.clone(), + extra_env: self.data.cargo_extraEnv.clone(), } } @@ -1023,7 +1066,11 @@ impl Config { Some(args) if !args.is_empty() => { let mut args = args.clone(); let command = args.remove(0); - FlycheckConfig::CustomCommand { command, args } + FlycheckConfig::CustomCommand { + command, + args, + extra_env: self.check_on_save_extra_env(), + } } Some(_) | None => FlycheckConfig::CargoCommand { command: self.data.checkOnSave_command.clone(), @@ -1051,6 +1098,7 @@ impl Config { CargoFeatures::Listed(it) => it, }, extra_args: self.data.checkOnSave_extraArgs.clone(), + extra_env: self.check_on_save_extra_env(), }, }; Some(flycheck_config) @@ -1133,6 +1181,7 @@ impl Config { CallableCompletionDef::None => None, }, insert_use: self.insert_use_config(), + prefer_no_std: self.data.imports_prefer_no_std, snippet_cap: SnippetCap::new(try_or_def!( self.caps .text_document @@ -1147,6 +1196,10 @@ impl Config { } } + pub fn find_all_refs_exclude_imports(&self) -> bool { + self.data.references_excludeImports + } + pub fn snippet_cap(&self) -> bool { self.experimental("snippetTextEdit") } @@ -1156,6 +1209,7 @@ impl Config { snippet_cap: SnippetCap::new(self.experimental("snippetTextEdit")), allowed: None, insert_use: self.insert_use_config(), + prefer_no_std: self.data.imports_prefer_no_std, } } @@ -1185,6 +1239,7 @@ impl Config { refs_trait: self.data.lens_enable && self.data.lens_references_trait_enable, enum_variant_refs: self.data.lens_enable && self.data.lens_references_enumVariant_enable, + location: self.data.lens_location, } } @@ -1921,6 +1976,14 @@ fn field_props(field: &str, ty: &str, doc: &[&str], default: &str) -> serde_json "Use server-side file watching", ], }, + "AnnotationLocation" => set! { + "type": "string", + "enum": ["above_name", "above_whole_item"], + "enumDescriptions": [ + "Render annotations above the name of the item.", + "Render annotations above the whole item, including documentation comments and attributes." + ], + }, _ => panic!("missing entry for {}: {}", ty, default), } diff --git a/src/tools/rust-analyzer/crates/rust-analyzer/src/handlers.rs b/src/tools/rust-analyzer/crates/rust-analyzer/src/handlers.rs index e79cf3d3fd6e9..8c3ea77d06115 100644 --- a/src/tools/rust-analyzer/crates/rust-analyzer/src/handlers.rs +++ b/src/tools/rust-analyzer/crates/rust-analyzer/src/handlers.rs @@ -10,8 +10,8 @@ use std::{ use anyhow::Context; use ide::{ AnnotationConfig, AssistKind, AssistResolveStrategy, FileId, FilePosition, FileRange, - HoverAction, HoverGotoTypeData, Query, RangeInfo, Runnable, RunnableKind, SingleResolve, - SourceChange, TextEdit, + HoverAction, HoverGotoTypeData, Query, RangeInfo, ReferenceCategory, Runnable, RunnableKind, + SingleResolve, SourceChange, TextEdit, }; use ide_db::SymbolKind; use lsp_server::ErrorCode; @@ -1012,6 +1012,8 @@ pub(crate) fn handle_references( let _p = profile::span("handle_references"); let position = from_proto::file_position(&snap, params.text_document_position)?; + let exclude_imports = snap.config.find_all_refs_exclude_imports(); + let refs = match snap.analysis.find_all_refs(position, None)? { None => return Ok(None), Some(refs) => refs, @@ -1032,7 +1034,11 @@ pub(crate) fn handle_references( refs.references .into_iter() .flat_map(|(file_id, refs)| { - refs.into_iter().map(move |(range, _)| FileRange { file_id, range }) + refs.into_iter() + .filter(|&(_, category)| { + !exclude_imports || category != Some(ReferenceCategory::Import) + }) + .map(move |(range, _)| FileRange { file_id, range }) }) .chain(decl) }) @@ -1234,6 +1240,7 @@ pub(crate) fn handle_code_lens( annotate_references: lens_config.refs_adt, annotate_method_references: lens_config.method_refs, annotate_enum_variant_references: lens_config.enum_variant_refs, + location: lens_config.location.into(), }, file_id, )?; @@ -1283,7 +1290,7 @@ pub(crate) fn handle_document_highlight( .into_iter() .map(|ide::HighlightedRange { range, category }| lsp_types::DocumentHighlight { range: to_proto::range(&line_index, range), - kind: category.map(to_proto::document_highlight_kind), + kind: category.and_then(to_proto::document_highlight_kind), }) .collect(); Ok(Some(res)) @@ -1782,6 +1789,7 @@ fn run_rustfmt( let mut command = match snap.config.rustfmt() { RustfmtConfig::Rustfmt { extra_args, enable_range_formatting } => { let mut cmd = process::Command::new(toolchain::rustfmt()); + cmd.envs(snap.config.extra_env()); cmd.args(extra_args); // try to chdir to the file so we can respect `rustfmt.toml` // FIXME: use `rustfmt --config-path` once @@ -1839,6 +1847,7 @@ fn run_rustfmt( } RustfmtConfig::CustomCommand { command, args } => { let mut cmd = process::Command::new(command); + cmd.envs(snap.config.extra_env()); cmd.args(args); cmd } diff --git a/src/tools/rust-analyzer/crates/rust-analyzer/src/integrated_benchmarks.rs b/src/tools/rust-analyzer/crates/rust-analyzer/src/integrated_benchmarks.rs index e49a98685a7f1..96b1cb6b12713 100644 --- a/src/tools/rust-analyzer/crates/rust-analyzer/src/integrated_benchmarks.rs +++ b/src/tools/rust-analyzer/crates/rust-analyzer/src/integrated_benchmarks.rs @@ -145,6 +145,7 @@ fn integrated_completion_benchmark() { skip_glob_imports: true, }, snippets: Vec::new(), + prefer_no_std: false, }; let position = FilePosition { file_id, offset: TextSize::try_from(completion_offset).unwrap() }; @@ -182,6 +183,7 @@ fn integrated_completion_benchmark() { skip_glob_imports: true, }, snippets: Vec::new(), + prefer_no_std: false, }; let position = FilePosition { file_id, offset: TextSize::try_from(completion_offset).unwrap() }; diff --git a/src/tools/rust-analyzer/crates/rust-analyzer/src/reload.rs b/src/tools/rust-analyzer/crates/rust-analyzer/src/reload.rs index e47f70fff39e0..4cf5de46c485e 100644 --- a/src/tools/rust-analyzer/crates/rust-analyzer/src/reload.rs +++ b/src/tools/rust-analyzer/crates/rust-analyzer/src/reload.rs @@ -143,6 +143,7 @@ impl GlobalState { project_model::ProjectWorkspace::load_inline( it.clone(), cargo_config.target.as_deref(), + &cargo_config, ) } }) @@ -398,7 +399,11 @@ impl GlobalState { dummy_replacements.get(crate_name).map(|v| &**v).unwrap_or_default(), ) }; - crate_graph.extend(ws.to_crate_graph(&mut load_proc_macro, &mut load)); + crate_graph.extend(ws.to_crate_graph( + &mut load_proc_macro, + &mut load, + &self.config.cargo(), + )); } crate_graph }; diff --git a/src/tools/rust-analyzer/crates/rust-analyzer/src/to_proto.rs b/src/tools/rust-analyzer/crates/rust-analyzer/src/to_proto.rs index e083b9d0e3378..1de801e23e5c8 100644 --- a/src/tools/rust-analyzer/crates/rust-analyzer/src/to_proto.rs +++ b/src/tools/rust-analyzer/crates/rust-analyzer/src/to_proto.rs @@ -83,10 +83,11 @@ pub(crate) fn structure_node_kind(kind: StructureNodeKind) -> lsp_types::SymbolK pub(crate) fn document_highlight_kind( category: ReferenceCategory, -) -> lsp_types::DocumentHighlightKind { +) -> Option<lsp_types::DocumentHighlightKind> { match category { - ReferenceCategory::Read => lsp_types::DocumentHighlightKind::READ, - ReferenceCategory::Write => lsp_types::DocumentHighlightKind::WRITE, + ReferenceCategory::Read => Some(lsp_types::DocumentHighlightKind::READ), + ReferenceCategory::Write => Some(lsp_types::DocumentHighlightKind::WRITE), + ReferenceCategory::Import => None, } } diff --git a/src/tools/rust-analyzer/docs/user/generated_config.adoc b/src/tools/rust-analyzer/docs/user/generated_config.adoc index 72b925726479e..996d4c023d7b2 100644 --- a/src/tools/rust-analyzer/docs/user/generated_config.adoc +++ b/src/tools/rust-analyzer/docs/user/generated_config.adoc @@ -46,6 +46,12 @@ cargo check --quiet --workspace --message-format=json --all-targets Use `RUSTC_WRAPPER=rust-analyzer` when running build scripts to avoid checking unnecessary things. -- +[[rust-analyzer.cargo.extraEnv]]rust-analyzer.cargo.extraEnv (default: `{}`):: ++ +-- +Extra environment variables that will be set when running cargo, rustc +or other commands within the workspace. Useful for setting RUSTFLAGS. +-- [[rust-analyzer.cargo.features]]rust-analyzer.cargo.features (default: `[]`):: + -- @@ -93,6 +99,11 @@ Run specified `cargo check` command for diagnostics on save. -- Extra arguments for `cargo check`. -- +[[rust-analyzer.checkOnSave.extraEnv]]rust-analyzer.checkOnSave.extraEnv (default: `{}`):: ++ +-- +Extra environment variables that will be set when running `cargo check`. +-- [[rust-analyzer.checkOnSave.features]]rust-analyzer.checkOnSave.features (default: `null`):: + -- @@ -353,6 +364,11 @@ Group inserted imports by the https://rust-analyzer.github.io/manual.html#auto-i -- Whether to allow import insertion to merge new imports into single path glob imports like `use std::fmt::*;`. -- +[[rust-analyzer.imports.prefer.no.std]]rust-analyzer.imports.prefer.no.std (default: `false`):: ++ +-- +Prefer to unconditionally use imports of the core and alloc crate, over the std crate. +-- [[rust-analyzer.imports.prefix]]rust-analyzer.imports.prefix (default: `"plain"`):: + -- @@ -474,6 +490,11 @@ client doesn't set the corresponding capability. Whether to show `Implementations` lens. Only applies when `#rust-analyzer.lens.enable#` is set. -- +[[rust-analyzer.lens.location]]rust-analyzer.lens.location (default: `"above_name"`):: ++ +-- +Where to render annotations. +-- [[rust-analyzer.lens.references.adt.enable]]rust-analyzer.lens.references.adt.enable (default: `false`):: + -- @@ -546,6 +567,11 @@ This config takes a map of crate names with the exported proc-macro names to ign Internal config, path to proc-macro server executable (typically, this is rust-analyzer itself, but we override this in tests). -- +[[rust-analyzer.references.excludeImports]]rust-analyzer.references.excludeImports (default: `false`):: ++ +-- +Exclude imports from find-all-references. +-- [[rust-analyzer.runnables.command]]rust-analyzer.runnables.command (default: `null`):: + -- diff --git a/src/tools/rust-analyzer/editors/code/package.json b/src/tools/rust-analyzer/editors/code/package.json index 767c5875bf7e7..94b41c049bc30 100644 --- a/src/tools/rust-analyzer/editors/code/package.json +++ b/src/tools/rust-analyzer/editors/code/package.json @@ -206,11 +206,6 @@ "title": "Show RA Version", "category": "rust-analyzer" }, - { - "command": "rust-analyzer.toggleInlayHints", - "title": "Toggle inlay hints", - "category": "rust-analyzer" - }, { "command": "rust-analyzer.openDocs", "title": "Open docs under cursor", @@ -442,6 +437,11 @@ "default": true, "type": "boolean" }, + "rust-analyzer.cargo.extraEnv": { + "markdownDescription": "Extra environment variables that will be set when running cargo, rustc\nor other commands within the workspace. Useful for setting RUSTFLAGS.", + "default": {}, + "type": "object" + }, "rust-analyzer.cargo.features": { "markdownDescription": "List of features to activate.\n\nSet this to `\"all\"` to pass `--all-features` to cargo.", "default": [], @@ -514,6 +514,11 @@ "type": "string" } }, + "rust-analyzer.checkOnSave.extraEnv": { + "markdownDescription": "Extra environment variables that will be set when running `cargo check`.", + "default": {}, + "type": "object" + }, "rust-analyzer.checkOnSave.features": { "markdownDescription": "List of features to activate. Defaults to\n`#rust-analyzer.cargo.features#`.\n\nSet to `\"all\"` to pass `--all-features` to Cargo.", "default": null, @@ -803,6 +808,11 @@ "default": true, "type": "boolean" }, + "rust-analyzer.imports.prefer.no.std": { + "markdownDescription": "Prefer to unconditionally use imports of the core and alloc crate, over the std crate.", + "default": false, + "type": "boolean" + }, "rust-analyzer.imports.prefix": { "markdownDescription": "The path structure for newly inserted paths to use.", "default": "plain", @@ -963,6 +973,19 @@ "default": true, "type": "boolean" }, + "rust-analyzer.lens.location": { + "markdownDescription": "Where to render annotations.", + "default": "above_name", + "type": "string", + "enum": [ + "above_name", + "above_whole_item" + ], + "enumDescriptions": [ + "Render annotations above the name of the item.", + "Render annotations above the whole item, including documentation comments and attributes." + ] + }, "rust-analyzer.lens.references.adt.enable": { "markdownDescription": "Whether to show `References` lens for Struct, Enum, and Union.\nOnly applies when `#rust-analyzer.lens.enable#` is set.", "default": false, @@ -1036,6 +1059,11 @@ "string" ] }, + "rust-analyzer.references.excludeImports": { + "markdownDescription": "Exclude imports from find-all-references.", + "default": false, + "type": "boolean" + }, "rust-analyzer.runnables.command": { "markdownDescription": "Command to be executed instead of 'cargo' for runnables.", "default": null, @@ -1633,10 +1661,6 @@ "command": "rust-analyzer.serverVersion", "when": "inRustProject" }, - { - "command": "rust-analyzer.toggleInlayHints", - "when": "inRustProject" - }, { "command": "rust-analyzer.openDocs", "when": "inRustProject" diff --git a/src/tools/rust-analyzer/editors/code/src/commands.ts b/src/tools/rust-analyzer/editors/code/src/commands.ts index a21b304bbdaa0..b9ad525e361f0 100644 --- a/src/tools/rust-analyzer/editors/code/src/commands.ts +++ b/src/tools/rust-analyzer/editors/code/src/commands.ts @@ -321,30 +321,6 @@ export function serverVersion(ctx: Ctx): Cmd { }; } -export function toggleInlayHints(_ctx: Ctx): Cmd { - return async () => { - const config = vscode.workspace.getConfiguration("editor.inlayHints", { - languageId: "rust", - }); - - const value = config.get("enabled"); - let stringValue; - if (typeof value === "string") { - stringValue = value; - } else { - stringValue = value ? "on" : "off"; - } - const nextValues: Record<string, string> = { - on: "off", - off: "on", - onUnlessPressed: "offUnlessPressed", - offUnlessPressed: "onUnlessPressed", - }; - const nextValue = nextValues[stringValue] ?? "on"; - await config.update("enabled", nextValue, vscode.ConfigurationTarget.Global); - }; -} - // Opens the virtual file that will show the syntax tree // // The contents of the file come from the `TextDocumentContentProvider` diff --git a/src/tools/rust-analyzer/editors/code/src/main.ts b/src/tools/rust-analyzer/editors/code/src/main.ts index e9b62e0cc2578..41bde4195e07d 100644 --- a/src/tools/rust-analyzer/editors/code/src/main.ts +++ b/src/tools/rust-analyzer/editors/code/src/main.ts @@ -180,7 +180,6 @@ async function initCommonContext(context: vscode.ExtensionContext, ctx: Ctx) { ctx.registerCommand("ssr", commands.ssr); ctx.registerCommand("serverVersion", commands.serverVersion); - ctx.registerCommand("toggleInlayHints", commands.toggleInlayHints); // Internal commands which are invoked by the server. ctx.registerCommand("runSingle", commands.runSingle);