diff options
| author | bors <bors@rust-lang.org> | 2023-12-10 06:59:25 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-12-10 06:59:25 +0000 |
| commit | 42dfac5e08b87900dd513124dd0b2540c16f267a (patch) | |
| tree | 5758cebb2d2019825a37c8aba60914c1cfc35535 /compiler/rustc_middle/src | |
| parent | 61afc9c92896a43fce92bd5e3bba6274c5e3e960 (diff) | |
| parent | afa35e90ef4e813f83052e0e66fde893b9a29722 (diff) | |
| download | rust-42dfac5e08b87900dd513124dd0b2540c16f267a.tar.gz rust-42dfac5e08b87900dd513124dd0b2540c16f267a.zip | |
Auto merge of #118788 - compiler-errors:const-pretty, r=fee1-dead
Don't print host effect param in pretty `path_generic_args` Make `own_args_no_defaults` pass back the `GenericParamDef`, so that we can pass both the args *and* param definitions into `path_generic_args`. That allows us to use the `GenericParamDef` to filter out effect params. This allows us to filter out the host param regardless of whether it's `sym::host` or `true`/`false`. This also renames a couple of `const_effect_param` -> `host_effect_param`, and restores `~const` pretty printing to `TraitPredPrintModifiersAndPath`. cc #118785 r? `@fee1-dead` cc `@oli-obk`
Diffstat (limited to 'compiler/rustc_middle/src')
| -rw-r--r-- | compiler/rustc_middle/src/ty/generics.rs | 12 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/print/mod.rs | 5 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/print/pretty.rs | 51 | ||||
| -rw-r--r-- | compiler/rustc_middle/src/ty/util.rs | 11 |
4 files changed, 38 insertions, 41 deletions
diff --git a/compiler/rustc_middle/src/ty/generics.rs b/compiler/rustc_middle/src/ty/generics.rs index 8316f9c3058..12294b0a602 100644 --- a/compiler/rustc_middle/src/ty/generics.rs +++ b/compiler/rustc_middle/src/ty/generics.rs @@ -320,9 +320,11 @@ impl<'tcx> Generics { &'tcx self, tcx: TyCtxt<'tcx>, args: &'tcx [ty::GenericArg<'tcx>], - ) -> &'tcx [ty::GenericArg<'tcx>] { - let mut own_params = self.parent_count..self.count(); + ) -> (&'tcx [ty::GenericArg<'tcx>], &'tcx [ty::GenericParamDef]) { + let mut own_args = self.parent_count..self.count(); + let mut own_params = 0..self.params.len(); if self.has_self && self.parent.is_none() { + own_args.start = 1; own_params.start = 1; } @@ -332,7 +334,7 @@ impl<'tcx> Generics { // of semantic equivalence. While not ideal, that's // good enough for now as this should only be used // for diagnostics anyways. - own_params.end -= self + let num_default_params = self .params .iter() .rev() @@ -342,8 +344,10 @@ impl<'tcx> Generics { }) }) .count(); + own_params.end -= num_default_params; + own_args.end -= num_default_params; - &args[own_params] + (&args[own_args], &self.params[own_params]) } /// Returns the args corresponding to the generic parameters of this item, excluding `Self`. diff --git a/compiler/rustc_middle/src/ty/print/mod.rs b/compiler/rustc_middle/src/ty/print/mod.rs index 5e09154789a..948246c64e2 100644 --- a/compiler/rustc_middle/src/ty/print/mod.rs +++ b/compiler/rustc_middle/src/ty/print/mod.rs @@ -83,6 +83,7 @@ pub trait Printer<'tcx>: Sized { &mut self, print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>, args: &[GenericArg<'tcx>], + params: &[ty::GenericParamDef], ) -> Result<(), PrintError>; // Defaults (should not be overridden): @@ -141,10 +142,12 @@ pub trait Printer<'tcx>: Sized { // on top of the same path, but without its own generics. _ => { if !generics.params.is_empty() && args.len() >= generics.count() { - let args = generics.own_args_no_defaults(self.tcx(), args); + let (args, params) = + generics.own_args_no_defaults(self.tcx(), args); return self.path_generic_args( |cx| cx.print_def_path(def_id, parent_args), args, + params, ); } } diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index bd9f0fd3ea9..fd53111001b 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -19,7 +19,6 @@ use rustc_hir::LangItem; use rustc_session::config::TrimmedDefPaths; use rustc_session::cstore::{ExternCrate, ExternCrateSource}; use rustc_session::Limit; -use rustc_span::sym; use rustc_span::symbol::{kw, Ident, Symbol}; use rustc_span::FileNameDisplayPreference; use rustc_target::abi::Size; @@ -967,7 +966,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { define_scoped_cx!(cx); // Get the (single) generic ty (the args) of this FnOnce trait ref. let generics = tcx.generics_of(trait_ref.def_id); - let own_args = generics.own_args_no_defaults(tcx, trait_ref.args); + let (own_args, _) = generics.own_args_no_defaults(tcx, trait_ref.args); match (entry.return_ty, own_args[0].expect_ty()) { // We can only print `impl Fn() -> ()` if we have a tuple of args and we recorded @@ -1033,7 +1032,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { p!(print(trait_ref.print_only_trait_name())); let generics = tcx.generics_of(trait_ref.def_id); - let own_args = generics.own_args_no_defaults(tcx, trait_ref.args); + let (own_args, _) = generics.own_args_no_defaults(tcx, trait_ref.args); if !own_args.is_empty() || !assoc_items.is_empty() { let mut first = true; @@ -1185,6 +1184,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { ) }, &alias_ty.args[1..], + &self.tcx().generics_of(alias_ty.def_id).params, ) } @@ -1233,7 +1233,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { let dummy_cx = Ty::new_fresh(cx.tcx(), 0); let principal = principal.with_self_ty(cx.tcx(), dummy_cx); - let args = cx + let (args, _) = cx .tcx() .generics_of(principal.def_id) .own_args_no_defaults(cx.tcx(), principal.args); @@ -2031,40 +2031,26 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> { &mut self, print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>, args: &[GenericArg<'tcx>], + params: &[ty::GenericParamDef], ) -> Result<(), PrintError> { print_prefix(self)?; let tcx = self.tcx; - - let args = args.iter().copied(); - - let args: Vec<_> = if !tcx.sess.verbose() { - // skip host param as those are printed as `~const` - args.filter(|arg| match arg.unpack() { - // FIXME(effects) there should be a better way than just matching the name - GenericArgKind::Const(c) - if tcx.features().effects - && matches!( - c.kind(), - ty::ConstKind::Param(ty::ParamConst { name: sym::host, .. }) - ) => - { - false - } - _ => true, - }) - .collect() - } else { + let verbose = tcx.sess.verbose(); + let mut args = args + .iter() + .copied() + .zip(params) // If -Zverbose is passed, we should print the host parameter instead // of eating it. - args.collect() - }; + .filter(|(_, param)| verbose || !param.is_host_effect()) + .peekable(); - if !args.is_empty() { + if args.peek().is_some() { if self.in_value { write!(self, "::")?; } - self.generic_delimiters(|cx| cx.comma_sep(args.into_iter())) + self.generic_delimiters(|cx| cx.comma_sep(args.map(|(arg, _)| arg))) } else { Ok(()) } @@ -2894,11 +2880,15 @@ define_print_and_forward_display! { } TraitPredPrintModifiersAndPath<'tcx> { - // FIXME(effects) print `~const` here + if let Some(idx) = cx.tcx().generics_of(self.0.trait_ref.def_id).host_effect_index + { + if self.0.trait_ref.args.const_at(idx) != cx.tcx().consts.true_ { + p!("~const "); + } + } if let ty::ImplPolarity::Negative = self.0.polarity { p!("!") } - p!(print(self.0.trait_ref.print_only_trait_path())); } @@ -2933,7 +2923,6 @@ define_print_and_forward_display! { p!("~const "); } } - // FIXME(effects) print `~const` here if let ty::ImplPolarity::Negative = self.polarity { p!("!"); } diff --git a/compiler/rustc_middle/src/ty/util.rs b/compiler/rustc_middle/src/ty/util.rs index b7c3edee9e5..1da090a7e46 100644 --- a/compiler/rustc_middle/src/ty/util.rs +++ b/compiler/rustc_middle/src/ty/util.rs @@ -782,7 +782,7 @@ impl<'tcx> TyCtxt<'tcx> { || self.extern_crate(key.as_def_id()).is_some_and(|e| e.is_direct()) } - pub fn expected_const_effect_param_for_body(self, def_id: LocalDefId) -> ty::Const<'tcx> { + pub fn expected_host_effect_param_for_body(self, def_id: LocalDefId) -> ty::Const<'tcx> { // FIXME(effects): This is suspicious and should probably not be done, // especially now that we enforce host effects and then properly handle // effect vars during fallback. @@ -817,7 +817,7 @@ impl<'tcx> TyCtxt<'tcx> { } /// Constructs generic args for an item, optionally appending a const effect param type - pub fn with_opt_const_effect_param( + pub fn with_opt_host_effect_param( self, caller_def_id: LocalDefId, callee_def_id: DefId, @@ -826,9 +826,10 @@ impl<'tcx> TyCtxt<'tcx> { let generics = self.generics_of(callee_def_id); assert_eq!(generics.parent, None); - let opt_const_param = generics.host_effect_index.is_some().then(|| { - ty::GenericArg::from(self.expected_const_effect_param_for_body(caller_def_id)) - }); + let opt_const_param = generics + .host_effect_index + .is_some() + .then(|| ty::GenericArg::from(self.expected_host_effect_param_for_body(caller_def_id))); self.mk_args_from_iter(args.into_iter().map(|arg| arg.into()).chain(opt_const_param)) } |
