summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2020-03-27 00:25:26 +0000
committerbors <bors@rust-lang.org>2020-03-27 00:25:26 +0000
commit7b73d14b0b35e7b4f79f2d71dc1bbbab31698288 (patch)
tree274c58c487448e0f043ca91acd85c82d1a1164b2
parentAuto merge of #70441 - Dylan-DPC:rollup-qv7h2ph, r=Dylan-DPC (diff)
parentRollup merge of #70435 - Alexendoo:test-66706, r=Centril (diff)
downloadrust-7b73d14b0b35e7b4f79f2d71dc1bbbab31698288.tar.gz
rust-7b73d14b0b35e7b4f79f2d71dc1bbbab31698288.tar.bz2
rust-7b73d14b0b35e7b4f79f2d71dc1bbbab31698288.tar.xz
Auto merge of #70451 - Dylan-DPC:rollup-2g9oyht, r=Dylan-DPC
Rollup of 5 pull requests Successful merges: - #69644 (Remove framework in `dataflow/mod.rs` in favor of "generic" one) - #69936 (Fix cycle error when emitting suggestion for mismatched `fn` type) - #70048 (Allow obtaining &mut OsStr) - #70344 (Decouple `rustc_hir::print` into `rustc_hir_pretty`) - #70435 (Add regression test for #66706) Failed merges: r? @ghost
-rw-r--r--Cargo.lock17
-rw-r--r--src/librustc/hir/map/mod.rs65
-rw-r--r--src/librustc_driver/Cargo.toml1
-rw-r--r--src/librustc_driver/pretty.rs9
-rw-r--r--src/librustc_hir/Cargo.toml2
-rw-r--r--src/librustc_hir/hir.rs98
-rw-r--r--src/librustc_hir/intravisit.rs5
-rw-r--r--src/librustc_hir/lib.rs2
-rw-r--r--src/librustc_hir_pretty/Cargo.toml18
-rw-r--r--src/librustc_hir_pretty/lib.rs (renamed from src/librustc_hir/print.rs)56
-rw-r--r--src/librustc_metadata/Cargo.toml1
-rw-r--r--src/librustc_metadata/rmeta/encoder.rs13
-rw-r--r--src/librustc_mir/borrow_check/mod.rs6
-rw-r--r--src/librustc_mir/borrow_check/nll.rs2
-rw-r--r--src/librustc_mir/borrow_check/type_check/liveness/mod.rs2
-rw-r--r--src/librustc_mir/borrow_check/type_check/liveness/trace.rs2
-rw-r--r--src/librustc_mir/borrow_check/type_check/mod.rs2
-rw-r--r--src/librustc_mir/dataflow/at_location.rs169
-rw-r--r--src/librustc_mir/dataflow/framework/cursor.rs (renamed from src/librustc_mir/dataflow/generic/cursor.rs)0
-rw-r--r--src/librustc_mir/dataflow/framework/engine.rs (renamed from src/librustc_mir/dataflow/generic/engine.rs)0
-rw-r--r--src/librustc_mir/dataflow/framework/graphviz.rs (renamed from src/librustc_mir/dataflow/generic/graphviz.rs)0
-rw-r--r--src/librustc_mir/dataflow/framework/mod.rs (renamed from src/librustc_mir/dataflow/generic/mod.rs)75
-rw-r--r--src/librustc_mir/dataflow/framework/tests.rs (renamed from src/librustc_mir/dataflow/generic/tests.rs)0
-rw-r--r--src/librustc_mir/dataflow/framework/visitor.rs (renamed from src/librustc_mir/dataflow/generic/visitor.rs)0
-rw-r--r--src/librustc_mir/dataflow/impls/borrowed_locals.rs2
-rw-r--r--src/librustc_mir/dataflow/impls/borrows.rs6
-rw-r--r--src/librustc_mir/dataflow/impls/mod.rs3
-rw-r--r--src/librustc_mir/dataflow/impls/storage_liveness.rs2
-rw-r--r--src/librustc_mir/dataflow/mod.rs914
-rw-r--r--src/librustc_mir/transform/check_consts/resolver.rs4
-rw-r--r--src/librustc_mir/transform/check_consts/validation.rs2
-rw-r--r--src/librustc_mir/transform/elaborate_drops.rs2
-rw-r--r--src/librustc_mir/transform/generator.rs2
-rw-r--r--src/librustc_mir/transform/rustc_peek.rs2
-rw-r--r--src/librustc_passes/liveness.rs18
-rw-r--r--src/librustc_privacy/lib.rs16
-rw-r--r--src/librustc_save_analysis/Cargo.toml5
-rw-r--r--src/librustc_save_analysis/lib.rs7
-rw-r--r--src/librustc_trait_selection/traits/error_reporting/mod.rs3
-rw-r--r--src/librustc_trait_selection/traits/object_safety.rs45
-rw-r--r--src/librustc_typeck/astconv.rs15
-rw-r--r--src/librustc_typeck/check/_match.rs9
-rw-r--r--src/librustc_typeck/check/callee.rs22
-rw-r--r--src/librustc_typeck/check/coercion.rs9
-rw-r--r--src/librustc_typeck/check/demand.rs19
-rw-r--r--src/librustc_typeck/check/expr.rs51
-rw-r--r--src/librustc_typeck/check/method/suggest.rs2
-rw-r--r--src/librustc_typeck/check/mod.rs6
-rw-r--r--src/librustc_typeck/check/op.rs26
-rw-r--r--src/librustc_typeck/check/pat.rs37
-rw-r--r--src/librustc_typeck/check_unused.rs14
-rw-r--r--src/librustc_typeck/collect.rs9
-rw-r--r--src/librustdoc/clean/inline.rs4
-rw-r--r--src/librustdoc/clean/utils.rs2
-rw-r--r--src/librustdoc/lib.rs1
-rw-r--r--src/librustdoc/test.rs2
-rw-r--r--src/libstd/ffi/os_str.rs27
-rw-r--r--src/libstd/sys/windows/os_str.rs12
-rw-r--r--src/libstd/sys_common/os_str_bytes.rs11
-rw-r--r--src/test/ui/issues/issue-66667-function-cmp-cycle.rs16
-rw-r--r--src/test/ui/issues/issue-66667-function-cmp-cycle.stderr55
-rw-r--r--src/test/ui/issues/issue-66706.rs13
-rw-r--r--src/test/ui/issues/issue-66706.stderr32
-rw-r--r--src/test/ui/methods/method-path-in-pattern.stderr6
-rw-r--r--src/test/ui/privacy/associated-item-privacy-trait.rs6
-rw-r--r--src/test/ui/privacy/associated-item-privacy-trait.stderr4
-rw-r--r--src/test/ui/qualified/qualified-path-params.stderr2
67 files changed, 547 insertions, 1443 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 8bf6198..f1fa913 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3668,6 +3668,7 @@ dependencies = [
3668 "rustc_errors", 3668 "rustc_errors",
3669 "rustc_feature", 3669 "rustc_feature",
3670 "rustc_hir", 3670 "rustc_hir",
3671 "rustc_hir_pretty",
3671 "rustc_interface", 3672 "rustc_interface",
3672 "rustc_lint", 3673 "rustc_lint",
3673 "rustc_metadata", 3674 "rustc_metadata",
@@ -3742,9 +3743,7 @@ dependencies = [
3742 "lazy_static 1.4.0", 3743 "lazy_static 1.4.0",
3743 "log", 3744 "log",
3744 "rustc_ast", 3745 "rustc_ast",
3745 "rustc_ast_pretty",
3746 "rustc_data_structures", 3746 "rustc_data_structures",
3747 "rustc_errors",
3748 "rustc_index", 3747 "rustc_index",
3749 "rustc_macros", 3748 "rustc_macros",
3750 "rustc_span", 3749 "rustc_span",
@@ -3754,6 +3753,18 @@ dependencies = [
3754] 3753]
3755 3754
3756[[package]] 3755[[package]]
3756name = "rustc_hir_pretty"
3757version = "0.0.0"
3758dependencies = [
3759 "rustc_ast",
3760 "rustc_ast_pretty",
3761 "rustc_data_structures",
3762 "rustc_hir",
3763 "rustc_span",
3764 "rustc_target",
3765]
3766
3767[[package]]
3757name = "rustc_incremental" 3768name = "rustc_incremental"
3758version = "0.0.0" 3769version = "0.0.0"
3759dependencies = [ 3770dependencies = [
@@ -3903,6 +3914,7 @@ dependencies = [
3903 "rustc_errors", 3914 "rustc_errors",
3904 "rustc_expand", 3915 "rustc_expand",
3905 "rustc_hir", 3916 "rustc_hir",
3917 "rustc_hir_pretty",
3906 "rustc_index", 3918 "rustc_index",
3907 "rustc_session", 3919 "rustc_session",
3908 "rustc_span", 3920 "rustc_span",
@@ -4087,6 +4099,7 @@ dependencies = [
4087 "rustc_ast_pretty", 4099 "rustc_ast_pretty",
4088 "rustc_data_structures", 4100 "rustc_data_structures",
4089 "rustc_hir", 4101 "rustc_hir",
4102 "rustc_hir_pretty",
4090 "rustc_parse", 4103 "rustc_parse",
4091 "rustc_session", 4104 "rustc_session",
4092 "rustc_span", 4105 "rustc_span",
diff --git a/src/librustc/hir/map/mod.rs b/src/librustc/hir/map/mod.rs
index 6290f5b..bc42ac1 100644
--- a/src/librustc/hir/map/mod.rs
+++ b/src/librustc/hir/map/mod.rs
@@ -10,7 +10,6 @@ use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
10use rustc_hir::definitions::{DefKey, DefPath, Definitions}; 10use rustc_hir::definitions::{DefKey, DefPath, Definitions};
11use rustc_hir::intravisit; 11use rustc_hir::intravisit;
12use rustc_hir::itemlikevisit::ItemLikeVisitor; 12use rustc_hir::itemlikevisit::ItemLikeVisitor;
13use rustc_hir::print::Nested;
14use rustc_hir::*; 13use rustc_hir::*;
15use rustc_index::vec::IndexVec; 14use rustc_index::vec::IndexVec;
16use rustc_span::hygiene::MacroKind; 15use rustc_span::hygiene::MacroKind;
@@ -890,20 +889,18 @@ impl<'hir> Map<'hir> {
890 } 889 }
891 } 890 }
892 891
892 /// Get a representation of this `id` for debugging purposes.
893 /// NOTE: Do NOT use this in diagnostics!
893 pub fn node_to_string(&self, id: HirId) -> String { 894 pub fn node_to_string(&self, id: HirId) -> String {
894 hir_id_to_string(self, id, true) 895 hir_id_to_string(self, id)
895 }
896
897 pub fn hir_to_user_string(&self, id: HirId) -> String {
898 hir_id_to_string(self, id, false)
899 }
900
901 pub fn hir_to_pretty_string(&self, id: HirId) -> String {
902 print::to_string(self, |s| s.print_node(self.get(id)))
903 } 896 }
904} 897}
905 898
906impl<'hir> intravisit::Map<'hir> for Map<'hir> { 899impl<'hir> intravisit::Map<'hir> for Map<'hir> {
900 fn find(&self, hir_id: HirId) -> Option<Node<'hir>> {
901 self.find(hir_id)
902 }
903
907 fn body(&self, id: BodyId) -> &'hir Body<'hir> { 904 fn body(&self, id: BodyId) -> &'hir Body<'hir> {
908 self.body(id) 905 self.body(id)
909 } 906 }
@@ -982,23 +979,8 @@ pub(super) fn index_hir<'tcx>(tcx: TyCtxt<'tcx>, cnum: CrateNum) -> &'tcx Indexe
982 tcx.arena.alloc(IndexedHir { crate_hash, map }) 979 tcx.arena.alloc(IndexedHir { crate_hash, map })
983} 980}
984 981
985/// Identical to the `PpAnn` implementation for `hir::Crate`, 982fn hir_id_to_string(map: &Map<'_>, id: HirId) -> String {
986/// except it avoids creating a dependency on the whole crate.
987impl<'hir> print::PpAnn for Map<'hir> {
988 fn nested(&self, state: &mut print::State<'_>, nested: print::Nested) {
989 match nested {
990 Nested::Item(id) => state.print_item(self.expect_item(id.id)),
991 Nested::TraitItem(id) => state.print_trait_item(self.trait_item(id)),
992 Nested::ImplItem(id) => state.print_impl_item(self.impl_item(id)),
993 Nested::Body(id) => state.print_expr(&self.body(id).value),
994 Nested::BodyParamPat(id, i) => state.print_pat(&self.body(id).params[i].pat),
995 }
996 }
997}
998
999fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String {
1000 let id_str = format!(" (hir_id={})", id); 983 let id_str = format!(" (hir_id={})", id);
1001 let id_str = if include_id { &id_str[..] } else { "" };
1002 984
1003 let path_str = || { 985 let path_str = || {
1004 // This functionality is used for debugging, try to use `TyCtxt` to get 986 // This functionality is used for debugging, try to use `TyCtxt` to get
@@ -1019,6 +1001,9 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String {
1019 }) 1001 })
1020 }; 1002 };
1021 1003
1004 let span_str = || map.tcx.sess.source_map().span_to_snippet(map.span(id)).unwrap_or_default();
1005 let node_str = |prefix| format!("{} {}{}", prefix, span_str(), id_str);
1006
1022 match map.find(id) { 1007 match map.find(id) {
1023 Some(Node::Item(item)) => { 1008 Some(Node::Item(item)) => {
1024 let item_str = match item.kind { 1009 let item_str = match item.kind {
@@ -1069,22 +1054,20 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String {
1069 Some(Node::Field(ref field)) => { 1054 Some(Node::Field(ref field)) => {
1070 format!("field {} in {}{}", field.ident, path_str(), id_str) 1055 format!("field {} in {}{}", field.ident, path_str(), id_str)
1071 } 1056 }
1072 Some(Node::AnonConst(_)) => format!("const {}{}", map.hir_to_pretty_string(id), id_str), 1057 Some(Node::AnonConst(_)) => node_str("const"),
1073 Some(Node::Expr(_)) => format!("expr {}{}", map.hir_to_pretty_string(id), id_str), 1058 Some(Node::Expr(_)) => node_str("expr"),
1074 Some(Node::Stmt(_)) => format!("stmt {}{}", map.hir_to_pretty_string(id), id_str), 1059 Some(Node::Stmt(_)) => node_str("stmt"),
1075 Some(Node::PathSegment(_)) => { 1060 Some(Node::PathSegment(_)) => node_str("path segment"),
1076 format!("path segment {}{}", map.hir_to_pretty_string(id), id_str) 1061 Some(Node::Ty(_)) => node_str("type"),
1077 } 1062 Some(Node::TraitRef(_)) => node_str("trait ref"),
1078 Some(Node::Ty(_)) => format!("type {}{}", map.hir_to_pretty_string(id), id_str), 1063 Some(Node::Binding(_)) => node_str("local"),
1079 Some(Node::TraitRef(_)) => format!("trait_ref {}{}", map.hir_to_pretty_string(id), id_str), 1064 Some(Node::Pat(_)) => node_str("pat"),
1080 Some(Node::Binding(_)) => format!("local {}{}", map.hir_to_pretty_string(id), id_str), 1065 Some(Node::Param(_)) => node_str("param"),
1081 Some(Node::Pat(_)) => format!("pat {}{}", map.hir_to_pretty_string(id), id_str), 1066 Some(Node::Arm(_)) => node_str("arm"),
1082 Some(Node::Param(_)) => format!("param {}{}", map.hir_to_pretty_string(id), id_str), 1067 Some(Node::Block(_)) => node_str("block"),
1083 Some(Node::Arm(_)) => format!("arm {}{}", map.hir_to_pretty_string(id), id_str), 1068 Some(Node::Local(_)) => node_str("local"),
1084 Some(Node::Block(_)) => format!("block {}{}", map.hir_to_pretty_string(id), id_str),
1085 Some(Node::Local(_)) => format!("local {}{}", map.hir_to_pretty_string(id), id_str),
1086 Some(Node::Ctor(..)) => format!("ctor {}{}", path_str(), id_str), 1069 Some(Node::Ctor(..)) => format!("ctor {}{}", path_str(), id_str),
1087 Some(Node::Lifetime(_)) => format!("lifetime {}{}", map.hir_to_pretty_string(id), id_str), 1070 Some(Node::Lifetime(_)) => node_str("lifetime"),
1088 Some(Node::GenericParam(ref param)) => format!("generic_param {:?}{}", param, id_str), 1071 Some(Node::GenericParam(ref param)) => format!("generic_param {:?}{}", param, id_str),
1089 Some(Node::Visibility(ref vis)) => format!("visibility {:?}{}", vis, id_str), 1072 Some(Node::Visibility(ref vis)) => format!("visibility {:?}{}", vis, id_str),
1090 Some(Node::MacroDef(_)) => format!("macro {}{}", path_str(), id_str), 1073 Some(Node::MacroDef(_)) => format!("macro {}{}", path_str(), id_str),
diff --git a/src/librustc_driver/Cargo.toml b/src/librustc_driver/Cargo.toml
index aec10ee..3ca39b2 100644
--- a/src/librustc_driver/Cargo.toml
+++ b/src/librustc_driver/Cargo.toml
@@ -21,6 +21,7 @@ rustc_data_structures = { path = "../librustc_data_structures" }
21rustc_errors = { path = "../librustc_errors" } 21rustc_errors = { path = "../librustc_errors" }
22rustc_feature = { path = "../librustc_feature" } 22rustc_feature = { path = "../librustc_feature" }
23rustc_hir = { path = "../librustc_hir" } 23rustc_hir = { path = "../librustc_hir" }
24rustc_hir_pretty = { path = "../librustc_hir_pretty" }
24rustc_metadata = { path = "../librustc_metadata" } 25rustc_metadata = { path = "../librustc_metadata" }
25rustc_mir = { path = "../librustc_mir" } 26rustc_mir = { path = "../librustc_mir" }
26rustc_parse = { path = "../librustc_parse" } 27rustc_parse = { path = "../librustc_parse" }
diff --git a/src/librustc_driver/pretty.rs b/src/librustc_driver/pretty.rs
index 1e5cc55..a57a70e 100644
--- a/src/librustc_driver/pretty.rs
+++ b/src/librustc_driver/pretty.rs
@@ -7,7 +7,7 @@ use rustc_ast::ast;
7use rustc_ast_pretty::pprust; 7use rustc_ast_pretty::pprust;
8use rustc_hir as hir; 8use rustc_hir as hir;
9use rustc_hir::def_id::LOCAL_CRATE; 9use rustc_hir::def_id::LOCAL_CRATE;
10use rustc_hir::print as pprust_hir; 10use rustc_hir_pretty as pprust_hir;
11use rustc_mir::util::{write_mir_graphviz, write_mir_pretty}; 11use rustc_mir::util::{write_mir_graphviz, write_mir_pretty};
12use rustc_session::config::{Input, PpMode, PpSourceMode}; 12use rustc_session::config::{Input, PpMode, PpSourceMode};
13use rustc_session::Session; 13use rustc_session::Session;
@@ -155,7 +155,7 @@ impl<'hir> pprust::PpAnn for NoAnn<'hir> {}
155impl<'hir> pprust_hir::PpAnn for NoAnn<'hir> { 155impl<'hir> pprust_hir::PpAnn for NoAnn<'hir> {
156 fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) { 156 fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
157 if let Some(tcx) = self.tcx { 157 if let Some(tcx) = self.tcx {
158 pprust_hir::PpAnn::nested(&tcx.hir(), state, nested) 158 pprust_hir::PpAnn::nested(&(&tcx.hir() as &dyn hir::intravisit::Map<'_>), state, nested)
159 } 159 }
160 } 160 }
161} 161}
@@ -228,7 +228,7 @@ impl<'hir> HirPrinterSupport<'hir> for IdentifiedAnnotation<'hir> {
228impl<'hir> pprust_hir::PpAnn for IdentifiedAnnotation<'hir> { 228impl<'hir> pprust_hir::PpAnn for IdentifiedAnnotation<'hir> {
229 fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) { 229 fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
230 if let Some(ref tcx) = self.tcx { 230 if let Some(ref tcx) = self.tcx {
231 pprust_hir::PpAnn::nested(&tcx.hir(), state, nested) 231 pprust_hir::PpAnn::nested(&(&tcx.hir() as &dyn hir::intravisit::Map<'_>), state, nested)
232 } 232 }
233 } 233 }
234 fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) { 234 fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
@@ -334,7 +334,8 @@ impl<'a, 'tcx> pprust_hir::PpAnn for TypedAnnotation<'a, 'tcx> {
334 if let pprust_hir::Nested::Body(id) = nested { 334 if let pprust_hir::Nested::Body(id) = nested {
335 self.tables.set(self.tcx.body_tables(id)); 335 self.tables.set(self.tcx.body_tables(id));
336 } 336 }
337 pprust_hir::PpAnn::nested(&self.tcx.hir(), state, nested); 337 let pp_ann = &(&self.tcx.hir() as &dyn hir::intravisit::Map<'_>);
338 pprust_hir::PpAnn::nested(pp_ann, state, nested);
338 self.tables.set(old_tables); 339 self.tables.set(old_tables);
339 } 340 }
340 fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) { 341 fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
diff --git a/src/librustc_hir/Cargo.toml b/src/librustc_hir/Cargo.toml
index b3682ea..811440f 100644
--- a/src/librustc_hir/Cargo.toml
+++ b/src/librustc_hir/Cargo.toml
@@ -10,13 +10,11 @@ path = "lib.rs"
10doctest = false 10doctest = false
11 11
12[dependencies] 12[dependencies]
13rustc_ast_pretty = { path = "../librustc_ast_pretty" }
14rustc_target = { path = "../librustc_target" } 13rustc_target = { path = "../librustc_target" }
15rustc_macros = { path = "../librustc_macros" } 14rustc_macros = { path = "../librustc_macros" }
16rustc_data_structures = { path = "../librustc_data_structures" } 15rustc_data_structures = { path = "../librustc_data_structures" }
17rustc_index = { path = "../librustc_index" } 16rustc_index = { path = "../librustc_index" }
18rustc_span = { path = "../librustc_span" } 17rustc_span = { path = "../librustc_span" }
19rustc_errors = { path = "../librustc_errors" }
20rustc_serialize = { path = "../libserialize", package = "serialize" } 18rustc_serialize = { path = "../libserialize", package = "serialize" }
21rustc_ast = { path = "../librustc_ast" } 19rustc_ast = { path = "../librustc_ast" }
22lazy_static = "1" 20lazy_static = "1"
diff --git a/src/librustc_hir/hir.rs b/src/librustc_hir/hir.rs
index bb864ed..2054759 100644
--- a/src/librustc_hir/hir.rs
+++ b/src/librustc_hir/hir.rs
@@ -2,11 +2,6 @@ use crate::def::{DefKind, Namespace, Res};
2use crate::def_id::DefId; 2use crate::def_id::DefId;
3crate use crate::hir_id::HirId; 3crate use crate::hir_id::HirId;
4use crate::itemlikevisit; 4use crate::itemlikevisit;
5use crate::print;
6
7crate use BlockCheckMode::*;
8crate use FnRetTy::*;
9crate use UnsafeSource::*;
10 5
11use rustc_ast::ast::{self, AsmDialect, CrateSugar, Ident, Name}; 6use rustc_ast::ast::{self, AsmDialect, CrateSugar, Ident, Name};
12use rustc_ast::ast::{AttrVec, Attribute, FloatTy, IntTy, Label, LitKind, StrStyle, UintTy}; 7use rustc_ast::ast::{AttrVec, Attribute, FloatTy, IntTy, Label, LitKind, StrStyle, UintTy};
@@ -16,7 +11,6 @@ use rustc_ast::node_id::NodeMap;
16use rustc_ast::util::parser::ExprPrecedence; 11use rustc_ast::util::parser::ExprPrecedence;
17use rustc_data_structures::fx::FxHashSet; 12use rustc_data_structures::fx::FxHashSet;
18use rustc_data_structures::sync::{par_for_each_in, Send, Sync}; 13use rustc_data_structures::sync::{par_for_each_in, Send, Sync};
19use rustc_errors::FatalError;
20use rustc_macros::HashStable_Generic; 14use rustc_macros::HashStable_Generic;
21use rustc_span::source_map::{SourceMap, Spanned}; 15use rustc_span::source_map::{SourceMap, Spanned};
22use rustc_span::symbol::{kw, sym, Symbol}; 16use rustc_span::symbol::{kw, sym, Symbol};
@@ -169,12 +163,7 @@ impl fmt::Display for Lifetime {
169 163
170impl fmt::Debug for Lifetime { 164impl fmt::Debug for Lifetime {
171 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 165 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
172 write!( 166 write!(f, "lifetime({}: {})", self.hir_id, self.name.ident())
173 f,
174 "lifetime({}: {})",
175 self.hir_id,
176 print::to_string(print::NO_ANN, |s| s.print_lifetime(self))
177 )
178 } 167 }
179} 168}
180 169
@@ -191,7 +180,7 @@ impl Lifetime {
191/// A `Path` is essentially Rust's notion of a name; for instance, 180/// A `Path` is essentially Rust's notion of a name; for instance,
192/// `std::cmp::PartialEq`. It's represented as a sequence of identifiers, 181/// `std::cmp::PartialEq`. It's represented as a sequence of identifiers,
193/// along with a bunch of supporting information. 182/// along with a bunch of supporting information.
194#[derive(RustcEncodable, RustcDecodable, HashStable_Generic)] 183#[derive(RustcEncodable, RustcDecodable, Debug, HashStable_Generic)]
195pub struct Path<'hir> { 184pub struct Path<'hir> {
196 pub span: Span, 185 pub span: Span,
197 /// The resolution for the path. 186 /// The resolution for the path.
@@ -206,18 +195,6 @@ impl Path<'_> {
206 } 195 }
207} 196}
208 197
209impl fmt::Debug for Path<'_> {
210 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
211 write!(f, "path({})", self)
212 }
213}
214
215impl fmt::Display for Path<'_> {
216 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
217 write!(f, "{}", print::to_string(print::NO_ANN, |s| s.print_path(self, false)))
218 }
219}
220
221/// A segment of a path: an identifier, an optional lifetime, and a set of 198/// A segment of a path: an identifier, an optional lifetime, and a set of
222/// types. 199/// types.
223#[derive(RustcEncodable, RustcDecodable, Debug, HashStable_Generic)] 200#[derive(RustcEncodable, RustcDecodable, Debug, HashStable_Generic)]
@@ -388,9 +365,9 @@ pub enum GenericBound<'hir> {
388} 365}
389 366
390impl GenericBound<'_> { 367impl GenericBound<'_> {
391 pub fn trait_def_id(&self) -> Option<DefId> { 368 pub fn trait_ref(&self) -> Option<&TraitRef<'_>> {
392 match self { 369 match self {
393 GenericBound::Trait(data, _) => Some(data.trait_ref.trait_def_id()), 370 GenericBound::Trait(data, _) => Some(&data.trait_ref),
394 _ => None, 371 _ => None,
395 } 372 }
396 } 373 }
@@ -758,7 +735,7 @@ pub struct Block<'hir> {
758 pub targeted_by_break: bool, 735 pub targeted_by_break: bool,
759} 736}
760 737
761#[derive(RustcEncodable, RustcDecodable, HashStable_Generic)] 738#[derive(Debug, RustcEncodable, RustcDecodable, HashStable_Generic)]
762pub struct Pat<'hir> { 739pub struct Pat<'hir> {
763 #[stable_hasher(ignore)] 740 #[stable_hasher(ignore)]
764 pub hir_id: HirId, 741 pub hir_id: HirId,
@@ -766,17 +743,6 @@ pub struct Pat<'hir> {
766 pub span: Span, 743 pub span: Span,
767} 744}
768 745
769impl fmt::Debug for Pat<'_> {
770 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
771 write!(
772 f,
773 "pat({}: {})",
774 self.hir_id,
775 print::to_string(print::NO_ANN, |s| s.print_pat(self))
776 )
777 }
778}
779
780impl Pat<'_> { 746impl Pat<'_> {
781 // FIXME(#19596) this is a workaround, but there should be a better way 747 // FIXME(#19596) this is a workaround, but there should be a better way
782 fn walk_short_(&self, it: &mut impl FnMut(&Pat<'_>) -> bool) -> bool { 748 fn walk_short_(&self, it: &mut impl FnMut(&Pat<'_>) -> bool) -> bool {
@@ -1118,26 +1084,15 @@ impl UnOp {
1118} 1084}
1119 1085
1120/// A statement. 1086/// A statement.
1121#[derive(RustcEncodable, RustcDecodable, HashStable_Generic)] 1087#[derive(RustcEncodable, RustcDecodable, Debug, HashStable_Generic)]
1122pub struct Stmt<'hir> { 1088pub struct Stmt<'hir> {
1123 pub hir_id: HirId, 1089 pub hir_id: HirId,
1124 pub kind: StmtKind<'hir>, 1090 pub kind: StmtKind<'hir>,
1125 pub span: Span, 1091 pub span: Span,
1126} 1092}
1127 1093
1128impl fmt::Debug for Stmt<'_> {
1129 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1130 write!(
1131 f,
1132 "stmt({}: {})",
1133 self.hir_id,
1134 print::to_string(print::NO_ANN, |s| s.print_stmt(self))
1135 )
1136 }
1137}
1138
1139/// The contents of a statement. 1094/// The contents of a statement.
1140#[derive(RustcEncodable, RustcDecodable, HashStable_Generic)] 1095#[derive(RustcEncodable, RustcDecodable, Debug, HashStable_Generic)]
1141pub enum StmtKind<'hir> { 1096pub enum StmtKind<'hir> {
1142 /// A local (`let`) binding. 1097 /// A local (`let`) binding.
1143 Local(&'hir Local<'hir>), 1098 Local(&'hir Local<'hir>),
@@ -1351,7 +1306,7 @@ pub struct AnonConst {
1351} 1306}
1352 1307
1353/// An expression. 1308/// An expression.
1354#[derive(RustcEncodable, RustcDecodable)] 1309#[derive(Debug, RustcEncodable, RustcDecodable)]
1355pub struct Expr<'hir> { 1310pub struct Expr<'hir> {
1356 pub hir_id: HirId, 1311 pub hir_id: HirId,
1357 pub kind: ExprKind<'hir>, 1312 pub kind: ExprKind<'hir>,
@@ -1472,17 +1427,6 @@ impl Expr<'_> {
1472 } 1427 }
1473} 1428}
1474 1429
1475impl fmt::Debug for Expr<'_> {
1476 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1477 write!(
1478 f,
1479 "expr({}: {})",
1480 self.hir_id,
1481 print::to_string(print::NO_ANN, |s| s.print_expr(self))
1482 )
1483 }
1484}
1485
1486/// Checks if the specified expression is a built-in range literal. 1430/// Checks if the specified expression is a built-in range literal.
1487/// (See: `LoweringContext::lower_expr()`). 1431/// (See: `LoweringContext::lower_expr()`).
1488/// 1432///
@@ -1965,19 +1909,13 @@ impl TypeBinding<'_> {
1965 } 1909 }
1966} 1910}
1967 1911
1968#[derive(RustcEncodable, RustcDecodable)] 1912#[derive(Debug, RustcEncodable, RustcDecodable)]
1969pub struct Ty<'hir> { 1913pub struct Ty<'hir> {
1970 pub hir_id: HirId, 1914 pub hir_id: HirId,
1971 pub kind: TyKind<'hir>, 1915 pub kind: TyKind<'hir>,
1972 pub span: Span, 1916 pub span: Span,
1973} 1917}
1974 1918
1975impl fmt::Debug for Ty<'_> {
1976 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1977 write!(f, "type({})", print::to_string(print::NO_ANN, |s| s.print_type(self)))
1978 }
1979}
1980
1981/// Not represented directly in the AST; referred to by name through a `ty_path`. 1919/// Not represented directly in the AST; referred to by name through a `ty_path`.
1982#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] 1920#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1983#[derive(HashStable_Generic)] 1921#[derive(HashStable_Generic)]
@@ -2182,15 +2120,6 @@ pub enum FnRetTy<'hir> {
2182 Return(&'hir Ty<'hir>), 2120 Return(&'hir Ty<'hir>),
2183} 2121}
2184 2122
2185impl fmt::Display for FnRetTy<'_> {
2186 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2187 match self {
2188 Self::Return(ref ty) => print::to_string(print::NO_ANN, |s| s.print_type(ty)).fmt(f),
2189 Self::DefaultReturn(_) => "()".fmt(f),
2190 }
2191 }
2192}
2193
2194impl FnRetTy<'_> { 2123impl FnRetTy<'_> {
2195 pub fn span(&self) -> Span { 2124 pub fn span(&self) -> Span {
2196 match *self { 2125 match *self {
@@ -2274,13 +2203,10 @@ pub struct TraitRef<'hir> {
2274 2203
2275impl TraitRef<'_> { 2204impl TraitRef<'_> {
2276 /// Gets the `DefId` of the referenced trait. It _must_ actually be a trait or trait alias. 2205 /// Gets the `DefId` of the referenced trait. It _must_ actually be a trait or trait alias.
2277 pub fn trait_def_id(&self) -> DefId { 2206 pub fn trait_def_id(&self) -> Option<DefId> {
2278 match self.path.res { 2207 match self.path.res {
2279 Res::Def(DefKind::Trait, did) => did, 2208 Res::Def(DefKind::Trait | DefKind::TraitAlias, did) => Some(did),
2280 Res::Def(DefKind::TraitAlias, did) => did, 2209 Res::Err => None,
2281 Res::Err => {
2282 FatalError.raise();
2283 }
2284 _ => unreachable!(), 2210 _ => unreachable!(),
2285 } 2211 }
2286 } 2212 }
diff --git a/src/librustc_hir/intravisit.rs b/src/librustc_hir/intravisit.rs
index 11749cf..08b4ef1 100644
--- a/src/librustc_hir/intravisit.rs
+++ b/src/librustc_hir/intravisit.rs
@@ -121,6 +121,8 @@ impl<'a> FnKind<'a> {
121 121
122/// An abstract representation of the HIR `rustc::hir::map::Map`. 122/// An abstract representation of the HIR `rustc::hir::map::Map`.
123pub trait Map<'hir> { 123pub trait Map<'hir> {
124 /// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found.
125 fn find(&self, hir_id: HirId) -> Option<Node<'hir>>;
124 fn body(&self, id: BodyId) -> &'hir Body<'hir>; 126 fn body(&self, id: BodyId) -> &'hir Body<'hir>;
125 fn item(&self, id: HirId) -> &'hir Item<'hir>; 127 fn item(&self, id: HirId) -> &'hir Item<'hir>;
126 fn trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir>; 128 fn trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir>;
@@ -132,6 +134,9 @@ pub trait Map<'hir> {
132pub struct ErasedMap<'hir>(&'hir dyn Map<'hir>); 134pub struct ErasedMap<'hir>(&'hir dyn Map<'hir>);
133 135
134impl<'hir> Map<'hir> for ErasedMap<'hir> { 136impl<'hir> Map<'hir> for ErasedMap<'hir> {
137 fn find(&self, _: HirId) -> Option<Node<'hir>> {
138 None
139 }
135 fn body(&self, id: BodyId) -> &'hir Body<'hir> { 140 fn body(&self, id: BodyId) -> &'hir Body<'hir> {
136 self.0.body(id) 141 self.0.body(id)
137 } 142 }
diff --git a/src/librustc_hir/lib.rs b/src/librustc_hir/lib.rs
index fbb3d6b..49692c7 100644
--- a/src/librustc_hir/lib.rs
+++ b/src/librustc_hir/lib.rs
@@ -7,6 +7,7 @@
7#![feature(const_fn)] // For the unsizing cast on `&[]` 7#![feature(const_fn)] // For the unsizing cast on `&[]`
8#![feature(const_panic)] 8#![feature(const_panic)]
9#![feature(in_band_lifetimes)] 9#![feature(in_band_lifetimes)]
10#![feature(or_patterns)]
10#![feature(specialization)] 11#![feature(specialization)]
11#![recursion_limit = "256"] 12#![recursion_limit = "256"]
12 13
@@ -23,7 +24,6 @@ pub mod intravisit;
23pub mod itemlikevisit; 24pub mod itemlikevisit;
24pub mod lang_items; 25pub mod lang_items;
25pub mod pat_util; 26pub mod pat_util;
26pub mod print;
27mod stable_hash_impls; 27mod stable_hash_impls;
28mod target; 28mod target;
29pub mod weak_lang_items; 29pub mod weak_lang_items;
diff --git a/src/librustc_hir_pretty/Cargo.toml b/src/librustc_hir_pretty/Cargo.toml
new file mode 100644
index 0000000..6a9339b
--- /dev/null
+++ b/src/librustc_hir_pretty/Cargo.toml
@@ -0,0 +1,18 @@
1[package]
2authors = ["The Rust Project Developers"]
3name = "rustc_hir_pretty"
4version = "0.0.0"
5edition = "2018"
6
7[lib]
8name = "rustc_hir_pretty"
9path = "lib.rs"
10doctest = false
11
12[dependencies]
13rustc_ast_pretty = { path = "../librustc_ast_pretty" }
14rustc_hir = { path = "../librustc_hir" }
15rustc_target = { path = "../librustc_target" }
16rustc_data_structures = { path = "../librustc_data_structures" }
17rustc_span = { path = "../librustc_span" }
18rustc_ast = { path = "../librustc_ast" }
diff --git a/src/librustc_hir/print.rs b/src/librustc_hir_pretty/lib.rs
index cd16e45..88b1288 100644
--- a/src/librustc_hir/print.rs
+++ b/src/librustc_hir_pretty/lib.rs
@@ -1,21 +1,26 @@
1#![recursion_limit = "256"]
2
1use rustc_ast::ast; 3use rustc_ast::ast;
2use rustc_ast::util::parser::{self, AssocOp, Fixity}; 4use rustc_ast::util::parser::{self, AssocOp, Fixity};
3use rustc_ast_pretty::pp::Breaks::{Consistent, Inconsistent}; 5use rustc_ast_pretty::pp::Breaks::{Consistent, Inconsistent};
4use rustc_ast_pretty::pp::{self, Breaks}; 6use rustc_ast_pretty::pp::{self, Breaks};
5use rustc_ast_pretty::pprust::{Comments, PrintState}; 7use rustc_ast_pretty::pprust::{Comments, PrintState};
8use rustc_hir as hir;
9use rustc_hir::{GenericArg, GenericParam, GenericParamKind, Node};
10use rustc_hir::{GenericBound, PatKind, RangeEnd, TraitBoundModifier};
6use rustc_span::source_map::{SourceMap, Spanned}; 11use rustc_span::source_map::{SourceMap, Spanned};
7use rustc_span::symbol::{kw, IdentPrinter}; 12use rustc_span::symbol::{kw, IdentPrinter};
8use rustc_span::{self, BytePos, FileName}; 13use rustc_span::{self, BytePos, FileName};
9use rustc_target::spec::abi::Abi; 14use rustc_target::spec::abi::Abi;
10 15
11use crate::hir;
12use crate::hir::{GenericArg, GenericParam, GenericParamKind, Node};
13use crate::hir::{GenericBound, PatKind, RangeEnd, TraitBoundModifier};
14
15use std::borrow::Cow; 16use std::borrow::Cow;
16use std::cell::Cell; 17use std::cell::Cell;
17use std::vec; 18use std::vec;
18 19
20pub fn id_to_string(map: &dyn rustc_hir::intravisit::Map<'_>, hir_id: hir::HirId) -> String {
21 to_string(&map, |s| s.print_node(map.find(hir_id).unwrap()))
22}
23
19pub enum AnnNode<'a> { 24pub enum AnnNode<'a> {
20 Name(&'a ast::Name), 25 Name(&'a ast::Name),
21 Block(&'a hir::Block<'a>), 26 Block(&'a hir::Block<'a>),
@@ -47,7 +52,7 @@ pub struct NoAnn;
47impl PpAnn for NoAnn {} 52impl PpAnn for NoAnn {}
48pub const NO_ANN: &dyn PpAnn = &NoAnn; 53pub const NO_ANN: &dyn PpAnn = &NoAnn;
49 54
50impl PpAnn for hir::Crate<'a> { 55impl PpAnn for hir::Crate<'_> {
51 fn try_fetch_item(&self, item: hir::HirId) -> Option<&hir::Item<'_>> { 56 fn try_fetch_item(&self, item: hir::HirId) -> Option<&hir::Item<'_>> {
52 Some(self.item(item)) 57 Some(self.item(item))
53 } 58 }
@@ -62,6 +67,20 @@ impl PpAnn for hir::Crate<'a> {
62 } 67 }
63} 68}
64 69
70/// Identical to the `PpAnn` implementation for `hir::Crate`,
71/// except it avoids creating a dependency on the whole crate.
72impl PpAnn for &dyn rustc_hir::intravisit::Map<'_> {
73 fn nested(&self, state: &mut State<'_>, nested: Nested) {
74 match nested {
75 Nested::Item(id) => state.print_item(self.item(id.id)),
76 Nested::TraitItem(id) => state.print_trait_item(self.trait_item(id)),
77 Nested::ImplItem(id) => state.print_impl_item(self.impl_item(id)),
78 Nested::Body(id) => state.print_expr(&self.body(id).value),
79 Nested::BodyParamPat(id, i) => state.print_pat(&self.body(id).params[i].pat),
80 }
81 }
82}
83
65pub struct State<'a> { 84pub struct State<'a> {
66 pub s: pp::Printer, 85 pub s: pp::Printer,
67 comments: Option<Comments<'a>>, 86 comments: Option<Comments<'a>>,
@@ -1006,10 +1025,10 @@ impl<'a> State<'a> {
1006 close_box: bool, 1025 close_box: bool,
1007 ) { 1026 ) {
1008 match blk.rules { 1027 match blk.rules {
1009 hir::UnsafeBlock(..) => self.word_space("unsafe"), 1028 hir::BlockCheckMode::UnsafeBlock(..) => self.word_space("unsafe"),
1010 hir::PushUnsafeBlock(..) => self.word_space("push_unsafe"), 1029 hir::BlockCheckMode::PushUnsafeBlock(..) => self.word_space("push_unsafe"),
1011 hir::PopUnsafeBlock(..) => self.word_space("pop_unsafe"), 1030 hir::BlockCheckMode::PopUnsafeBlock(..) => self.word_space("pop_unsafe"),
1012 hir::DefaultBlock => (), 1031 hir::BlockCheckMode::DefaultBlock => (),
1013 } 1032 }
1014 self.maybe_print_comment(blk.span.lo()); 1033 self.maybe_print_comment(blk.span.lo());
1015 self.ann.pre(self, AnnNode::Block(blk)); 1034 self.ann.pre(self, AnnNode::Block(blk));
@@ -1092,7 +1111,7 @@ impl<'a> State<'a> {
1092 &mut self, 1111 &mut self,
1093 qpath: &hir::QPath<'_>, 1112 qpath: &hir::QPath<'_>,
1094 fields: &[hir::Field<'_>], 1113 fields: &[hir::Field<'_>],
1095 wth: &Option<&'hir hir::Expr<'_>>, 1114 wth: &Option<&hir::Expr<'_>>,
1096 ) { 1115 ) {
1097 self.print_qpath(qpath, true); 1116 self.print_qpath(qpath, true);
1098 self.s.word("{"); 1117 self.s.word("{");
@@ -1848,7 +1867,8 @@ impl<'a> State<'a> {
1848 self.print_block_unclosed(&blk); 1867 self.print_block_unclosed(&blk);
1849 1868
1850 // If it is a user-provided unsafe block, print a comma after it 1869 // If it is a user-provided unsafe block, print a comma after it
1851 if let hir::UnsafeBlock(hir::UserProvided) = blk.rules { 1870 if let hir::BlockCheckMode::UnsafeBlock(hir::UnsafeSource::UserProvided) = blk.rules
1871 {
1852 self.s.word(","); 1872 self.s.word(",");
1853 } 1873 }
1854 } 1874 }
@@ -1928,18 +1948,18 @@ impl<'a> State<'a> {
1928 }); 1948 });
1929 self.s.word("|"); 1949 self.s.word("|");
1930 1950
1931 if let hir::DefaultReturn(..) = decl.output { 1951 if let hir::FnRetTy::DefaultReturn(..) = decl.output {
1932 return; 1952 return;
1933 } 1953 }
1934 1954
1935 self.space_if_not_bol(); 1955 self.space_if_not_bol();
1936 self.word_space("->"); 1956 self.word_space("->");
1937 match decl.output { 1957 match decl.output {
1938 hir::Return(ref ty) => { 1958 hir::FnRetTy::Return(ref ty) => {
1939 self.print_type(&ty); 1959 self.print_type(&ty);
1940 self.maybe_print_comment(ty.span.lo()) 1960 self.maybe_print_comment(ty.span.lo())
1941 } 1961 }
1942 hir::DefaultReturn(..) => unreachable!(), 1962 hir::FnRetTy::DefaultReturn(..) => unreachable!(),
1943 } 1963 }
1944 } 1964 }
1945 1965
@@ -2112,7 +2132,7 @@ impl<'a> State<'a> {
2112 } 2132 }
2113 2133
2114 pub fn print_fn_output(&mut self, decl: &hir::FnDecl<'_>) { 2134 pub fn print_fn_output(&mut self, decl: &hir::FnDecl<'_>) {
2115 if let hir::DefaultReturn(..) = decl.output { 2135 if let hir::FnRetTy::DefaultReturn(..) = decl.output {
2116 return; 2136 return;
2117 } 2137 }
2118 2138
@@ -2120,13 +2140,13 @@ impl<'a> State<'a> {
2120 self.ibox(INDENT_UNIT); 2140 self.ibox(INDENT_UNIT);
2121 self.word_space("->"); 2141 self.word_space("->");
2122 match decl.output { 2142 match decl.output {
2123 hir::DefaultReturn(..) => unreachable!(), 2143 hir::FnRetTy::DefaultReturn(..) => unreachable!(),
2124 hir::Return(ref ty) => self.print_type(&ty), 2144 hir::FnRetTy::Return(ref ty) => self.print_type(&ty),
2125 } 2145 }
2126 self.end(); 2146 self.end();
2127 2147
2128 match decl.output { 2148 match decl.output {
2129 hir::Return(ref output) => self.maybe_print_comment(output.span.lo()), 2149 hir::FnRetTy::Return(ref output) => self.maybe_print_comment(output.span.lo()),
2130 _ => {} 2150 _ => {}
2131 } 2151 }
2132 } 2152 }
diff --git a/src/librustc_metadata/Cargo.toml b/src/librustc_metadata/Cargo.toml
index 088cba8..ea439b1 100644
--- a/src/librustc_metadata/Cargo.toml
+++ b/src/librustc_metadata/Cargo.toml
@@ -19,6 +19,7 @@ rustc_attr = { path = "../librustc_attr" }
19rustc_data_structures = { path = "../librustc_data_structures" } 19rustc_data_structures = { path = "../librustc_data_structures" }
20rustc_errors = { path = "../librustc_errors" } 20rustc_errors = { path = "../librustc_errors" }
21rustc_hir = { path = "../librustc_hir" } 21rustc_hir = { path = "../librustc_hir" }
22rustc_hir_pretty = { path = "../librustc_hir_pretty" }
22rustc_target = { path = "../librustc_target" } 23rustc_target = { path = "../librustc_target" }
23rustc_index = { path = "../librustc_index" } 24rustc_index = { path = "../librustc_index" }
24rustc_serialize = { path = "../libserialize", package = "serialize" } 25rustc_serialize = { path = "../libserialize", package = "serialize" }
diff --git a/src/librustc_metadata/rmeta/encoder.rs b/src/librustc_metadata/rmeta/encoder.rs
index 9718d19..3686b2f 100644
--- a/src/librustc_metadata/rmeta/encoder.rs
+++ b/src/librustc_metadata/rmeta/encoder.rs
@@ -824,8 +824,10 @@ impl EncodeContext<'tcx> {
824 824
825 record!(self.per_def.kind[def_id] <- match trait_item.kind { 825 record!(self.per_def.kind[def_id] <- match trait_item.kind {
826 ty::AssocKind::Const => { 826 ty::AssocKind::Const => {
827 let rendered = 827 let rendered = rustc_hir_pretty::to_string(
828 hir::print::to_string(&self.tcx.hir(), |s| s.print_trait_item(ast_item)); 828 &(&self.tcx.hir() as &dyn intravisit::Map<'_>),
829 |s| s.print_trait_item(ast_item)
830 );
829 let rendered_const = self.lazy(RenderedConst(rendered)); 831 let rendered_const = self.lazy(RenderedConst(rendered));
830 832
831 EntryKind::AssocConst( 833 EntryKind::AssocConst(
@@ -1044,8 +1046,11 @@ impl EncodeContext<'tcx> {
1044 } 1046 }
1045 1047
1046 fn encode_rendered_const_for_body(&mut self, body_id: hir::BodyId) -> Lazy<RenderedConst> { 1048 fn encode_rendered_const_for_body(&mut self, body_id: hir::BodyId) -> Lazy<RenderedConst> {
1047 let body = self.tcx.hir().body(body_id); 1049 let hir = self.tcx.hir();
1048 let rendered = hir::print::to_string(&self.tcx.hir(), |s| s.print_expr(&body.value)); 1050 let body = hir.body(body_id);
1051 let rendered = rustc_hir_pretty::to_string(&(&hir as &dyn intravisit::Map<'_>), |s| {
1052 s.print_expr(&body.value)
1053 });
1049 let rendered_const = &RenderedConst(rendered); 1054 let rendered_const = &RenderedConst(rendered);
1050 self.lazy(rendered_const) 1055 self.lazy(rendered_const)
1051 } 1056 }
diff --git a/src/librustc_mir/borrow_check/mod.rs b/src/librustc_mir/borrow_check/mod.rs
index 6c19014..882c6bd 100644
--- a/src/librustc_mir/borrow_check/mod.rs
+++ b/src/librustc_mir/borrow_check/mod.rs
@@ -29,12 +29,12 @@ use std::mem;
29use std::rc::Rc; 29use std::rc::Rc;
30 30
31use crate::dataflow; 31use crate::dataflow;
32use crate::dataflow::generic::{Analysis, BorrowckFlowState as Flows, BorrowckResults};
33use crate::dataflow::indexes::{BorrowIndex, InitIndex, MoveOutIndex, MovePathIndex}; 32use crate::dataflow::indexes::{BorrowIndex, InitIndex, MoveOutIndex, MovePathIndex};
34use crate::dataflow::move_paths::{InitLocation, LookupResult, MoveData, MoveError}; 33use crate::dataflow::move_paths::{InitLocation, LookupResult, MoveData, MoveError};
35use crate::dataflow::Borrows; 34use crate::dataflow::Borrows;
36use crate::dataflow::EverInitializedPlaces; 35use crate::dataflow::EverInitializedPlaces;
37use crate::dataflow::MoveDataParamEnv; 36use crate::dataflow::MoveDataParamEnv;
37use crate::dataflow::{Analysis, BorrowckFlowState as Flows, BorrowckResults};
38use crate::dataflow::{MaybeInitializedPlaces, MaybeUninitializedPlaces}; 38use crate::dataflow::{MaybeInitializedPlaces, MaybeUninitializedPlaces};
39use crate::transform::MirSource; 39use crate::transform::MirSource;
40 40
@@ -298,7 +298,7 @@ fn do_mir_borrowck<'a, 'tcx>(
298 mbcx.report_move_errors(errors); 298 mbcx.report_move_errors(errors);
299 } 299 }
300 300
301 dataflow::generic::visit_results( 301 dataflow::visit_results(
302 &*body, 302 &*body,
303 traversal::reverse_postorder(&*body).map(|(bb, _)| bb), 303 traversal::reverse_postorder(&*body).map(|(bb, _)| bb),
304 &results, 304 &results,
@@ -509,7 +509,7 @@ crate struct MirBorrowckCtxt<'cx, 'tcx> {
509// 2. loans made in overlapping scopes do not conflict 509// 2. loans made in overlapping scopes do not conflict
510// 3. assignments do not affect things loaned out as immutable 510// 3. assignments do not affect things loaned out as immutable
511// 4. moves do not affect things loaned out in any way 511// 4. moves do not affect things loaned out in any way
512impl<'cx, 'tcx> dataflow::generic::ResultsVisitor<'cx, 'tcx> for MirBorrowckCtxt<'cx, 'tcx> { 512impl<'cx, 'tcx> dataflow::ResultsVisitor<'cx, 'tcx> for MirBorrowckCtxt<'cx, 'tcx> {
513 type FlowState = Flows<'cx, 'tcx>; 513 type FlowState = Flows<'cx, 'tcx>;
514 514
515 fn visit_statement( 515 fn visit_statement(
diff --git a/src/librustc_mir/borrow_check/nll.rs b/src/librustc_mir/borrow_check/nll.rs
index 077ed49..8e929a4 100644
--- a/src/librustc_mir/borrow_check/nll.rs
+++ b/src/librustc_mir/borrow_check/nll.rs
@@ -21,9 +21,9 @@ use std::str::FromStr;
21use self::mir_util::PassWhere; 21use self::mir_util::PassWhere;
22use polonius_engine::{Algorithm, Output}; 22use polonius_engine::{Algorithm, Output};
23 23
24use crate::dataflow::generic::ResultsCursor;
25use crate::dataflow::move_paths::{InitKind, InitLocation, MoveData}; 24use crate::dataflow::move_paths::{InitKind, InitLocation, MoveData};
26use crate::dataflow::MaybeInitializedPlaces; 25use crate::dataflow::MaybeInitializedPlaces;
26use crate::dataflow::ResultsCursor;
27use crate::transform::MirSource; 27use crate::transform::MirSource;
28use crate::util as mir_util; 28use crate::util as mir_util;
29use crate::util::pretty; 29use crate::util::pretty;
diff --git a/src/librustc_mir/borrow_check/type_check/liveness/mod.rs b/src/librustc_mir/borrow_check/type_check/liveness/mod.rs
index cdf962e..a55529e 100644
--- a/src/librustc_mir/borrow_check/type_check/liveness/mod.rs
+++ b/src/librustc_mir/borrow_check/type_check/liveness/mod.rs
@@ -3,9 +3,9 @@ use rustc::ty::{RegionVid, TyCtxt};
3use rustc_data_structures::fx::FxHashSet; 3use rustc_data_structures::fx::FxHashSet;
4use std::rc::Rc; 4use std::rc::Rc;
5 5
6use crate::dataflow::generic::ResultsCursor;
7use crate::dataflow::move_paths::MoveData; 6use crate::dataflow::move_paths::MoveData;
8use crate::dataflow::MaybeInitializedPlaces; 7use crate::dataflow::MaybeInitializedPlaces;
8use crate::dataflow::ResultsCursor;
9 9
10use crate::borrow_check::{ 10use crate::borrow_check::{
11 constraints::OutlivesConstraintSet, 11 constraints::OutlivesConstraintSet,
diff --git a/src/librustc_mir/borrow_check/type_check/liveness/trace.rs b/src/librustc_mir/borrow_check/type_check/liveness/trace.rs
index 0c49ee4..75b269c 100644
--- a/src/librustc_mir/borrow_check/type_check/liveness/trace.rs
+++ b/src/librustc_mir/borrow_check/type_check/liveness/trace.rs
@@ -8,10 +8,10 @@ use rustc_trait_selection::traits::query::type_op::outlives::DropckOutlives;
8use rustc_trait_selection::traits::query::type_op::TypeOp; 8use rustc_trait_selection::traits::query::type_op::TypeOp;
9use std::rc::Rc; 9use std::rc::Rc;
10 10
11use crate::dataflow::generic::ResultsCursor;
12use crate::dataflow::indexes::MovePathIndex; 11use crate::dataflow::indexes::MovePathIndex;
13use crate::dataflow::move_paths::{HasMoveData, MoveData}; 12use crate::dataflow::move_paths::{HasMoveData, MoveData};
14use crate::dataflow::MaybeInitializedPlaces; 13use crate::dataflow::MaybeInitializedPlaces;
14use crate::dataflow::ResultsCursor;
15 15
16use crate::borrow_check::{ 16use crate::borrow_check::{
17 region_infer::values::{self, PointIndex, RegionValueElements}, 17 region_infer::values::{self, PointIndex, RegionValueElements},
diff --git a/src/librustc_mir/borrow_check/type_check/mod.rs b/src/librustc_mir/borrow_check/type_check/mod.rs
index f94160c..02f6bca 100644
--- a/src/librustc_mir/borrow_check/type_check/mod.rs
+++ b/src/librustc_mir/borrow_check/type_check/mod.rs
@@ -39,9 +39,9 @@ use rustc_trait_selection::traits::query::type_op::custom::CustomTypeOp;
39use rustc_trait_selection::traits::query::{Fallible, NoSolution}; 39use rustc_trait_selection::traits::query::{Fallible, NoSolution};
40use rustc_trait_selection::traits::{self, ObligationCause, PredicateObligations}; 40use rustc_trait_selection::traits::{self, ObligationCause, PredicateObligations};
41 41
42use crate::dataflow::generic::ResultsCursor;
43use crate::dataflow::move_paths::MoveData; 42use crate::dataflow::move_paths::MoveData;
44use crate::dataflow::MaybeInitializedPlaces; 43use crate::dataflow::MaybeInitializedPlaces;
44use crate::dataflow::ResultsCursor;
45use crate::transform::promote_consts::should_suggest_const_in_array_repeat_expressions_attribute; 45use crate::transform::promote_consts::should_suggest_const_in_array_repeat_expressions_attribute;
46 46
47use crate::borrow_check::{ 47use crate::borrow_check::{
diff --git a/src/librustc_mir/dataflow/at_location.rs b/src/librustc_mir/dataflow/at_location.rs
deleted file mode 100644
index e4eb850..0000000
--- a/src/librustc_mir/dataflow/at_location.rs
+++ /dev/null
@@ -1,169 +0,0 @@
1//! A nice wrapper to consume dataflow results at several CFG
2//! locations.
3
4use rustc::mir::{BasicBlock, Location};
5use rustc_index::bit_set::{BitIter, BitSet, HybridBitSet};
6
7use crate::dataflow::{BitDenotation, DataflowResults, GenKillSet};
8
9use std::borrow::Borrow;
10use std::iter;
11
12/// A trait for "cartesian products" of multiple FlowAtLocation.
13///
14/// There's probably a way to auto-impl this, but I think
15/// it is cleaner to have manual visitor impls.
16pub trait FlowsAtLocation {
17 /// Reset the state bitvector to represent the entry to block `bb`.
18 fn reset_to_entry_of(&mut self, bb: BasicBlock);
19
20 /// Reset the state bitvector to represent the exit of the
21 /// terminator of block `bb`.
22 ///
23 /// **Important:** In the case of a `Call` terminator, these
24 /// effects do *not* include the result of storing the destination
25 /// of the call, since that is edge-dependent (in other words, the
26 /// effects don't apply to the unwind edge).
27 fn reset_to_exit_of(&mut self, bb: BasicBlock);
28
29 /// Builds gen and kill sets for statement at `loc`.
30 ///
31 /// Note that invoking this method alone does not change the
32 /// `curr_state` -- you must invoke `apply_local_effect`
33 /// afterwards.
34 fn reconstruct_statement_effect(&mut self, loc: Location);
35
36 /// Builds gen and kill sets for terminator for `loc`.
37 ///
38 /// Note that invoking this method alone does not change the
39 /// `curr_state` -- you must invoke `apply_local_effect`
40 /// afterwards.
41 fn reconstruct_terminator_effect(&mut self, loc: Location);
42
43 /// Apply current gen + kill sets to `flow_state`.
44 ///
45 /// (`loc` parameters can be ignored if desired by
46 /// client. For the terminator, the `stmt_idx` will be the number
47 /// of statements in the block.)
48 fn apply_local_effect(&mut self, loc: Location);
49}
50
51/// Represents the state of dataflow at a particular
52/// CFG location, both before and after it is
53/// executed.
54///
55/// Data flow results are typically computed only as basic block
56/// boundaries. A `FlowInProgress` allows you to reconstruct the
57/// effects at any point in the control-flow graph by starting with
58/// the state at the start of the basic block (`reset_to_entry_of`)
59/// and then replaying the effects of statements and terminators
60/// (e.g., via `reconstruct_statement_effect` and
61/// `reconstruct_terminator_effect`; don't forget to call
62/// `apply_local_effect`).
63pub struct FlowAtLocation<'tcx, BD, DR = DataflowResults<'tcx, BD>>
64where
65 BD: BitDenotation<'tcx>,
66 DR: Borrow<DataflowResults<'tcx, BD>>,
67{
68 base_results: DR,
69 curr_state: BitSet<BD::Idx>,
70 stmt_trans: GenKillSet<BD::Idx>,
71}
72
73impl<'tcx, BD, DR> FlowAtLocation<'tcx, BD, DR>
74where
75 BD: BitDenotation<'tcx>,
76 DR: Borrow<DataflowResults<'tcx, BD>>,
77{
78 /// Iterate over each bit set in the current state.
79 pub fn each_state_bit<F>(&self, f: F)
80 where
81 F: FnMut(BD::Idx),
82 {
83 self.curr_state.iter().for_each(f)
84 }
85
86 /// Iterate over each `gen` bit in the current effect (invoke
87 /// `reconstruct_statement_effect` or
88 /// `reconstruct_terminator_effect` first).
89 pub fn each_gen_bit<F>(&self, f: F)
90 where
91 F: FnMut(BD::Idx),
92 {
93 self.stmt_trans.gen_set.iter().for_each(f)
94 }
95
96 pub fn new(results: DR) -> Self {
97 let bits_per_block = results.borrow().sets().bits_per_block();
98 let curr_state = BitSet::new_empty(bits_per_block);
99 let stmt_trans = GenKillSet::from_elem(HybridBitSet::new_empty(bits_per_block));
100 FlowAtLocation { base_results: results, curr_state, stmt_trans }
101 }
102
103 /// Access the underlying operator.
104 pub fn operator(&self) -> &BD {
105 self.base_results.borrow().operator()
106 }
107
108 pub fn contains(&self, x: BD::Idx) -> bool {
109 self.curr_state.contains(x)
110 }
111
112 /// Returns an iterator over the elements present in the current state.
113 pub fn iter_incoming(&self) -> iter::Peekable<BitIter<'_, BD::Idx>> {
114 self.curr_state.iter().peekable()
115 }
116
117 /// Creates a clone of the current state and applies the local
118 /// effects to the clone (leaving the state of self intact).
119 /// Invokes `f` with an iterator over the resulting state.
120 pub fn with_iter_outgoing<F>(&self, f: F)
121 where
122 F: FnOnce(BitIter<'_, BD::Idx>),
123 {
124 let mut curr_state = self.curr_state.clone();
125 self.stmt_trans.apply(&mut curr_state);
126 f(curr_state.iter());
127 }
128
129 /// Returns a bitset of the elements present in the current state.
130 pub fn as_dense(&self) -> &BitSet<BD::Idx> {
131 &self.curr_state
132 }
133}
134
135impl<'tcx, BD, DR> FlowsAtLocation for FlowAtLocation<'tcx, BD, DR>
136where
137 BD: BitDenotation<'tcx>,
138 DR: Borrow<DataflowResults<'tcx, BD>>,
139{
140 fn reset_to_entry_of(&mut self, bb: BasicBlock) {
141 self.curr_state.overwrite(self.base_results.borrow().sets().entry_set_for(bb.index()));
142 }
143
144 fn reset_to_exit_of(&mut self, bb: BasicBlock) {
145 self.reset_to_entry_of(bb);
146 let trans = self.base_results.borrow().sets().trans_for(bb.index());
147 trans.apply(&mut self.curr_state)
148 }
149
150 fn reconstruct_statement_effect(&mut self, loc: Location) {
151 self.stmt_trans.clear();
152 self.base_results.borrow().operator().before_statement_effect(&mut self.stmt_trans, loc);
153 self.stmt_trans.apply(&mut self.curr_state);
154
155 self.base_results.borrow().operator().statement_effect(&mut self.stmt_trans, loc);
156 }
157
158 fn reconstruct_terminator_effect(&mut self, loc: Location) {
159 self.stmt_trans.clear();
160 self.base_results.borrow().operator().before_terminator_effect(&mut self.stmt_trans, loc);
161 self.stmt_trans.apply(&mut self.curr_state);
162
163 self.base_results.borrow().operator().terminator_effect(&mut self.stmt_trans, loc);
164 }
165
166 fn apply_local_effect(&mut self, _loc: Location) {
167 self.stmt_trans.apply(&mut self.curr_state)
168 }
169}
diff --git a/src/librustc_mir/dataflow/generic/cursor.rs b/src/librustc_mir/dataflow/framework/cursor.rs
index 170157a..170157a 100644
--- a/src/librustc_mir/dataflow/generic/cursor.rs
+++ b/src/librustc_mir/dataflow/framework/cursor.rs
diff --git a/src/librustc_mir/dataflow/generic/engine.rs b/src/librustc_mir/dataflow/framework/engine.rs
index d320721..d320721 100644
--- a/src/librustc_mir/dataflow/generic/engine.rs
+++ b/src/librustc_mir/dataflow/framework/engine.rs
diff --git a/src/librustc_mir/dataflow/generic/graphviz.rs b/src/librustc_mir/dataflow/framework/graphviz.rs
index c15f2a7..c15f2a7 100644
--- a/src/librustc_mir/dataflow/generic/graphviz.rs
+++ b/src/librustc_mir/dataflow/framework/graphviz.rs
diff --git a/src/librustc_mir/dataflow/generic/mod.rs b/src/librustc_mir/dataflow/framework/mod.rs
index fb4b7b9..8556be7 100644
--- a/src/librustc_mir/dataflow/generic/mod.rs
+++ b/src/librustc_mir/dataflow/framework/mod.rs
@@ -1,26 +1,25 @@
1//! A framework that can express both [gen-kill] and generic dataflow problems. 1//! A framework that can express both [gen-kill] and generic dataflow problems.
2//! 2//!
3//! There is another interface for dataflow in the compiler in `librustc_mir/dataflow/mod.rs`. The 3//! To actually use this framework, you must implement either the `Analysis` or the
4//! interface in this module will eventually [replace that one][design-meeting]. 4//! `GenKillAnalysis` trait. If your transfer function can be expressed with only gen/kill
5//! operations, prefer `GenKillAnalysis` since it will run faster while iterating to fixpoint. The
6//! `impls` module contains several examples of gen/kill dataflow analyses.
5//! 7//!
6//! To actually use this framework, you must implement either the `Analysis` or the `GenKillAnalysis` 8//! Create an `Engine` for your analysis using the `into_engine` method on the `Analysis` trait,
7//! trait. If your transfer function can be expressed with only gen/kill operations, prefer 9//! then call `iterate_to_fixpoint`. From there, you can use a `ResultsCursor` to inspect the
8//! `GenKillAnalysis` since it will run faster while iterating to fixpoint. Create an `Engine` using 10//! fixpoint solution to your dataflow problem, or implement the `ResultsVisitor` interface and use
9//! the appropriate constructor and call `iterate_to_fixpoint`. You can use a `ResultsCursor` to 11//! `visit_results`. The following example uses the `ResultsCursor` approach.
10//! inspect the fixpoint solution to your dataflow problem.
11//! 12//!
12//! ```ignore(cross-crate-imports) 13//! ```ignore(cross-crate-imports)
13//! fn do_my_analysis(tcx: TyCtxt<'tcx>, body: &mir::Body<'tcx>, did: DefId) { 14//! use rustc_mir::dataflow::Analysis; // Makes `into_engine` available.
14//! let analysis = MyAnalysis::new();
15//!
16//! // If `MyAnalysis` implements `GenKillAnalysis`.
17//! let results = Engine::new_gen_kill(tcx, body, did, analysis).iterate_to_fixpoint();
18//! 15//!
19//! // If `MyAnalysis` implements `Analysis`. 16//! fn do_my_analysis(tcx: TyCtxt<'tcx>, body: &mir::Body<'tcx>, did: DefId) {
20//! // let results = Engine::new_generic(tcx, body, did, analysis).iterate_to_fixpoint(); 17//! let analysis = MyAnalysis::new()
21//! 18//! .into_engine(tcx, body, did)
22//! let mut cursor = ResultsCursor::new(body, results); 19//! .iterate_to_fixpoint()
20//! .into_results_cursor(body);
23//! 21//!
22//! // Print the dataflow state *after* each statement in the start block.
24//! for (_, statement_index) in body.block_data[START_BLOCK].statements.iter_enumerated() { 23//! for (_, statement_index) in body.block_data[START_BLOCK].statements.iter_enumerated() {
25//! cursor.seek_after(Location { block: START_BLOCK, statement_index }); 24//! cursor.seek_after(Location { block: START_BLOCK, statement_index });
26//! let state = cursor.get(); 25//! let state = cursor.get();
@@ -30,7 +29,6 @@
30//! ``` 29//! ```
31//! 30//!
32//! [gen-kill]: https://en.wikipedia.org/wiki/Data-flow_analysis#Bit_vector_problems 31//! [gen-kill]: https://en.wikipedia.org/wiki/Data-flow_analysis#Bit_vector_problems
33//! [design-meeting]https://github.com/rust-lang/compiler-team/issues/202
34 32
35use std::io; 33use std::io;
36 34
@@ -41,8 +39,6 @@ use rustc_hir::def_id::DefId;
41use rustc_index::bit_set::{BitSet, HybridBitSet}; 39use rustc_index::bit_set::{BitSet, HybridBitSet};
42use rustc_index::vec::{Idx, IndexVec}; 40use rustc_index::vec::{Idx, IndexVec};
43 41
44use crate::dataflow::BottomValue;
45
46mod cursor; 42mod cursor;
47mod engine; 43mod engine;
48mod graphviz; 44mod graphviz;
@@ -95,6 +91,47 @@ where
95 } 91 }
96} 92}
97 93
94/// Parameterization for the precise form of data flow that is used.
95///
96/// `BottomValue` determines whether the initial entry set for each basic block is empty or full.
97/// This also determines the semantics of the lattice `join` operator used to merge dataflow
98/// results, since dataflow works by starting at the bottom and moving monotonically to a fixed
99/// point.
100///
101/// This means, for propagation across the graph, that you either want to start at all-zeroes and
102/// then use Union as your merge when propagating, or you start at all-ones and then use Intersect
103/// as your merge when propagating.
104pub trait BottomValue {
105 /// Specifies the initial value for each bit in the entry set for each basic block.
106 const BOTTOM_VALUE: bool;
107
108 /// Merges `in_set` into `inout_set`, returning `true` if `inout_set` changed.
109 ///
110 /// It is almost certainly wrong to override this, since it automatically applies
111 /// * `inout_set & in_set` if `BOTTOM_VALUE == true`
112 /// * `inout_set | in_set` if `BOTTOM_VALUE == false`
113 ///
114 /// This means that if a bit is not `BOTTOM_VALUE`, it is propagated into all target blocks.
115 /// For clarity, the above statement again from a different perspective:
116 /// A bit in the block's entry set is `!BOTTOM_VALUE` if *any* predecessor block's bit value is
117 /// `!BOTTOM_VALUE`.
118 ///
119 /// There are situations where you want the opposite behaviour: propagate only if *all*
120 /// predecessor blocks's value is `!BOTTOM_VALUE`.
121 /// E.g. if you want to know whether a bit is *definitely* set at a specific location. This
122 /// means that all code paths leading to the location must have set the bit, instead of any
123 /// code path leading there.
124 ///
125 /// If you want this kind of "definitely set" analysis, you need to
126 /// 1. Invert `BOTTOM_VALUE`
127 /// 2. Reset the `entry_set` in `start_block_effect` to `!BOTTOM_VALUE`
128 /// 3. Override `join` to do the opposite from what it's doing now.
129 #[inline]
130 fn join<T: Idx>(&self, inout_set: &mut BitSet<T>, in_set: &BitSet<T>) -> bool {
131 if !Self::BOTTOM_VALUE { inout_set.union(in_set) } else { inout_set.intersect(in_set) }
132 }
133}
134
98/// Define the domain of a dataflow problem. 135/// Define the domain of a dataflow problem.
99/// 136///
100/// This trait specifies the lattice on which this analysis operates. For now, this must be a 137/// This trait specifies the lattice on which this analysis operates. For now, this must be a
diff --git a/src/librustc_mir/dataflow/generic/tests.rs b/src/librustc_mir/dataflow/framework/tests.rs
index 8f07a10..8f07a10 100644
--- a/src/librustc_mir/dataflow/generic/tests.rs
+++ b/src/librustc_mir/dataflow/framework/tests.rs
diff --git a/src/librustc_mir/dataflow/generic/visitor.rs b/src/librustc_mir/dataflow/framework/visitor.rs
index 6e1513b..6e1513b 100644
--- a/src/librustc_mir/dataflow/generic/visitor.rs
+++ b/src/librustc_mir/dataflow/framework/visitor.rs
diff --git a/src/librustc_mir/dataflow/impls/borrowed_locals.rs b/src/librustc_mir/dataflow/impls/borrowed_locals.rs
index 95a676c..955021d 100644
--- a/src/librustc_mir/dataflow/impls/borrowed_locals.rs
+++ b/src/librustc_mir/dataflow/impls/borrowed_locals.rs
@@ -1,6 +1,6 @@
1pub use super::*; 1pub use super::*;
2 2
3use crate::dataflow::generic::{AnalysisDomain, GenKill, GenKillAnalysis}; 3use crate::dataflow::{AnalysisDomain, GenKill, GenKillAnalysis};
4use rustc::mir::visit::Visitor; 4use rustc::mir::visit::Visitor;
5use rustc::mir::*; 5use rustc::mir::*;
6use rustc::ty::{ParamEnv, TyCtxt}; 6use rustc::ty::{ParamEnv, TyCtxt};
diff --git a/src/librustc_mir/dataflow/impls/borrows.rs b/src/librustc_mir/dataflow/impls/borrows.rs
index a7c0efd..9e9e414 100644
--- a/src/librustc_mir/dataflow/impls/borrows.rs
+++ b/src/librustc_mir/dataflow/impls/borrows.rs
@@ -8,8 +8,8 @@ use rustc_index::bit_set::BitSet;
8use crate::borrow_check::{ 8use crate::borrow_check::{
9 places_conflict, BorrowSet, PlaceConflictBias, PlaceExt, RegionInferenceContext, ToRegionVid, 9 places_conflict, BorrowSet, PlaceConflictBias, PlaceExt, RegionInferenceContext, ToRegionVid,
10}; 10};
11use crate::dataflow::generic::{self, GenKill};
12use crate::dataflow::BottomValue; 11use crate::dataflow::BottomValue;
12use crate::dataflow::{self, GenKill};
13 13
14use std::rc::Rc; 14use std::rc::Rc;
15 15
@@ -226,7 +226,7 @@ impl<'a, 'tcx> Borrows<'a, 'tcx> {
226 } 226 }
227} 227}
228 228
229impl<'tcx> generic::AnalysisDomain<'tcx> for Borrows<'_, 'tcx> { 229impl<'tcx> dataflow::AnalysisDomain<'tcx> for Borrows<'_, 'tcx> {
230 type Idx = BorrowIndex; 230 type Idx = BorrowIndex;
231 231
232 const NAME: &'static str = "borrows"; 232 const NAME: &'static str = "borrows";
@@ -245,7 +245,7 @@ impl<'tcx> generic::AnalysisDomain<'tcx> for Borrows<'_, 'tcx> {
245 } 245 }
246} 246}
247 247
248impl<'tcx> generic::GenKillAnalysis<'tcx> for Borrows<'_, 'tcx> { 248impl<'tcx> dataflow::GenKillAnalysis<'tcx> for Borrows<'_, 'tcx> {
249 fn before_statement_effect( 249 fn before_statement_effect(
250 &self, 250 &self,
251 trans: &mut impl GenKill<Self::Idx>, 251 trans: &mut impl GenKill<Self::Idx>,
diff --git a/src/librustc_mir/dataflow/impls/mod.rs b/src/librustc_mir/dataflow/impls/mod.rs
index 87d8e9e..180094e 100644
--- a/src/librustc_mir/dataflow/impls/mod.rs
+++ b/src/librustc_mir/dataflow/impls/mod.rs
@@ -12,9 +12,8 @@ use super::MoveDataParamEnv;
12 12
13use crate::util::elaborate_drops::DropFlagState; 13use crate::util::elaborate_drops::DropFlagState;
14 14
15use super::generic::{AnalysisDomain, GenKill, GenKillAnalysis};
16use super::move_paths::{HasMoveData, InitIndex, InitKind, LookupResult, MoveData, MovePathIndex}; 15use super::move_paths::{HasMoveData, InitIndex, InitKind, LookupResult, MoveData, MovePathIndex};
17use super::BottomValue; 16use super::{AnalysisDomain, BottomValue, GenKill, GenKillAnalysis};
18 17
19use super::drop_flag_effects_for_function_entry; 18use super::drop_flag_effects_for_function_entry;
20use super::drop_flag_effects_for_location; 19use super::drop_flag_effects_for_location;
diff --git a/src/librustc_mir/dataflow/impls/storage_liveness.rs b/src/librustc_mir/dataflow/impls/storage_liveness.rs
index 5341d66..f8d1efa 100644
--- a/src/librustc_mir/dataflow/impls/storage_liveness.rs
+++ b/src/librustc_mir/dataflow/impls/storage_liveness.rs
@@ -1,7 +1,7 @@
1pub use super::*; 1pub use super::*;
2 2
3use crate::dataflow::generic::{self as dataflow, GenKill, Results, ResultsRefCursor};
4use crate::dataflow::BottomValue; 3use crate::dataflow::BottomValue;
4use crate::dataflow::{self, GenKill, Results, ResultsRefCursor};
5use rustc::mir::visit::{NonMutatingUseContext, PlaceContext, Visitor}; 5use rustc::mir::visit::{NonMutatingUseContext, PlaceContext, Visitor};
6use rustc::mir::*; 6use rustc::mir::*;
7use std::cell::RefCell; 7use std::cell::RefCell;
diff --git a/src/librustc_mir/dataflow/mod.rs b/src/librustc_mir/dataflow/mod.rs
index c98a5e8..96e5b69 100644
--- a/src/librustc_mir/dataflow/mod.rs
+++ b/src/librustc_mir/dataflow/mod.rs
@@ -1,35 +1,22 @@
1use rustc::mir::traversal; 1use rustc::ty;
2use rustc::mir::{self, BasicBlock, BasicBlockData, Body, Location, Statement, Terminator};
3use rustc::ty::{self, TyCtxt};
4use rustc_ast::ast::{self, MetaItem}; 2use rustc_ast::ast::{self, MetaItem};
5use rustc_ast_pretty::pprust;
6use rustc_data_structures::work_queue::WorkQueue;
7use rustc_hir::def_id::DefId;
8use rustc_index::bit_set::{BitSet, HybridBitSet};
9use rustc_index::vec::Idx;
10use rustc_session::Session;
11use rustc_span::symbol::{sym, Symbol}; 3use rustc_span::symbol::{sym, Symbol};
12 4
13use std::borrow::Borrow;
14use std::fmt;
15use std::io;
16use std::path::PathBuf;
17
18pub use self::at_location::{FlowAtLocation, FlowsAtLocation};
19pub(crate) use self::drop_flag_effects::*; 5pub(crate) use self::drop_flag_effects::*;
20pub use self::impls::borrows::Borrows; 6pub use self::framework::{
21pub use self::impls::DefinitelyInitializedPlaces; 7 visit_results, Analysis, AnalysisDomain, BorrowckFlowState, BorrowckResults, BottomValue,
22pub use self::impls::EverInitializedPlaces; 8 Engine, GenKill, GenKillAnalysis, Results, ResultsCursor, ResultsRefCursor, ResultsVisitor,
23pub use self::impls::{MaybeBorrowedLocals, MaybeMutBorrowedLocals}; 9};
24pub use self::impls::{MaybeInitializedPlaces, MaybeUninitializedPlaces}; 10pub use self::impls::{
25pub use self::impls::{MaybeRequiresStorage, MaybeStorageLive}; 11 borrows::Borrows, DefinitelyInitializedPlaces, EverInitializedPlaces, MaybeBorrowedLocals,
12 MaybeInitializedPlaces, MaybeMutBorrowedLocals, MaybeRequiresStorage, MaybeStorageLive,
13 MaybeUninitializedPlaces,
14};
26 15
27use self::move_paths::MoveData; 16use self::move_paths::MoveData;
28 17
29mod at_location;
30pub mod drop_flag_effects; 18pub mod drop_flag_effects;
31pub mod generic; 19mod framework;
32mod graphviz;
33mod impls; 20mod impls;
34pub mod move_paths; 21pub mod move_paths;
35 22
@@ -40,74 +27,9 @@ pub(crate) mod indexes {
40 }; 27 };
41} 28}
42 29
43pub(crate) struct DataflowBuilder<'a, 'tcx, BD> 30pub struct MoveDataParamEnv<'tcx> {
44where 31 pub(crate) move_data: MoveData<'tcx>,
45 BD: BitDenotation<'tcx>, 32 pub(crate) param_env: ty::ParamEnv<'tcx>,
46{
47 def_id: DefId,
48 flow_state: DataflowAnalysis<'a, 'tcx, BD>,
49 print_preflow_to: Option<String>,
50 print_postflow_to: Option<String>,
51}
52
53/// `DebugFormatted` encapsulates the "{:?}" rendering of some
54/// arbitrary value. This way: you pay cost of allocating an extra
55/// string (as well as that of rendering up-front); in exchange, you
56/// don't have to hand over ownership of your value or deal with
57/// borrowing it.
58pub struct DebugFormatted(String);
59
60impl DebugFormatted {
61 pub fn new(input: &dyn fmt::Debug) -> DebugFormatted {
62 DebugFormatted(format!("{:?}", input))
63 }
64}
65
66impl fmt::Debug for DebugFormatted {
67 fn fmt(&self, w: &mut fmt::Formatter<'_>) -> fmt::Result {
68 write!(w, "{}", self.0)
69 }
70}
71
72pub trait Dataflow<'tcx, BD: BitDenotation<'tcx>> {
73 /// Sets up and runs the dataflow problem, using `p` to render results if
74 /// implementation so chooses.
75 fn dataflow<P>(&mut self, p: P)
76 where
77 P: Fn(&BD, BD::Idx) -> DebugFormatted,
78 {
79 let _ = p; // default implementation does not instrument process.
80 self.build_sets();
81 self.propagate();
82 }
83
84 /// Sets up the entry, gen, and kill sets for this instance of a dataflow problem.
85 fn build_sets(&mut self);
86
87 /// Finds a fixed-point solution to this instance of a dataflow problem.
88 fn propagate(&mut self);
89}
90
91impl<'a, 'tcx, BD> Dataflow<'tcx, BD> for DataflowBuilder<'a, 'tcx, BD>
92where
93 BD: BitDenotation<'tcx>,
94{
95 fn dataflow<P>(&mut self, p: P)
96 where
97 P: Fn(&BD, BD::Idx) -> DebugFormatted,
98 {
99 self.flow_state.build_sets();
100 self.pre_dataflow_instrumentation(|c, i| p(c, i)).unwrap();
101 self.flow_state.propagate();
102 self.post_dataflow_instrumentation(|c, i| p(c, i)).unwrap();
103 }
104
105 fn build_sets(&mut self) {
106 self.flow_state.build_sets();
107 }
108 fn propagate(&mut self) {
109 self.flow_state.propagate();
110 }
111} 33}
112 34
113pub(crate) fn has_rustc_mir_with(attrs: &[ast::Attribute], name: Symbol) -> Option<MetaItem> { 35pub(crate) fn has_rustc_mir_with(attrs: &[ast::Attribute], name: Symbol) -> Option<MetaItem> {
@@ -124,811 +46,3 @@ pub(crate) fn has_rustc_mir_with(attrs: &[ast::Attribute], name: Symbol) -> Opti
124 } 46 }
125 None 47 None
126} 48}
127
128pub struct MoveDataParamEnv<'tcx> {
129 pub(crate) move_data: MoveData<'tcx>,
130 pub(crate) param_env: ty::ParamEnv<'tcx>,
131}
132
133pub fn do_dataflow<'a, 'tcx, BD, P>(
134 tcx: TyCtxt<'tcx>,
135 body: &'a Body<'tcx>,
136 def_id: DefId,
137 attributes: &[ast::Attribute],
138 dead_unwinds: &BitSet<BasicBlock>,
139 bd: BD,
140 p: P,
141) -> DataflowResults<'tcx, BD>
142where
143 BD: BitDenotation<'tcx>,
144 P: Fn(&BD, BD::Idx) -> DebugFormatted,
145{
146 let flow_state = DataflowAnalysis::new(body, dead_unwinds, bd);
147 flow_state.run(tcx, def_id, attributes, p)
148}
149
150impl<'a, 'tcx, BD> DataflowAnalysis<'a, 'tcx, BD>
151where
152 BD: BitDenotation<'tcx>,
153{
154 pub(crate) fn run<P>(
155 self,
156 tcx: TyCtxt<'tcx>,
157 def_id: DefId,
158 attributes: &[ast::Attribute],
159 p: P,
160 ) -> DataflowResults<'tcx, BD>
161 where
162 P: Fn(&BD, BD::Idx) -> DebugFormatted,
163 {
164 let name_found = |sess: &Session, attrs: &[ast::Attribute], name| -> Option<String> {
165 if let Some(item) = has_rustc_mir_with(attrs, name) {
166 if let Some(s) = item.value_str() {
167 return Some(s.to_string());
168 } else {
169 let path = pprust::path_to_string(&item.path);
170 sess.span_err(item.span, &format!("{} attribute requires a path", path));
171 return None;
172 }
173 }
174 None
175 };
176
177 let print_preflow_to = name_found(tcx.sess, attributes, sym::borrowck_graphviz_preflow);
178 let print_postflow_to = name_found(tcx.sess, attributes, sym::borrowck_graphviz_postflow);
179
180 let mut mbcx =
181 DataflowBuilder { def_id, print_preflow_to, print_postflow_to, flow_state: self };
182
183 mbcx.dataflow(p);
184 mbcx.flow_state.results()
185 }
186}
187
188struct PropagationContext<'b, 'a, 'tcx, O>
189where
190 O: BitDenotation<'tcx>,
191{
192 builder: &'b mut DataflowAnalysis<'a, 'tcx, O>,
193}
194
195impl<'a, 'tcx, BD> DataflowAnalysis<'a, 'tcx, BD>
196where
197 BD: BitDenotation<'tcx>,
198{
199 fn propagate(&mut self) {
200 let mut temp = BitSet::new_empty(self.flow_state.sets.bits_per_block);
201 let mut propcx = PropagationContext { builder: self };
202 propcx.walk_cfg(&mut temp);
203 }
204
205 fn build_sets(&mut self) {
206 // Build the transfer function for each block.
207 for (bb, data) in self.body.basic_blocks().iter_enumerated() {
208 let &mir::BasicBlockData { ref statements, ref terminator, is_cleanup: _ } = data;
209
210 let trans = self.flow_state.sets.trans_mut_for(bb.index());
211 for j_stmt in 0..statements.len() {
212 let location = Location { block: bb, statement_index: j_stmt };
213 self.flow_state.operator.before_statement_effect(trans, location);
214 self.flow_state.operator.statement_effect(trans, location);
215 }
216
217 if terminator.is_some() {
218 let location = Location { block: bb, statement_index: statements.len() };
219 self.flow_state.operator.before_terminator_effect(trans, location);
220 self.flow_state.operator.terminator_effect(trans, location);
221 }
222 }
223
224 // Initialize the flow state at entry to the start block.
225 let on_entry = self.flow_state.sets.entry_set_mut_for(mir::START_BLOCK.index());
226 self.flow_state.operator.start_block_effect(on_entry);
227 }
228}
229
230impl<'b, 'a, 'tcx, BD> PropagationContext<'b, 'a, 'tcx, BD>
231where
232 BD: BitDenotation<'tcx>,
233{
234 fn walk_cfg(&mut self, in_out: &mut BitSet<BD::Idx>) {
235 let body = self.builder.body;
236
237 // Initialize the dirty queue in reverse post-order. This makes it more likely that the
238 // entry state for each basic block will have the effects of its predecessors applied
239 // before it is processed. In fact, for CFGs without back edges, this guarantees that
240 // dataflow will converge in exactly `N` iterations, where `N` is the number of basic
241 // blocks.
242 let mut dirty_queue: WorkQueue<mir::BasicBlock> =
243 WorkQueue::with_none(body.basic_blocks().len());
244 for (bb, _) in traversal::reverse_postorder(body) {
245 dirty_queue.insert(bb);
246 }
247
248 // Add blocks which are not reachable from START_BLOCK to the work queue. These blocks will
249 // be processed after the ones added above.
250 for bb in body.basic_blocks().indices() {
251 dirty_queue.insert(bb);
252 }
253
254 while let Some(bb) = dirty_queue.pop() {
255 let (on_entry, trans) = self.builder.flow_state.sets.get_mut(bb.index());
256 debug_assert!(in_out.words().len() == on_entry.words().len());
257 in_out.overwrite(on_entry);
258 trans.apply(in_out);
259
260 let bb_data = &body[bb];
261 self.builder.propagate_bits_into_graph_successors_of(
262 in_out,
263 (bb, bb_data),
264 &mut dirty_queue,
265 );
266 }
267 }
268}
269
270fn dataflow_path(context: &str, path: &str) -> PathBuf {
271 let mut path = PathBuf::from(path);
272 let new_file_name = {
273 let orig_file_name = path.file_name().unwrap().to_str().unwrap();
274 format!("{}_{}", context, orig_file_name)
275 };
276 path.set_file_name(new_file_name);
277 path
278}
279
280impl<'a, 'tcx, BD> DataflowBuilder<'a, 'tcx, BD>
281where
282 BD: BitDenotation<'tcx>,
283{
284 fn pre_dataflow_instrumentation<P>(&self, p: P) -> io::Result<()>
285 where
286 P: Fn(&BD, BD::Idx) -> DebugFormatted,
287 {
288 if let Some(ref path_str) = self.print_preflow_to {
289 let path = dataflow_path(BD::name(), path_str);
290 graphviz::print_borrowck_graph_to(self, &path, p)
291 } else {
292 Ok(())
293 }
294 }
295
296 fn post_dataflow_instrumentation<P>(&self, p: P) -> io::Result<()>
297 where
298 P: Fn(&BD, BD::Idx) -> DebugFormatted,
299 {
300 if let Some(ref path_str) = self.print_postflow_to {
301 let path = dataflow_path(BD::name(), path_str);
302 graphviz::print_borrowck_graph_to(self, &path, p)
303 } else {
304 Ok(())
305 }
306 }
307}
308
309/// DataflowResultsConsumer abstracts over walking the MIR with some
310/// already constructed dataflow results.
311///
312/// It abstracts over the FlowState and also completely hides the
313/// underlying flow analysis results, because it needs to handle cases
314/// where we are combining the results of *multiple* flow analyses
315/// (e.g., borrows + inits + uninits).
316pub(crate) trait DataflowResultsConsumer<'a, 'tcx: 'a> {
317 type FlowState: FlowsAtLocation;
318
319 // Observation Hooks: override (at least one of) these to get analysis feedback.
320 fn visit_block_entry(&mut self, _bb: BasicBlock, _flow_state: &Self::FlowState) {}
321
322 fn visit_statement_entry(
323 &mut self,
324 _loc: Location,
325 _stmt: &'a Statement<'tcx>,
326 _flow_state: &Self::FlowState,
327 ) {
328 }
329
330 fn visit_terminator_entry(
331 &mut self,
332 _loc: Location,
333 _term: &'a Terminator<'tcx>,
334 _flow_state: &Self::FlowState,
335 ) {
336 }
337
338 // Main entry point: this drives the processing of results.
339
340 fn analyze_results(&mut self, flow_uninit: &mut Self::FlowState) {
341 let flow = flow_uninit;
342 for (bb, _) in traversal::reverse_postorder(self.body()) {
343 flow.reset_to_entry_of(bb);
344 self.process_basic_block(bb, flow);
345 }
346 }
347
348 fn process_basic_block(&mut self, bb: BasicBlock, flow_state: &mut Self::FlowState) {
349 self.visit_block_entry(bb, flow_state);
350
351 let BasicBlockData { ref statements, ref terminator, is_cleanup: _ } = self.body()[bb];
352 let mut location = Location { block: bb, statement_index: 0 };
353 for stmt in statements.iter() {
354 flow_state.reconstruct_statement_effect(location);
355 self.visit_statement_entry(location, stmt, flow_state);
356 flow_state.apply_local_effect(location);
357 location.statement_index += 1;
358 }
359
360 if let Some(ref term) = *terminator {
361 flow_state.reconstruct_terminator_effect(location);
362 self.visit_terminator_entry(location, term, flow_state);
363
364 // We don't need to apply the effect of the terminator,
365 // since we are only visiting dataflow state on control
366 // flow entry to the various nodes. (But we still need to
367 // reconstruct the effect, because the visit method might
368 // inspect it.)
369 }
370 }
371
372 // Delegated Hooks: Provide access to the MIR and process the flow state.
373
374 fn body(&self) -> &'a Body<'tcx>;
375}
376
377/// Allows iterating dataflow results in a flexible and reasonably fast way.
378pub struct DataflowResultsCursor<'mir, 'tcx, BD, DR = DataflowResults<'tcx, BD>>
379where
380 BD: BitDenotation<'tcx>,
381 DR: Borrow<DataflowResults<'tcx, BD>>,
382{
383 flow_state: FlowAtLocation<'tcx, BD, DR>,
384
385 // The statement (or terminator) whose effect has been reconstructed in
386 // flow_state.
387 curr_loc: Option<Location>,
388
389 body: &'mir Body<'tcx>,
390}
391
392pub type DataflowResultsRefCursor<'mir, 'tcx, BD> =
393 DataflowResultsCursor<'mir, 'tcx, BD, &'mir DataflowResults<'tcx, BD>>;
394
395impl<'mir, 'tcx, BD, DR> DataflowResultsCursor<'mir, 'tcx, BD, DR>
396where
397 BD: BitDenotation<'tcx>,
398 DR: Borrow<DataflowResults<'tcx, BD>>,
399{
400 pub fn new(result: DR, body: &'mir Body<'tcx>) -> Self {
401 DataflowResultsCursor { flow_state: FlowAtLocation::new(result), curr_loc: None, body }
402 }
403
404 /// Seek to the given location in MIR. This method is fast if you are
405 /// traversing your MIR statements in order.
406 ///
407 /// After calling `seek`, the current state will reflect all effects up to
408 /// and including the `before_statement_effect` of the statement at location
409 /// `loc`. The `statement_effect` of the statement at `loc` will be
410 /// available as the current effect (see e.g. `each_gen_bit`).
411 ///
412 /// If `loc.statement_index` equals the number of statements in the block,
413 /// we will reconstruct the terminator effect in the same way as described
414 /// above.
415 pub fn seek(&mut self, loc: Location) {
416 if self.curr_loc.map(|cur| loc == cur).unwrap_or(false) {
417 return;
418 }
419
420 let start_index;
421 let should_reset = match self.curr_loc {
422 None => true,
423 Some(cur) if loc.block != cur.block || loc.statement_index < cur.statement_index => {
424 true
425 }
426 _ => false,
427 };
428 if should_reset {
429 self.flow_state.reset_to_entry_of(loc.block);
430 start_index = 0;
431 } else {
432 let curr_loc = self.curr_loc.unwrap();
433 start_index = curr_loc.statement_index;
434 // Apply the effect from the last seek to the current state.
435 self.flow_state.apply_local_effect(curr_loc);
436 }
437
438 for stmt in start_index..loc.statement_index {
439 let mut stmt_loc = loc;
440 stmt_loc.statement_index = stmt;
441 self.flow_state.reconstruct_statement_effect(stmt_loc);
442 self.flow_state.apply_local_effect(stmt_loc);
443 }
444
445 if loc.statement_index == self.body[loc.block].statements.len() {
446 self.flow_state.reconstruct_terminator_effect(loc);
447 } else {
448 self.flow_state.reconstruct_statement_effect(loc);
449 }
450 self.curr_loc = Some(loc);
451 }
452
453 /// Return whether the current state contains bit `x`.
454 pub fn contains(&self, x: BD::Idx) -> bool {
455 self.flow_state.contains(x)
456 }
457
458 /// Iterate over each `gen` bit in the current effect (invoke `seek` first).
459 pub fn each_gen_bit<F>(&self, f: F)
460 where
461 F: FnMut(BD::Idx),
462 {
463 self.flow_state.each_gen_bit(f)
464 }
465
466 pub fn get(&self) -> &BitSet<BD::Idx> {
467 self.flow_state.as_dense()
468 }
469}
470
471pub struct DataflowAnalysis<'a, 'tcx, O>
472where
473 O: BitDenotation<'tcx>,
474{
475 flow_state: DataflowState<'tcx, O>,
476 dead_unwinds: &'a BitSet<mir::BasicBlock>,
477 body: &'a Body<'tcx>,
478}
479
480impl<'a, 'tcx, O> DataflowAnalysis<'a, 'tcx, O>
481where
482 O: BitDenotation<'tcx>,
483{
484 pub fn results(self) -> DataflowResults<'tcx, O> {
485 DataflowResults(self.flow_state)
486 }
487
488 pub fn body(&self) -> &'a Body<'tcx> {
489 self.body
490 }
491}
492
493pub struct DataflowResults<'tcx, O>(pub(crate) DataflowState<'tcx, O>)
494where
495 O: BitDenotation<'tcx>;
496
497impl<'tcx, O: BitDenotation<'tcx>> DataflowResults<'tcx, O> {
498 pub fn sets(&self) -> &AllSets<O::Idx> {
499 &self.0.sets
500 }
501
502 pub fn operator(&self) -> &O {
503 &self.0.operator
504 }
505}
506
507/// State of a dataflow analysis; couples a collection of bit sets
508/// with operator used to initialize and merge bits during analysis.
509pub struct DataflowState<'tcx, O: BitDenotation<'tcx>> {
510 /// All the sets for the analysis. (Factored into its
511 /// own structure so that we can borrow it mutably
512 /// on its own separate from other fields.)
513 pub sets: AllSets<O::Idx>,
514
515 /// operator used to initialize, combine, and interpret bits.
516 pub(crate) operator: O,
517}
518
519impl<'tcx, O: BitDenotation<'tcx>> DataflowState<'tcx, O> {
520 pub(crate) fn interpret_set<'c, P>(
521 &self,
522 o: &'c O,
523 set: &BitSet<O::Idx>,
524 render_idx: &P,
525 ) -> Vec<DebugFormatted>
526 where
527 P: Fn(&O, O::Idx) -> DebugFormatted,
528 {
529 set.iter().map(|i| render_idx(o, i)).collect()
530 }
531
532 pub(crate) fn interpret_hybrid_set<'c, P>(
533 &self,
534 o: &'c O,
535 set: &HybridBitSet<O::Idx>,
536 render_idx: &P,
537 ) -> Vec<DebugFormatted>
538 where
539 P: Fn(&O, O::Idx) -> DebugFormatted,
540 {
541 set.iter().map(|i| render_idx(o, i)).collect()
542 }
543}
544
545/// A 2-tuple representing the "gen" and "kill" bitsets during
546/// dataflow analysis.
547///
548/// It is best to ensure that the intersection of `gen_set` and
549/// `kill_set` is empty; otherwise the results of the dataflow will
550/// have a hidden dependency on what order the bits are generated and
551/// killed during the iteration. (This is such a good idea that the
552/// `fn gen` and `fn kill` methods that set their state enforce this
553/// for you.)
554#[derive(Debug, Clone, Copy)]
555pub struct GenKill<T> {
556 pub(crate) gen_set: T,
557 pub(crate) kill_set: T,
558}
559
560pub type GenKillSet<T> = GenKill<HybridBitSet<T>>;
561
562impl<T> GenKill<T> {
563 /// Creates a new tuple where `gen_set == kill_set == elem`.
564 pub(crate) fn from_elem(elem: T) -> Self
565 where
566 T: Clone,
567 {
568 GenKill { gen_set: elem.clone(), kill_set: elem }
569 }
570}
571
572impl<E: Idx> GenKillSet<E> {
573 pub fn clear(&mut self) {
574 self.gen_set.clear();
575 self.kill_set.clear();
576 }
577
578 pub fn gen(&mut self, e: E) {
579 self.gen_set.insert(e);
580 self.kill_set.remove(e);
581 }
582
583 pub fn gen_all(&mut self, i: impl IntoIterator<Item: Borrow<E>>) {
584 for j in i {
585 self.gen(*j.borrow());
586 }
587 }
588
589 pub fn kill(&mut self, e: E) {
590 self.gen_set.remove(e);
591 self.kill_set.insert(e);
592 }
593
594 pub fn kill_all(&mut self, i: impl IntoIterator<Item: Borrow<E>>) {
595 for j in i {
596 self.kill(*j.borrow());
597 }
598 }
599
600 /// Computes `(set ∪ gen) - kill` and assigns the result to `set`.
601 pub(crate) fn apply(&self, set: &mut BitSet<E>) {
602 set.union(&self.gen_set);
603 set.subtract(&self.kill_set);
604 }
605}
606
607#[derive(Debug)]
608pub struct AllSets<E: Idx> {
609 /// Analysis bitwidth for each block.
610 bits_per_block: usize,
611
612 /// For each block, bits valid on entry to the block.
613 on_entry: Vec<BitSet<E>>,
614
615 /// The transfer function of each block expressed as the set of bits
616 /// generated and killed by executing the statements + terminator in the
617 /// block -- with one caveat. In particular, for *call terminators*, the
618 /// effect of storing the destination is not included, since that only takes
619 /// effect on the **success** edge (and not the unwind edge).
620 trans: Vec<GenKillSet<E>>,
621}
622
623impl<E: Idx> AllSets<E> {
624 pub fn bits_per_block(&self) -> usize {
625 self.bits_per_block
626 }
627
628 pub fn get_mut(&mut self, block_idx: usize) -> (&mut BitSet<E>, &mut GenKillSet<E>) {
629 (&mut self.on_entry[block_idx], &mut self.trans[block_idx])
630 }
631
632 pub fn trans_for(&self, block_idx: usize) -> &GenKillSet<E> {
633 &self.trans[block_idx]
634 }
635 pub fn trans_mut_for(&mut self, block_idx: usize) -> &mut GenKillSet<E> {
636 &mut self.trans[block_idx]
637 }
638 pub fn entry_set_for(&self, block_idx: usize) -> &BitSet<E> {
639 &self.on_entry[block_idx]
640 }
641 pub fn entry_set_mut_for(&mut self, block_idx: usize) -> &mut BitSet<E> {
642 &mut self.on_entry[block_idx]
643 }
644 pub fn gen_set_for(&self, block_idx: usize) -> &HybridBitSet<E> {
645 &self.trans_for(block_idx).gen_set
646 }
647 pub fn kill_set_for(&self, block_idx: usize) -> &HybridBitSet<E> {
648 &self.trans_for(block_idx).kill_set
649 }
650}
651
652/// Parameterization for the precise form of data flow that is used.
653///
654/// `BottomValue` determines whether the initial entry set for each basic block is empty or full.
655/// This also determines the semantics of the lattice `join` operator used to merge dataflow
656/// results, since dataflow works by starting at the bottom and moving monotonically to a fixed
657/// point.
658///
659/// This means, for propagation across the graph, that you either want to start at all-zeroes and
660/// then use Union as your merge when propagating, or you start at all-ones and then use Intersect
661/// as your merge when propagating.
662pub trait BottomValue {
663 /// Specifies the initial value for each bit in the entry set for each basic block.
664 const BOTTOM_VALUE: bool;
665
666 /// Merges `in_set` into `inout_set`, returning `true` if `inout_set` changed.
667 ///
668 /// It is almost certainly wrong to override this, since it automatically applies
669 /// * `inout_set & in_set` if `BOTTOM_VALUE == true`
670 /// * `inout_set | in_set` if `BOTTOM_VALUE == false`
671 ///
672 /// This means that if a bit is not `BOTTOM_VALUE`, it is propagated into all target blocks.
673 /// For clarity, the above statement again from a different perspective:
674 /// A bit in the block's entry set is `!BOTTOM_VALUE` if *any* predecessor block's bit value is
675 /// `!BOTTOM_VALUE`.
676 ///
677 /// There are situations where you want the opposite behaviour: propagate only if *all*
678 /// predecessor blocks's value is `!BOTTOM_VALUE`.
679 /// E.g. if you want to know whether a bit is *definitely* set at a specific location. This
680 /// means that all code paths leading to the location must have set the bit, instead of any
681 /// code path leading there.
682 ///
683 /// If you want this kind of "definitely set" analysis, you need to
684 /// 1. Invert `BOTTOM_VALUE`
685 /// 2. Reset the `entry_set` in `start_block_effect` to `!BOTTOM_VALUE`
686 /// 3. Override `join` to do the opposite from what it's doing now.
687 #[inline]
688 fn join<T: Idx>(&self, inout_set: &mut BitSet<T>, in_set: &BitSet<T>) -> bool {
689 if !Self::BOTTOM_VALUE { inout_set.union(in_set) } else { inout_set.intersect(in_set) }
690 }
691}
692
693/// A specific flavor of dataflow analysis.
694///
695/// To run a dataflow analysis, one sets up an initial state for the
696/// `START_BLOCK` via `start_block_effect` and a transfer function (`trans`)
697/// for each block individually. The entry set for all other basic blocks is
698/// initialized to `Self::BOTTOM_VALUE`. The dataflow analysis then
699/// iteratively modifies the various entry sets (but leaves the the transfer
700/// function unchanged). `BottomValue::join` is used to merge the bitsets from
701/// two blocks (e.g. when two blocks' terminator jumps to a single block, that
702/// target block's state is the merged state of both incoming blocks).
703pub trait BitDenotation<'tcx>: BottomValue {
704 /// Specifies what index type is used to access the bitvector.
705 type Idx: Idx;
706
707 /// A name describing the dataflow analysis that this
708 /// `BitDenotation` is supporting. The name should be something
709 /// suitable for plugging in as part of a filename (i.e., avoid
710 /// space-characters or other things that tend to look bad on a
711 /// file system, like slashes or periods). It is also better for
712 /// the name to be reasonably short, again because it will be
713 /// plugged into a filename.
714 fn name() -> &'static str;
715
716 /// Size of each bitvector allocated for each block in the analysis.
717 fn bits_per_block(&self) -> usize;
718
719 /// Mutates the entry set according to the effects that
720 /// have been established *prior* to entering the start
721 /// block. This can't access the gen/kill sets, because
722 /// these won't be accounted for correctly.
723 ///
724 /// (For example, establishing the call arguments.)
725 fn start_block_effect(&self, entry_set: &mut BitSet<Self::Idx>);
726
727 /// Similar to `statement_effect`, except it applies
728 /// *just before* the statement rather than *just after* it.
729 ///
730 /// This matters for "dataflow at location" APIs, because the
731 /// before-statement effect is visible while visiting the
732 /// statement, while the after-statement effect only becomes
733 /// visible at the next statement.
734 ///
735 /// Both the before-statement and after-statement effects are
736 /// applied, in that order, before moving for the next
737 /// statement.
738 fn before_statement_effect(&self, _trans: &mut GenKillSet<Self::Idx>, _location: Location) {}
739
740 /// Mutates the block-sets (the flow sets for the given
741 /// basic block) according to the effects of evaluating statement.
742 ///
743 /// This is used, in particular, for building up the
744 /// "transfer-function" representing the overall-effect of the
745 /// block, represented via GEN and KILL sets.
746 ///
747 /// The statement is identified as `bb_data[idx_stmt]`, where
748 /// `bb_data` is the sequence of statements identified by `bb` in
749 /// the MIR.
750 fn statement_effect(&self, trans: &mut GenKillSet<Self::Idx>, location: Location);
751
752 /// Similar to `terminator_effect`, except it applies
753 /// *just before* the terminator rather than *just after* it.
754 ///
755 /// This matters for "dataflow at location" APIs, because the
756 /// before-terminator effect is visible while visiting the
757 /// terminator, while the after-terminator effect only becomes
758 /// visible at the terminator's successors.
759 ///
760 /// Both the before-terminator and after-terminator effects are
761 /// applied, in that order, before moving for the next
762 /// terminator.
763 fn before_terminator_effect(&self, _trans: &mut GenKillSet<Self::Idx>, _location: Location) {}
764
765 /// Mutates the block-sets (the flow sets for the given
766 /// basic block) according to the effects of evaluating
767 /// the terminator.
768 ///
769 /// This is used, in particular, for building up the
770 /// "transfer-function" representing the overall-effect of the
771 /// block, represented via GEN and KILL sets.
772 ///
773 /// The effects applied here cannot depend on which branch the
774 /// terminator took.
775 fn terminator_effect(&self, trans: &mut GenKillSet<Self::Idx>, location: Location);
776
777 /// Mutates the block-sets according to the (flow-dependent)
778 /// effect of a successful return from a Call terminator.
779 ///
780 /// If basic-block BB_x ends with a call-instruction that, upon
781 /// successful return, flows to BB_y, then this method will be
782 /// called on the exit flow-state of BB_x in order to set up the
783 /// entry flow-state of BB_y.
784 ///
785 /// This is used, in particular, as a special case during the
786 /// "propagate" loop where all of the basic blocks are repeatedly
787 /// visited. Since the effects of a Call terminator are
788 /// flow-dependent, the current MIR cannot encode them via just
789 /// GEN and KILL sets attached to the block, and so instead we add
790 /// this extra machinery to represent the flow-dependent effect.
791 //
792 // FIXME: right now this is a bit of a wart in the API. It might
793 // be better to represent this as an additional gen- and
794 // kill-sets associated with each edge coming out of the basic
795 // block.
796 fn propagate_call_return(
797 &self,
798 in_out: &mut BitSet<Self::Idx>,
799 call_bb: mir::BasicBlock,
800 dest_bb: mir::BasicBlock,
801 dest_place: &mir::Place<'tcx>,
802 );
803}
804
805impl<'a, 'tcx, D> DataflowAnalysis<'a, 'tcx, D>
806where
807 D: BitDenotation<'tcx>,
808{
809 pub fn new(
810 body: &'a Body<'tcx>,
811 dead_unwinds: &'a BitSet<mir::BasicBlock>,
812 denotation: D,
813 ) -> Self {
814 let bits_per_block = denotation.bits_per_block();
815 let num_blocks = body.basic_blocks().len();
816
817 let on_entry = if D::BOTTOM_VALUE {
818 vec![BitSet::new_filled(bits_per_block); num_blocks]
819 } else {
820 vec![BitSet::new_empty(bits_per_block); num_blocks]
821 };
822 let nop = GenKill::from_elem(HybridBitSet::new_empty(bits_per_block));
823
824 DataflowAnalysis {
825 body,
826 dead_unwinds,
827 flow_state: DataflowState {
828 sets: AllSets { bits_per_block, on_entry, trans: vec![nop; num_blocks] },
829 operator: denotation,
830 },
831 }
832 }
833}
834
835impl<'a, 'tcx, D> DataflowAnalysis<'a, 'tcx, D>
836where
837 D: BitDenotation<'tcx>,
838{
839 /// Propagates the bits of `in_out` into all the successors of `bb`,
840 /// using bitwise operator denoted by `self.operator`.
841 ///
842 /// For most blocks, this is entirely uniform. However, for blocks
843 /// that end with a call terminator, the effect of the call on the
844 /// dataflow state may depend on whether the call returned
845 /// successfully or unwound.
846 ///
847 /// To reflect this, the `propagate_call_return` method of the
848 /// `BitDenotation` mutates `in_out` when propagating `in_out` via
849 /// a call terminator; such mutation is performed *last*, to
850 /// ensure its side-effects do not leak elsewhere (e.g., into
851 /// unwind target).
852 fn propagate_bits_into_graph_successors_of(
853 &mut self,
854 in_out: &mut BitSet<D::Idx>,
855 (bb, bb_data): (mir::BasicBlock, &mir::BasicBlockData<'tcx>),
856 dirty_list: &mut WorkQueue<mir::BasicBlock>,
857 ) {
858 match bb_data.terminator().kind {
859 mir::TerminatorKind::Return
860 | mir::TerminatorKind::Resume
861 | mir::TerminatorKind::Abort
862 | mir::TerminatorKind::GeneratorDrop
863 | mir::TerminatorKind::Unreachable => {}
864 mir::TerminatorKind::Goto { target }
865 | mir::TerminatorKind::Assert { target, cleanup: None, .. }
866 | mir::TerminatorKind::Yield { resume: target, drop: None, .. }
867 | mir::TerminatorKind::Drop { target, location: _, unwind: None }
868 | mir::TerminatorKind::DropAndReplace { target, value: _, location: _, unwind: None } =>
869 {
870 self.propagate_bits_into_entry_set_for(in_out, target, dirty_list);
871 }
872 mir::TerminatorKind::Yield { resume: target, drop: Some(drop), .. } => {
873 self.propagate_bits_into_entry_set_for(in_out, target, dirty_list);
874 self.propagate_bits_into_entry_set_for(in_out, drop, dirty_list);
875 }
876 mir::TerminatorKind::Assert { target, cleanup: Some(unwind), .. }
877 | mir::TerminatorKind::Drop { target, location: _, unwind: Some(unwind) }
878 | mir::TerminatorKind::DropAndReplace {
879 target,
880 value: _,
881 location: _,
882 unwind: Some(unwind),
883 } => {
884 self.propagate_bits_into_entry_set_for(in_out, target, dirty_list);
885 if !self.dead_unwinds.contains(bb) {
886 self.propagate_bits_into_entry_set_for(in_out, unwind, dirty_list);
887 }
888 }
889 mir::TerminatorKind::SwitchInt { ref targets, .. } => {
890 for target in targets {
891 self.propagate_bits_into_entry_set_for(in_out, *target, dirty_list);
892 }
893 }
894 mir::TerminatorKind::Call { cleanup, ref destination, .. } => {
895 if let Some(unwind) = cleanup {
896 if !self.dead_unwinds.contains(bb) {
897 self.propagate_bits_into_entry_set_for(in_out, unwind, dirty_list);
898 }
899 }
900 if let Some((ref dest_place, dest_bb)) = *destination {
901 // N.B.: This must be done *last*, after all other
902 // propagation, as documented in comment above.
903 self.flow_state.operator.propagate_call_return(in_out, bb, dest_bb, dest_place);
904 self.propagate_bits_into_entry_set_for(in_out, dest_bb, dirty_list);
905 }
906 }
907 mir::TerminatorKind::FalseEdges { real_target, imaginary_target } => {
908 self.propagate_bits_into_entry_set_for(in_out, real_target, dirty_list);
909 self.propagate_bits_into_entry_set_for(in_out, imaginary_target, dirty_list);
910 }
911 mir::TerminatorKind::FalseUnwind { real_target, unwind } => {
912 self.propagate_bits_into_entry_set_for(in_out, real_target, dirty_list);
913 if let Some(unwind) = unwind {
914 if !self.dead_unwinds.contains(bb) {
915 self.propagate_bits_into_entry_set_for(in_out, unwind, dirty_list);
916 }
917 }
918 }
919 }
920 }
921
922 fn propagate_bits_into_entry_set_for(
923 &mut self,
924 in_out: &BitSet<D::Idx>,
925 bb: mir::BasicBlock,
926 dirty_queue: &mut WorkQueue<mir::BasicBlock>,
927 ) {
928 let entry_set = self.flow_state.sets.entry_set_mut_for(bb.index());
929 let set_changed = self.flow_state.operator.join(entry_set, &in_out);
930 if set_changed {
931 dirty_queue.insert(bb);
932 }
933 }
934}
diff --git a/src/librustc_mir/transform/check_consts/resolver.rs b/src/librustc_mir/transform/check_consts/resolver.rs
index e42f64b..5f761ce 100644
--- a/src/librustc_mir/transform/check_consts/resolver.rs
+++ b/src/librustc_mir/transform/check_consts/resolver.rs
@@ -9,7 +9,7 @@ use rustc_index::bit_set::BitSet;
9use std::marker::PhantomData; 9use std::marker::PhantomData;
10 10
11use super::{qualifs, Item, Qualif}; 11use super::{qualifs, Item, Qualif};
12use crate::dataflow::{self as old_dataflow, generic as dataflow}; 12use crate::dataflow;
13 13
14/// A `Visitor` that propagates qualifs between locals. This defines the transfer function of 14/// A `Visitor` that propagates qualifs between locals. This defines the transfer function of
15/// `FlowSensitiveAnalysis`. 15/// `FlowSensitiveAnalysis`.
@@ -165,7 +165,7 @@ where
165 } 165 }
166} 166}
167 167
168impl<Q> old_dataflow::BottomValue for FlowSensitiveAnalysis<'_, '_, '_, Q> { 168impl<Q> dataflow::BottomValue for FlowSensitiveAnalysis<'_, '_, '_, Q> {
169 const BOTTOM_VALUE: bool = false; 169 const BOTTOM_VALUE: bool = false;
170} 170}
171 171
diff --git a/src/librustc_mir/transform/check_consts/validation.rs b/src/librustc_mir/transform/check_consts/validation.rs
index be461c0..7f4714e 100644
--- a/src/librustc_mir/transform/check_consts/validation.rs
+++ b/src/librustc_mir/transform/check_consts/validation.rs
@@ -22,8 +22,8 @@ use super::qualifs::{self, HasMutInterior, NeedsDrop};
22use super::resolver::FlowSensitiveAnalysis; 22use super::resolver::FlowSensitiveAnalysis;
23use super::{is_lang_panic_fn, ConstKind, Item, Qualif}; 23use super::{is_lang_panic_fn, ConstKind, Item, Qualif};
24use crate::const_eval::{is_const_fn, is_unstable_const_fn}; 24use crate::const_eval::{is_const_fn, is_unstable_const_fn};
25use crate::dataflow::generic::{self as dataflow, Analysis};
26use crate::dataflow::MaybeMutBorrowedLocals; 25use crate::dataflow::MaybeMutBorrowedLocals;
26use crate::dataflow::{self, Analysis};
27 27
28// We are using `MaybeMutBorrowedLocals` as a proxy for whether an item may have been mutated 28// We are using `MaybeMutBorrowedLocals` as a proxy for whether an item may have been mutated
29// through a pointer prior to the given point. This is okay even though `MaybeMutBorrowedLocals` 29// through a pointer prior to the given point. This is okay even though `MaybeMutBorrowedLocals`
diff --git a/src/librustc_mir/transform/elaborate_drops.rs b/src/librustc_mir/transform/elaborate_drops.rs
index 5d02074..4ec4ef0 100644
--- a/src/librustc_mir/transform/elaborate_drops.rs
+++ b/src/librustc_mir/transform/elaborate_drops.rs
@@ -1,9 +1,9 @@
1use crate::dataflow; 1use crate::dataflow;
2use crate::dataflow::generic::{Analysis, ResultsCursor};
3use crate::dataflow::move_paths::{LookupResult, MoveData, MovePathIndex}; 2use crate::dataflow::move_paths::{LookupResult, MoveData, MovePathIndex};
4use crate::dataflow::on_lookup_result_bits; 3use crate::dataflow::on_lookup_result_bits;
5use crate::dataflow::MoveDataParamEnv; 4use crate::dataflow::MoveDataParamEnv;
6use crate::dataflow::{on_all_children_bits, on_all_drop_children_bits}; 5use crate::dataflow::{on_all_children_bits, on_all_drop_children_bits};
6use crate::dataflow::{Analysis, ResultsCursor};
7use crate::dataflow::{MaybeInitializedPlaces, MaybeUninitializedPlaces}; 7use crate::dataflow::{MaybeInitializedPlaces, MaybeUninitializedPlaces};
8use crate::transform::{MirPass, MirSource}; 8use crate::transform::{MirPass, MirSource};
9use crate::util::elaborate_drops::{elaborate_drop, DropFlagState, Unwind}; 9use crate::util::elaborate_drops::{elaborate_drop, DropFlagState, Unwind};
diff --git a/src/librustc_mir/transform/generator.rs b/src/librustc_mir/transform/generator.rs
index 349cda8..1945efb 100644
--- a/src/librustc_mir/transform/generator.rs
+++ b/src/librustc_mir/transform/generator.rs
@@ -49,7 +49,7 @@
49//! For generators with state 1 (returned) and state 2 (poisoned) it does nothing. 49//! For generators with state 1 (returned) and state 2 (poisoned) it does nothing.
50//! Otherwise it drops all the values in scope at the last suspension point. 50//! Otherwise it drops all the values in scope at the last suspension point.
51 51
52use crate::dataflow::generic::{self as dataflow, Analysis}; 52use crate::dataflow::{self, Analysis};
53use crate::dataflow::{MaybeBorrowedLocals, MaybeRequiresStorage, MaybeStorageLive}; 53use crate::dataflow::{MaybeBorrowedLocals, MaybeRequiresStorage, MaybeStorageLive};
54use crate::transform::no_landing_pads::no_landing_pads; 54use crate::transform::no_landing_pads::no_landing_pads;
55use crate::transform::simplify; 55use crate::transform::simplify;
diff --git a/src/librustc_mir/transform/rustc_peek.rs b/src/librustc_mir/transform/rustc_peek.rs
index 22ac341..c9a0016 100644
--- a/src/librustc_mir/transform/rustc_peek.rs
+++ b/src/librustc_mir/transform/rustc_peek.rs
@@ -9,11 +9,11 @@ use rustc::ty::{self, Ty, TyCtxt};
9use rustc_hir::def_id::DefId; 9use rustc_hir::def_id::DefId;
10use rustc_index::bit_set::BitSet; 10use rustc_index::bit_set::BitSet;
11 11
12use crate::dataflow::generic::{Analysis, Results, ResultsCursor};
13use crate::dataflow::move_paths::{HasMoveData, MoveData}; 12use crate::dataflow::move_paths::{HasMoveData, MoveData};
14use crate::dataflow::move_paths::{LookupResult, MovePathIndex}; 13use crate::dataflow::move_paths::{LookupResult, MovePathIndex};
15use crate::dataflow::MaybeMutBorrowedLocals; 14use crate::dataflow::MaybeMutBorrowedLocals;
16use crate::dataflow::MoveDataParamEnv; 15use crate::dataflow::MoveDataParamEnv;
16use crate::dataflow::{Analysis, Results, ResultsCursor};
17use crate::dataflow::{ 17use crate::dataflow::{
18 DefinitelyInitializedPlaces, MaybeInitializedPlaces, MaybeUninitializedPlaces, 18 DefinitelyInitializedPlaces, MaybeInitializedPlaces, MaybeUninitializedPlaces,
19}; 19};
diff --git a/src/librustc_passes/liveness.rs b/src/librustc_passes/liveness.rs
index 97f6457..e729c2d 100644
--- a/src/librustc_passes/liveness.rs
+++ b/src/librustc_passes/liveness.rs
@@ -903,10 +903,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
903 } 903 }
904 904
905 fn compute(&mut self, body: &hir::Expr<'_>) -> LiveNode { 905 fn compute(&mut self, body: &hir::Expr<'_>) -> LiveNode {
906 debug!( 906 debug!("compute: using id for body, {:?}", body);
907 "compute: using id for body, {}",
908 self.ir.tcx.hir().hir_to_pretty_string(body.hir_id)
909 );
910 907
911 // the fallthrough exit is only for those cases where we do not 908 // the fallthrough exit is only for those cases where we do not
912 // explicitly return: 909 // explicitly return:
@@ -979,7 +976,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
979 } 976 }
980 977
981 fn propagate_through_expr(&mut self, expr: &Expr<'_>, succ: LiveNode) -> LiveNode { 978 fn propagate_through_expr(&mut self, expr: &Expr<'_>, succ: LiveNode) -> LiveNode {
982 debug!("propagate_through_expr: {}", self.ir.tcx.hir().hir_to_pretty_string(expr.hir_id)); 979 debug!("propagate_through_expr: {:?}", expr);
983 980
984 match expr.kind { 981 match expr.kind {
985 // Interesting cases with control flow or which gen/kill 982 // Interesting cases with control flow or which gen/kill
@@ -990,10 +987,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
990 hir::ExprKind::Field(ref e, _) => self.propagate_through_expr(&e, succ), 987 hir::ExprKind::Field(ref e, _) => self.propagate_through_expr(&e, succ),
991 988
992 hir::ExprKind::Closure(..) => { 989 hir::ExprKind::Closure(..) => {
993 debug!( 990 debug!("{:?} is an ExprKind::Closure", expr);
994 "{} is an ExprKind::Closure",
995 self.ir.tcx.hir().hir_to_pretty_string(expr.hir_id)
996 );
997 991
998 // the construction of a closure itself is not important, 992 // the construction of a closure itself is not important,
999 // but we have to consider the closed over variables. 993 // but we have to consider the closed over variables.
@@ -1344,11 +1338,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
1344 let mut first_merge = true; 1338 let mut first_merge = true;
1345 let ln = self.live_node(expr.hir_id, expr.span); 1339 let ln = self.live_node(expr.hir_id, expr.span);
1346 self.init_empty(ln, succ); 1340 self.init_empty(ln, succ);
1347 debug!( 1341 debug!("propagate_through_loop: using id for loop body {} {:?}", expr.hir_id, body);
1348 "propagate_through_loop: using id for loop body {} {}",
1349 expr.hir_id,
1350 self.ir.tcx.hir().hir_to_pretty_string(body.hir_id)
1351 );
1352 1342
1353 self.break_ln.insert(expr.hir_id, succ); 1343 self.break_ln.insert(expr.hir_id, succ);
1354 1344
diff --git a/src/librustc_privacy/lib.rs b/src/librustc_privacy/lib.rs
index 8734cee..0df1d08 100644
--- a/src/librustc_privacy/lib.rs
+++ b/src/librustc_privacy/lib.rs
@@ -1320,14 +1320,18 @@ impl<'a, 'tcx> Visitor<'tcx> for TypePrivacyVisitor<'a, 'tcx> {
1320 let is_local_static = 1320 let is_local_static =
1321 if let DefKind::Static = kind { def_id.is_local() } else { false }; 1321 if let DefKind::Static = kind { def_id.is_local() } else { false };
1322 if !self.item_is_accessible(def_id) && !is_local_static { 1322 if !self.item_is_accessible(def_id) && !is_local_static {
1323 let name = match *qpath { 1323 let sess = self.tcx.sess;
1324 hir::QPath::Resolved(_, ref path) => path.to_string(), 1324 let sm = sess.source_map();
1325 hir::QPath::TypeRelative(_, ref segment) => segment.ident.to_string(), 1325 let name = match qpath {
1326 hir::QPath::Resolved(_, path) => sm.span_to_snippet(path.span).ok(),
1327 hir::QPath::TypeRelative(_, segment) => Some(segment.ident.to_string()),
1326 }; 1328 };
1327 let kind = kind.descr(def_id); 1329 let kind = kind.descr(def_id);
1328 self.tcx 1330 let msg = match name {
1329 .sess 1331 Some(name) => format!("{} `{}` is private", kind, name),
1330 .struct_span_err(span, &format!("{} `{}` is private", kind, name)) 1332 None => format!("{} is private", kind),
1333 };
1334 sess.struct_span_err(span, &msg)
1331 .span_label(span, &format!("private {}", kind)) 1335 .span_label(span, &format!("private {}", kind))
1332 .emit(); 1336 .emit();
1333 return; 1337 return;
diff --git a/src/librustc_save_analysis/Cargo.toml b/src/librustc_save_analysis/Cargo.toml
index de851d9..623da1d 100644
--- a/src/librustc_save_analysis/Cargo.toml
+++ b/src/librustc_save_analysis/Cargo.toml
@@ -11,13 +11,14 @@ path = "lib.rs"
11[dependencies] 11[dependencies]
12log = "0.4" 12log = "0.4"
13rustc = { path = "../librustc" } 13rustc = { path = "../librustc" }
14rustc_ast = { path = "../librustc_ast" }
14rustc_ast_pretty = { path = "../librustc_ast_pretty" } 15rustc_ast_pretty = { path = "../librustc_ast_pretty" }
15rustc_data_structures = { path = "../librustc_data_structures" } 16rustc_data_structures = { path = "../librustc_data_structures" }
16rustc_session = { path = "../librustc_session" }
17rustc_hir = { path = "../librustc_hir" } 17rustc_hir = { path = "../librustc_hir" }
18rustc_hir_pretty = { path = "../librustc_hir_pretty" }
18rustc_parse = { path = "../librustc_parse" } 19rustc_parse = { path = "../librustc_parse" }
19serde_json = "1" 20serde_json = "1"
20rustc_ast = { path = "../librustc_ast" } 21rustc_session = { path = "../librustc_session" }
21rustc_span = { path = "../librustc_span" } 22rustc_span = { path = "../librustc_span" }
22rls-data = "0.19" 23rls-data = "0.19"
23rls-span = "0.5" 24rls-span = "0.5"
diff --git a/src/librustc_save_analysis/lib.rs b/src/librustc_save_analysis/lib.rs
index 024633c..21551ee 100644
--- a/src/librustc_save_analysis/lib.rs
+++ b/src/librustc_save_analysis/lib.rs
@@ -404,14 +404,15 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> {
404 Some(impl_id) => match self.tcx.hir().get_if_local(impl_id) { 404 Some(impl_id) => match self.tcx.hir().get_if_local(impl_id) {
405 Some(Node::Item(item)) => match item.kind { 405 Some(Node::Item(item)) => match item.kind {
406 hir::ItemKind::Impl { ref self_ty, .. } => { 406 hir::ItemKind::Impl { ref self_ty, .. } => {
407 let hir = self.tcx.hir();
408
407 let mut qualname = String::from("<"); 409 let mut qualname = String::from("<");
408 qualname.push_str(&self.tcx.hir().hir_to_pretty_string(self_ty.hir_id)); 410 qualname.push_str(&rustc_hir_pretty::id_to_string(&hir, self_ty.hir_id));
409 411
410 let trait_id = self.tcx.trait_id_of_impl(impl_id); 412 let trait_id = self.tcx.trait_id_of_impl(impl_id);
411 let mut docs = String::new(); 413 let mut docs = String::new();
412 let mut attrs = vec![]; 414 let mut attrs = vec![];
413 let hir_id = self.tcx.hir().node_to_hir_id(id); 415 if let Some(Node::ImplItem(item)) = hir.find(hir.node_to_hir_id(id)) {
414 if let Some(Node::ImplItem(item)) = self.tcx.hir().find(hir_id) {
415 docs = self.docs_for_attrs(&item.attrs); 416 docs = self.docs_for_attrs(&item.attrs);
416 attrs = item.attrs.to_vec(); 417 attrs = item.attrs.to_vec();
417 } 418 }
diff --git a/src/librustc_trait_selection/traits/error_reporting/mod.rs b/src/librustc_trait_selection/traits/error_reporting/mod.rs
index 1293951..8cbed43 100644
--- a/src/librustc_trait_selection/traits/error_reporting/mod.rs
+++ b/src/librustc_trait_selection/traits/error_reporting/mod.rs
@@ -1581,7 +1581,8 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
1581 for param in generics.params { 1581 for param in generics.params {
1582 if param.span == *span 1582 if param.span == *span
1583 && !param.bounds.iter().any(|bound| { 1583 && !param.bounds.iter().any(|bound| {
1584 bound.trait_def_id() == self.tcx.lang_items().sized_trait() 1584 bound.trait_ref().and_then(|trait_ref| trait_ref.trait_def_id())
1585 == self.tcx.lang_items().sized_trait()
1585 }) 1586 })
1586 { 1587 {
1587 let (span, separator) = match param.bounds { 1588 let (span, separator) = match param.bounds {
diff --git a/src/librustc_trait_selection/traits/object_safety.rs b/src/librustc_trait_selection/traits/object_safety.rs
index 5cc1da0..7d4ad61 100644
--- a/src/librustc_trait_selection/traits/object_safety.rs
+++ b/src/librustc_trait_selection/traits/object_safety.rs
@@ -15,7 +15,7 @@ use crate::traits::query::evaluate_obligation::InferCtxtExt;
15use crate::traits::{self, Obligation, ObligationCause}; 15use crate::traits::{self, Obligation, ObligationCause};
16use rustc::ty::subst::{InternalSubsts, Subst}; 16use rustc::ty::subst::{InternalSubsts, Subst};
17use rustc::ty::{self, Predicate, ToPredicate, Ty, TyCtxt, TypeFoldable, WithConstness}; 17use rustc::ty::{self, Predicate, ToPredicate, Ty, TyCtxt, TypeFoldable, WithConstness};
18use rustc_errors::Applicability; 18use rustc_errors::{Applicability, FatalError};
19use rustc_hir as hir; 19use rustc_hir as hir;
20use rustc_hir::def_id::DefId; 20use rustc_hir::def_id::DefId;
21use rustc_session::lint::builtin::WHERE_CLAUSES_OBJECT_SAFETY; 21use rustc_session::lint::builtin::WHERE_CLAUSES_OBJECT_SAFETY;
@@ -170,6 +170,24 @@ fn object_safety_violations_for_trait(
170 violations 170 violations
171} 171}
172 172
173fn sized_trait_bound_spans<'tcx>(
174 tcx: TyCtxt<'tcx>,
175 bounds: hir::GenericBounds<'tcx>,
176) -> impl 'tcx + Iterator<Item = Span> {
177 bounds.iter().filter_map(move |b| match b {
178 hir::GenericBound::Trait(trait_ref, hir::TraitBoundModifier::None)
179 if trait_has_sized_self(
180 tcx,
181 trait_ref.trait_ref.trait_def_id().unwrap_or_else(|| FatalError.raise()),
182 ) =>
183 {
184 // Fetch spans for supertraits that are `Sized`: `trait T: Super`
185 Some(trait_ref.span)
186 }
187 _ => None,
188 })
189}
190
173fn get_sized_bounds(tcx: TyCtxt<'_>, trait_def_id: DefId) -> SmallVec<[Span; 1]> { 191fn get_sized_bounds(tcx: TyCtxt<'_>, trait_def_id: DefId) -> SmallVec<[Span; 1]> {
174 tcx.hir() 192 tcx.hir()
175 .get_if_local(trait_def_id) 193 .get_if_local(trait_def_id)
@@ -189,33 +207,14 @@ fn get_sized_bounds(tcx: TyCtxt<'_>, trait_def_id: DefId) -> SmallVec<[Span; 1]>
189 { 207 {
190 // Fetch spans for trait bounds that are Sized: 208 // Fetch spans for trait bounds that are Sized:
191 // `trait T where Self: Pred` 209 // `trait T where Self: Pred`
192 Some(pred.bounds.iter().filter_map(|b| match b { 210 Some(sized_trait_bound_spans(tcx, pred.bounds))
193 hir::GenericBound::Trait(
194 trait_ref,
195 hir::TraitBoundModifier::None,
196 ) if trait_has_sized_self(
197 tcx,
198 trait_ref.trait_ref.trait_def_id(),
199 ) =>
200 {
201 Some(trait_ref.span)
202 }
203 _ => None,
204 }))
205 } 211 }
206 _ => None, 212 _ => None,
207 } 213 }
208 }) 214 })
209 .flatten() 215 .flatten()
210 .chain(bounds.iter().filter_map(|b| match b { 216 // Fetch spans for supertraits that are `Sized`: `trait T: Super`.
211 hir::GenericBound::Trait(trait_ref, hir::TraitBoundModifier::None) 217 .chain(sized_trait_bound_spans(tcx, bounds))
212 if trait_has_sized_self(tcx, trait_ref.trait_ref.trait_def_id()) =>
213 {
214 // Fetch spans for supertraits that are `Sized`: `trait T: Super`
215 Some(trait_ref.span)
216 }
217 _ => None,
218 }))
219 .collect::<SmallVec<[Span; 1]>>(), 218 .collect::<SmallVec<[Span; 1]>>(),
220 ), 219 ),
221 _ => None, 220 _ => None,
diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs
index 54c646b..07630d7 100644
--- a/src/librustc_typeck/astconv.rs
+++ b/src/librustc_typeck/astconv.rs
@@ -16,12 +16,11 @@ use rustc::ty::{GenericParamDef, GenericParamDefKind};
16use rustc_ast::ast; 16use rustc_ast::ast;
17use rustc_ast::util::lev_distance::find_best_match_for_name; 17use rustc_ast::util::lev_distance::find_best_match_for_name;
18use rustc_data_structures::fx::{FxHashMap, FxHashSet}; 18use rustc_data_structures::fx::{FxHashMap, FxHashSet};
19use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticId}; 19use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticId, FatalError};
20use rustc_hir as hir; 20use rustc_hir as hir;
21use rustc_hir::def::{CtorOf, DefKind, Namespace, Res}; 21use rustc_hir::def::{CtorOf, DefKind, Namespace, Res};
22use rustc_hir::def_id::DefId; 22use rustc_hir::def_id::DefId;
23use rustc_hir::intravisit::{walk_generics, Visitor}; 23use rustc_hir::intravisit::{walk_generics, Visitor as _};
24use rustc_hir::print;
25use rustc_hir::{Constness, GenericArg, GenericArgs}; 24use rustc_hir::{Constness, GenericArg, GenericArgs};
26use rustc_session::lint::builtin::{AMBIGUOUS_ASSOCIATED_ITEMS, LATE_BOUND_LIFETIME_ARGUMENTS}; 25use rustc_session::lint::builtin::{AMBIGUOUS_ASSOCIATED_ITEMS, LATE_BOUND_LIFETIME_ARGUMENTS};
27use rustc_session::parse::feature_err; 26use rustc_session::parse::feature_err;
@@ -991,7 +990,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
991 990
992 self.ast_path_to_mono_trait_ref( 991 self.ast_path_to_mono_trait_ref(
993 trait_ref.path.span, 992 trait_ref.path.span,
994 trait_ref.trait_def_id(), 993 trait_ref.trait_def_id().unwrap_or_else(|| FatalError.raise()),
995 self_ty, 994 self_ty,
996 trait_ref.path.segments.last().unwrap(), 995 trait_ref.path.segments.last().unwrap(),
997 ) 996 )
@@ -1007,7 +1006,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1007 bounds: &mut Bounds<'tcx>, 1006 bounds: &mut Bounds<'tcx>,
1008 speculative: bool, 1007 speculative: bool,
1009 ) -> Result<(), GenericArgCountMismatch> { 1008 ) -> Result<(), GenericArgCountMismatch> {
1010 let trait_def_id = trait_ref.trait_def_id(); 1009 let trait_def_id = trait_ref.trait_def_id().unwrap_or_else(|| FatalError.raise());
1011 1010
1012 debug!("instantiate_poly_trait_ref({:?}, def_id={:?})", trait_ref, trait_def_id); 1011 debug!("instantiate_poly_trait_ref({:?}, def_id={:?})", trait_ref, trait_def_id);
1013 1012
@@ -1118,6 +1117,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1118 if !self.tcx().features().unboxed_closures 1117 if !self.tcx().features().unboxed_closures
1119 && trait_segment.generic_args().parenthesized != trait_def.paren_sugar 1118 && trait_segment.generic_args().parenthesized != trait_def.paren_sugar
1120 { 1119 {
1120 let sess = &self.tcx().sess.parse_sess;
1121 // For now, require that parenthetical notation be used only with `Fn()` etc. 1121 // For now, require that parenthetical notation be used only with `Fn()` etc.
1122 let (msg, sugg) = if trait_def.paren_sugar { 1122 let (msg, sugg) = if trait_def.paren_sugar {
1123 ( 1123 (
@@ -1132,7 +1132,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1132 .and_then(|args| args.args.get(0)) 1132 .and_then(|args| args.args.get(0))
1133 .and_then(|arg| match arg { 1133 .and_then(|arg| match arg {
1134 hir::GenericArg::Type(ty) => { 1134 hir::GenericArg::Type(ty) => {
1135 Some(print::to_string(print::NO_ANN, |s| s.print_type(ty))) 1135 sess.source_map().span_to_snippet(ty.span).ok()
1136 } 1136 }
1137 _ => None, 1137 _ => None,
1138 }) 1138 })
@@ -1143,7 +1143,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1143 .iter() 1143 .iter()
1144 .filter_map(|b| match (b.ident.as_str() == "Output", &b.kind) { 1144 .filter_map(|b| match (b.ident.as_str() == "Output", &b.kind) {
1145 (true, hir::TypeBindingKind::Equality { ty }) => { 1145 (true, hir::TypeBindingKind::Equality { ty }) => {
1146 Some(print::to_string(print::NO_ANN, |s| s.print_type(ty))) 1146 sess.source_map().span_to_snippet(ty.span).ok()
1147 } 1147 }
1148 _ => None, 1148 _ => None,
1149 }) 1149 })
@@ -1154,7 +1154,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1154 } else { 1154 } else {
1155 ("parenthetical notation is only stable when used with `Fn`-family traits", None) 1155 ("parenthetical notation is only stable when used with `Fn`-family traits", None)
1156 }; 1156 };
1157 let sess = &self.tcx().sess.parse_sess;
1158 let mut err = feature_err(sess, sym::unboxed_closures, span, msg); 1157 let mut err = feature_err(sess, sym::unboxed_closures, span, msg);
1159 if let Some(sugg) = sugg { 1158 if let Some(sugg) = sugg {
1160 let msg = "use parenthetical notation instead"; 1159 let msg = "use parenthetical notation instead";
diff --git a/src/librustc_typeck/check/_match.rs b/src/librustc_typeck/check/_match.rs
index a34389b..b44bab2 100644
--- a/src/librustc_typeck/check/_match.rs
+++ b/src/librustc_typeck/check/_match.rs
@@ -245,11 +245,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
245 { 245 {
246 // check that the `if` expr without `else` is the fn body's expr 246 // check that the `if` expr without `else` is the fn body's expr
247 if expr.span == span { 247 if expr.span == span {
248 return self.get_fn_decl(hir_id).map(|(fn_decl, _)| { 248 return self.get_fn_decl(hir_id).and_then(|(fn_decl, _)| {
249 ( 249 let span = fn_decl.output.span();
250 fn_decl.output.span(), 250 let snippet = self.tcx.sess.source_map().span_to_snippet(span).ok()?;
251 format!("expected `{}` because of this return type", fn_decl.output), 251 Some((span, format!("expected `{}` because of this return type", snippet)))
252 )
253 }); 252 });
254 } 253 }
255 } 254 }
diff --git a/src/librustc_typeck/check/callee.rs b/src/librustc_typeck/check/callee.rs
index ec79604..0749516 100644
--- a/src/librustc_typeck/check/callee.rs
+++ b/src/librustc_typeck/check/callee.rs
@@ -265,7 +265,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
265 if let &ty::Adt(adt_def, ..) = t { 265 if let &ty::Adt(adt_def, ..) = t {
266 if adt_def.is_enum() { 266 if adt_def.is_enum() {
267 if let hir::ExprKind::Call(ref expr, _) = call_expr.kind { 267 if let hir::ExprKind::Call(ref expr, _) = call_expr.kind {
268 unit_variant = Some(self.tcx.hir().hir_to_pretty_string(expr.hir_id)) 268 unit_variant =
269 self.tcx.sess.source_map().span_to_snippet(expr.span).ok();
269 } 270 }
270 } 271 }
271 } 272 }
@@ -335,16 +336,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
335 err.span_label(call_expr.span, "call expression requires function"); 336 err.span_label(call_expr.span, "call expression requires function");
336 337
337 if let Some(span) = self.tcx.hir().res_span(def) { 338 if let Some(span) = self.tcx.hir().res_span(def) {
339 let callee_ty = callee_ty.to_string();
338 let label = match (unit_variant, inner_callee_path) { 340 let label = match (unit_variant, inner_callee_path) {
339 (Some(path), _) => format!("`{}` defined here", path), 341 (Some(path), _) => Some(format!("`{}` defined here", path)),
340 (_, Some(hir::QPath::Resolved(_, path))) => format!( 342 (_, Some(hir::QPath::Resolved(_, path))) => {
341 "`{}` defined here returns `{}`", 343 self.tcx.sess.source_map().span_to_snippet(path.span).ok().map(
342 path, 344 |p| format!("`{}` defined here returns `{}`", p, callee_ty),
343 callee_ty.to_string() 345 )
344 ), 346 }
345 _ => format!("`{}` defined here", callee_ty.to_string()), 347 _ => Some(format!("`{}` defined here", callee_ty)),
346 }; 348 };
347 err.span_label(span, label); 349 if let Some(label) = label {
350 err.span_label(span, label);
351 }
348 } 352 }
349 err.emit(); 353 err.emit();
350 } else { 354 } else {
diff --git a/src/librustc_typeck/check/coercion.rs b/src/librustc_typeck/check/coercion.rs
index 33fc18b..2dc2a48 100644
--- a/src/librustc_typeck/check/coercion.rs
+++ b/src/librustc_typeck/check/coercion.rs
@@ -1402,9 +1402,12 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
1402 { 1402 {
1403 // Are of this `impl Trait`'s traits object safe? 1403 // Are of this `impl Trait`'s traits object safe?
1404 is_object_safe = bounds.iter().all(|bound| { 1404 is_object_safe = bounds.iter().all(|bound| {
1405 bound.trait_def_id().map_or(false, |def_id| { 1405 bound
1406 fcx.tcx.object_safety_violations(def_id).is_empty() 1406 .trait_ref()
1407 }) 1407 .and_then(|t| t.trait_def_id())
1408 .map_or(false, |def_id| {
1409 fcx.tcx.object_safety_violations(def_id).is_empty()
1410 })
1408 }) 1411 })
1409 } 1412 }
1410 } 1413 }
diff --git a/src/librustc_typeck/check/demand.rs b/src/librustc_typeck/check/demand.rs
index 0556c80..f7ffb5a 100644
--- a/src/librustc_typeck/check/demand.rs
+++ b/src/librustc_typeck/check/demand.rs
@@ -9,7 +9,7 @@ use rustc::ty::{self, AssocItem, Ty};
9use rustc_ast::util::parser::PREC_POSTFIX; 9use rustc_ast::util::parser::PREC_POSTFIX;
10use rustc_errors::{Applicability, DiagnosticBuilder}; 10use rustc_errors::{Applicability, DiagnosticBuilder};
11use rustc_hir as hir; 11use rustc_hir as hir;
12use rustc_hir::{is_range_literal, print, Node}; 12use rustc_hir::{is_range_literal, Node};
13use rustc_span::symbol::sym; 13use rustc_span::symbol::sym;
14use rustc_span::Span; 14use rustc_span::Span;
15 15
@@ -198,13 +198,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
198 .peekable(); 198 .peekable();
199 199
200 if compatible_variants.peek().is_some() { 200 if compatible_variants.peek().is_some() {
201 let expr_text = 201 if let Ok(expr_text) = self.tcx.sess.source_map().span_to_snippet(expr.span) {
202 self.tcx.sess.source_map().span_to_snippet(expr.span).unwrap_or_else(|_| { 202 let suggestions = compatible_variants.map(|v| format!("{}({})", v, expr_text));
203 print::to_string(print::NO_ANN, |s| s.print_expr(expr)) 203 let msg = "try using a variant of the expected enum";
204 }); 204 err.span_suggestions(
205 let suggestions = compatible_variants.map(|v| format!("{}({})", v, expr_text)); 205 expr.span,
206 let msg = "try using a variant of the expected enum"; 206 msg,
207 err.span_suggestions(expr.span, msg, suggestions, Applicability::MaybeIncorrect); 207 suggestions,
208 Applicability::MaybeIncorrect,
209 );
210 }
208 } 211 }
209 } 212 }
210 } 213 }
diff --git a/src/librustc_typeck/check/expr.rs b/src/librustc_typeck/check/expr.rs
index dffed9a..53a20d9 100644
--- a/src/librustc_typeck/check/expr.rs
+++ b/src/librustc_typeck/check/expr.rs
@@ -475,7 +475,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
475 tcx.types.err 475 tcx.types.err
476 } 476 }
477 Res::Def(DefKind::Ctor(_, CtorKind::Fictive), _) => { 477 Res::Def(DefKind::Ctor(_, CtorKind::Fictive), _) => {
478 report_unexpected_variant_res(tcx, res, expr.span, qpath); 478 report_unexpected_variant_res(tcx, res, expr.span);
479 tcx.types.err 479 tcx.types.err
480 } 480 }
481 _ => self.instantiate_value_path(segs, opt_ty, res, expr.span, expr.hir_id).0, 481 _ => self.instantiate_value_path(segs, opt_ty, res, expr.span, expr.hir_id).0,
@@ -696,10 +696,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
696 self, 696 self,
697 &cause, 697 &cause,
698 &mut |db| { 698 &mut |db| {
699 db.span_label( 699 let span = fn_decl.output.span();
700 fn_decl.output.span(), 700 if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
701 format!("expected `{}` because of this return type", fn_decl.output,), 701 db.span_label(
702 ); 702 span,
703 format!("expected `{}` because of this return type", snippet),
704 );
705 }
703 }, 706 },
704 true, 707 true,
705 ); 708 );
@@ -1668,20 +1671,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1668 if let (Some(len), Ok(user_index)) = 1671 if let (Some(len), Ok(user_index)) =
1669 (len.try_eval_usize(self.tcx, self.param_env), field.as_str().parse::<u64>()) 1672 (len.try_eval_usize(self.tcx, self.param_env), field.as_str().parse::<u64>())
1670 { 1673 {
1671 let base = self 1674 if let Ok(base) = self.tcx.sess.source_map().span_to_snippet(base.span) {
1672 .tcx 1675 let help = "instead of using tuple indexing, use array indexing";
1673 .sess 1676 let suggestion = format!("{}[{}]", base, field);
1674 .source_map() 1677 let applicability = if len < user_index {
1675 .span_to_snippet(base.span) 1678 Applicability::MachineApplicable
1676 .unwrap_or_else(|_| self.tcx.hir().hir_to_pretty_string(base.hir_id)); 1679 } else {
1677 let help = "instead of using tuple indexing, use array indexing"; 1680 Applicability::MaybeIncorrect
1678 let suggestion = format!("{}[{}]", base, field); 1681 };
1679 let applicability = if len < user_index { 1682 err.span_suggestion(expr.span, help, suggestion, applicability);
1680 Applicability::MachineApplicable 1683 }
1681 } else {
1682 Applicability::MaybeIncorrect
1683 };
1684 err.span_suggestion(expr.span, help, suggestion, applicability);
1685 } 1684 }
1686 } 1685 }
1687 1686
@@ -1692,15 +1691,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1692 base: &hir::Expr<'_>, 1691 base: &hir::Expr<'_>,
1693 field: ast::Ident, 1692 field: ast::Ident,
1694 ) { 1693 ) {
1695 let base = self 1694 if let Ok(base) = self.tcx.sess.source_map().span_to_snippet(base.span) {
1696 .tcx 1695 let msg = format!("`{}` is a raw pointer; try dereferencing it", base);
1697 .sess 1696 let suggestion = format!("(*{}).{}", base, field);
1698 .source_map() 1697 err.span_suggestion(expr.span, &msg, suggestion, Applicability::MaybeIncorrect);
1699 .span_to_snippet(base.span) 1698 }
1700 .unwrap_or_else(|_| self.tcx.hir().hir_to_pretty_string(base.hir_id));
1701 let msg = format!("`{}` is a raw pointer; try dereferencing it", base);
1702 let suggestion = format!("(*{}).{}", base, field);
1703 err.span_suggestion(expr.span, &msg, suggestion, Applicability::MaybeIncorrect);
1704 } 1699 }
1705 1700
1706 fn no_such_field_err<T: Display>( 1701 fn no_such_field_err<T: Display>(
diff --git a/src/librustc_typeck/check/method/suggest.rs b/src/librustc_typeck/check/method/suggest.rs
index e940ecc..6888200 100644
--- a/src/librustc_typeck/check/method/suggest.rs
+++ b/src/librustc_typeck/check/method/suggest.rs
@@ -1061,7 +1061,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1061 let trait_def_ids: FxHashSet<DefId> = param 1061 let trait_def_ids: FxHashSet<DefId> = param
1062 .bounds 1062 .bounds
1063 .iter() 1063 .iter()
1064 .filter_map(|bound| bound.trait_def_id()) 1064 .filter_map(|bound| Some(bound.trait_ref()?.trait_def_id()?))
1065 .collect(); 1065 .collect();
1066 if !candidates.iter().any(|t| trait_def_ids.contains(&t.def_id)) { 1066 if !candidates.iter().any(|t| trait_def_ids.contains(&t.def_id)) {
1067 err.span_suggestions( 1067 err.span_suggestions(
diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs
index 91ab94b..f188ee0 100644
--- a/src/librustc_typeck/check/mod.rs
+++ b/src/librustc_typeck/check/mod.rs
@@ -2677,14 +2677,14 @@ pub fn check_enum<'tcx>(
2677 check_transparent(tcx, sp, def_id); 2677 check_transparent(tcx, sp, def_id);
2678} 2678}
2679 2679
2680fn report_unexpected_variant_res(tcx: TyCtxt<'_>, res: Res, span: Span, qpath: &QPath<'_>) { 2680fn report_unexpected_variant_res(tcx: TyCtxt<'_>, res: Res, span: Span) {
2681 struct_span_err!( 2681 struct_span_err!(
2682 tcx.sess, 2682 tcx.sess,
2683 span, 2683 span,
2684 E0533, 2684 E0533,
2685 "expected unit struct, unit variant or constant, found {} `{}`", 2685 "expected unit struct, unit variant or constant, found {}{}",
2686 res.descr(), 2686 res.descr(),
2687 hir::print::to_string(&tcx.hir(), |s| s.print_qpath(qpath, false)) 2687 tcx.sess.source_map().span_to_snippet(span).map_or(String::new(), |s| format!(" `{}`", s)),
2688 ) 2688 )
2689 .emit(); 2689 .emit();
2690} 2690}
diff --git a/src/librustc_typeck/check/op.rs b/src/librustc_typeck/check/op.rs
index f589805..657926a 100644
--- a/src/librustc_typeck/check/op.rs
+++ b/src/librustc_typeck/check/op.rs
@@ -492,36 +492,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
492 err.span_label(span, ty.to_string()); 492 err.span_label(span, ty.to_string());
493 if let FnDef(def_id, _) = ty.kind { 493 if let FnDef(def_id, _) = ty.kind {
494 let source_map = self.tcx.sess.source_map(); 494 let source_map = self.tcx.sess.source_map();
495 let hir_id = match self.tcx.hir().as_local_hir_id(def_id) {
496 Some(hir_id) => hir_id,
497 None => return false,
498 };
499 if !self.tcx.has_typeck_tables(def_id) { 495 if !self.tcx.has_typeck_tables(def_id) {
500 return false; 496 return false;
501 } 497 }
502 let fn_sig = { 498 // We're emitting a suggestion, so we can just ignore regions
503 match self.tcx.typeck_tables_of(def_id).liberated_fn_sigs().get(hir_id) { 499 let fn_sig = *self.tcx.fn_sig(def_id).skip_binder();
504 Some(f) => *f,
505 None => {
506 bug!("No fn-sig entry for def_id={:?}", def_id);
507 }
508 }
509 };
510 500
511 let other_ty = if let FnDef(def_id, _) = other_ty.kind { 501 let other_ty = if let FnDef(def_id, _) = other_ty.kind {
512 let hir_id = match self.tcx.hir().as_local_hir_id(def_id) {
513 Some(hir_id) => hir_id,
514 None => return false,
515 };
516 if !self.tcx.has_typeck_tables(def_id) { 502 if !self.tcx.has_typeck_tables(def_id) {
517 return false; 503 return false;
518 } 504 }
519 match self.tcx.typeck_tables_of(def_id).liberated_fn_sigs().get(hir_id) { 505 // We're emitting a suggestion, so we can just ignore regions
520 Some(f) => f.clone().output(), 506 self.tcx.fn_sig(def_id).skip_binder().output()
521 None => {
522 bug!("No fn-sig entry for def_id={:?}", def_id);
523 }
524 }
525 } else { 507 } else {
526 other_ty 508 other_ty
527 }; 509 };
diff --git a/src/librustc_typeck/check/pat.rs b/src/librustc_typeck/check/pat.rs
index 0c4a05e..ec703d5 100644
--- a/src/librustc_typeck/check/pat.rs
+++ b/src/librustc_typeck/check/pat.rs
@@ -171,9 +171,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
171 PatKind::TupleStruct(ref qpath, subpats, ddpos) => { 171 PatKind::TupleStruct(ref qpath, subpats, ddpos) => {
172 self.check_pat_tuple_struct(pat, qpath, subpats, ddpos, expected, def_bm, ti) 172 self.check_pat_tuple_struct(pat, qpath, subpats, ddpos, expected, def_bm, ti)
173 } 173 }
174 PatKind::Path(ref qpath) => { 174 PatKind::Path(_) => self.check_pat_path(pat, path_res.unwrap(), expected, ti),
175 self.check_pat_path(pat, path_res.unwrap(), qpath, expected, ti)
176 }
177 PatKind::Struct(ref qpath, fields, etc) => { 175 PatKind::Struct(ref qpath, fields, etc) => {
178 self.check_pat_struct(pat, qpath, fields, etc, expected, def_bm, ti) 176 self.check_pat_struct(pat, qpath, fields, etc, expected, def_bm, ti)
179 } 177 }
@@ -694,7 +692,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
694 &self, 692 &self,
695 pat: &Pat<'_>, 693 pat: &Pat<'_>,
696 path_resolution: (Res, Option<Ty<'tcx>>, &'b [hir::PathSegment<'b>]), 694 path_resolution: (Res, Option<Ty<'tcx>>, &'b [hir::PathSegment<'b>]),
697 qpath: &hir::QPath<'_>,
698 expected: Ty<'tcx>, 695 expected: Ty<'tcx>,
699 ti: TopInfo<'tcx>, 696 ti: TopInfo<'tcx>,
700 ) -> Ty<'tcx> { 697 ) -> Ty<'tcx> {
@@ -707,17 +704,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
707 self.set_tainted_by_errors(); 704 self.set_tainted_by_errors();
708 return tcx.types.err; 705 return tcx.types.err;
709 } 706 }
710 Res::Def(DefKind::AssocFn, _) 707 Res::Def(DefKind::AssocFn | DefKind::Ctor(_, CtorKind::Fictive | CtorKind::Fn), _) => {
711 | Res::Def(DefKind::Ctor(_, CtorKind::Fictive), _) 708 report_unexpected_variant_res(tcx, res, pat.span);
712 | Res::Def(DefKind::Ctor(_, CtorKind::Fn), _) => {
713 report_unexpected_variant_res(tcx, res, pat.span, qpath);
714 return tcx.types.err; 709 return tcx.types.err;
715 } 710 }
716 Res::Def(DefKind::Ctor(_, CtorKind::Const), _) 711 Res::SelfCtor(..)
717 | Res::SelfCtor(..) 712 | Res::Def(
718 | Res::Def(DefKind::Const, _) 713 DefKind::Ctor(_, CtorKind::Const)
719 | Res::Def(DefKind::AssocConst, _) 714 | DefKind::Const
720 | Res::Def(DefKind::ConstParam, _) => {} // OK 715 | DefKind::AssocConst
716 | DefKind::ConstParam,
717 _,
718 ) => {} // OK
721 _ => bug!("unexpected pattern resolution: {:?}", res), 719 _ => bug!("unexpected pattern resolution: {:?}", res),
722 } 720 }
723 721
@@ -791,14 +789,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
791 } 789 }
792 }; 790 };
793 let report_unexpected_res = |res: Res| { 791 let report_unexpected_res = |res: Res| {
792 let sm = tcx.sess.source_map();
793 let path_str = sm
794 .span_to_snippet(sm.span_until_char(pat.span, '('))
795 .map_or(String::new(), |s| format!(" `{}`", s.trim_end()));
794 let msg = format!( 796 let msg = format!(
795 "expected tuple struct or tuple variant, found {} `{}`", 797 "expected tuple struct or tuple variant, found {}{}",
796 res.descr(), 798 res.descr(),
797 hir::print::to_string(&tcx.hir(), |s| s.print_qpath(qpath, false)), 799 path_str
798 ); 800 );
801
799 let mut err = struct_span_err!(tcx.sess, pat.span, E0164, "{}", msg); 802 let mut err = struct_span_err!(tcx.sess, pat.span, E0164, "{}", msg);
800 match (res, &pat.kind) { 803 match res {
801 (Res::Def(DefKind::Fn, _), _) | (Res::Def(DefKind::AssocFn, _), _) => { 804 Res::Def(DefKind::Fn | DefKind::AssocFn, _) => {
802 err.span_label(pat.span, "`fn` calls are not allowed in patterns"); 805 err.span_label(pat.span, "`fn` calls are not allowed in patterns");
803 err.help( 806 err.help(
804 "for more information, visit \ 807 "for more information, visit \
diff --git a/src/librustc_typeck/check_unused.rs b/src/librustc_typeck/check_unused.rs
index d0414af5..9d8113e 100644
--- a/src/librustc_typeck/check_unused.rs
+++ b/src/librustc_typeck/check_unused.rs
@@ -5,7 +5,6 @@ use rustc_errors::Applicability;
5use rustc_hir as hir; 5use rustc_hir as hir;
6use rustc_hir::def_id::{DefId, DefIdSet, LOCAL_CRATE}; 6use rustc_hir::def_id::{DefId, DefIdSet, LOCAL_CRATE};
7use rustc_hir::itemlikevisit::ItemLikeVisitor; 7use rustc_hir::itemlikevisit::ItemLikeVisitor;
8use rustc_hir::print::visibility_qualified;
9use rustc_session::lint; 8use rustc_session::lint;
10use rustc_span::Span; 9use rustc_span::Span;
11 10
@@ -176,16 +175,13 @@ fn unused_crates_lint(tcx: TyCtxt<'_>) {
176 Some(orig_name) => format!("use {} as {};", orig_name, item.ident.name), 175 Some(orig_name) => format!("use {} as {};", orig_name, item.ident.name),
177 None => format!("use {};", item.ident.name), 176 None => format!("use {};", item.ident.name),
178 }; 177 };
179 178 let vis = tcx.sess.source_map().span_to_snippet(item.vis.span).unwrap_or_default();
180 let replacement = visibility_qualified(&item.vis, base_replacement); 179 let add_vis = |to| if vis.is_empty() { to } else { format!("{} {}", vis, to) };
181 let msg = "`extern crate` is not idiomatic in the new edition"; 180 lint.build("`extern crate` is not idiomatic in the new edition")
182 let help = format!("convert it to a `{}`", visibility_qualified(&item.vis, "use"));
183
184 lint.build(msg)
185 .span_suggestion_short( 181 .span_suggestion_short(
186 extern_crate.span, 182 extern_crate.span,
187 &help, 183 &format!("convert it to a `{}`", add_vis("use".to_string())),
188 replacement, 184 add_vis(base_replacement),
189 Applicability::MachineApplicable, 185 Applicability::MachineApplicable,
190 ) 186 )
191 .emit(); 187 .emit();
diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs
index 42cd724..31123c5 100644
--- a/src/librustc_typeck/collect.rs
+++ b/src/librustc_typeck/collect.rs
@@ -2146,13 +2146,18 @@ fn compute_sig_of_foreign_fn_decl<'tcx>(
2146 { 2146 {
2147 let check = |ast_ty: &hir::Ty<'_>, ty: Ty<'_>| { 2147 let check = |ast_ty: &hir::Ty<'_>, ty: Ty<'_>| {
2148 if ty.is_simd() { 2148 if ty.is_simd() {
2149 let snip = tcx
2150 .sess
2151 .source_map()
2152 .span_to_snippet(ast_ty.span)
2153 .map_or(String::new(), |s| format!(" `{}`", s));
2149 tcx.sess 2154 tcx.sess
2150 .struct_span_err( 2155 .struct_span_err(
2151 ast_ty.span, 2156 ast_ty.span,
2152 &format!( 2157 &format!(
2153 "use of SIMD type `{}` in FFI is highly experimental and \ 2158 "use of SIMD type{} in FFI is highly experimental and \
2154 may result in invalid code", 2159 may result in invalid code",
2155 tcx.hir().hir_to_pretty_string(ast_ty.hir_id) 2160 snip
2156 ), 2161 ),
2157 ) 2162 )
2158 .help("add `#![feature(simd_ffi)]` to the crate attributes to enable") 2163 .help("add `#![feature(simd_ffi)]` to the crate attributes to enable")
diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs
index 153f7af..510eae8 100644
--- a/src/librustdoc/clean/inline.rs
+++ b/src/librustdoc/clean/inline.rs
@@ -482,8 +482,8 @@ fn build_module(cx: &DocContext<'_>, did: DefId, visited: &mut FxHashSet<DefId>)
482} 482}
483 483
484pub fn print_inlined_const(cx: &DocContext<'_>, did: DefId) -> String { 484pub fn print_inlined_const(cx: &DocContext<'_>, did: DefId) -> String {
485 if let Some(node_id) = cx.tcx.hir().as_local_hir_id(did) { 485 if let Some(hir_id) = cx.tcx.hir().as_local_hir_id(did) {
486 cx.tcx.hir().hir_to_pretty_string(node_id) 486 rustc_hir_pretty::id_to_string(&cx.tcx.hir(), hir_id)
487 } else { 487 } else {
488 cx.tcx.rendered_const(did) 488 cx.tcx.rendered_const(did)
489 } 489 }
diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs
index 82e3471..b3bfb55 100644
--- a/src/librustdoc/clean/utils.rs
+++ b/src/librustdoc/clean/utils.rs
@@ -578,7 +578,7 @@ pub fn print_const_expr(cx: &DocContext<'_>, body: hir::BodyId) -> String {
578 None 578 None
579 }; 579 };
580 580
581 snippet.unwrap_or_else(|| cx.tcx.hir().hir_to_pretty_string(body.hir_id)) 581 snippet.unwrap_or_else(|| rustc_hir_pretty::id_to_string(&cx.tcx.hir(), body.hir_id))
582} 582}
583 583
584/// Given a type Path, resolve it to a Type using the TyCtxt 584/// Given a type Path, resolve it to a Type using the TyCtxt
diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs
index 33cabad..3c5df02 100644
--- a/src/librustdoc/lib.rs
+++ b/src/librustdoc/lib.rs
@@ -26,6 +26,7 @@ extern crate rustc_errors;
26extern crate rustc_expand; 26extern crate rustc_expand;
27extern crate rustc_feature; 27extern crate rustc_feature;
28extern crate rustc_hir; 28extern crate rustc_hir;
29extern crate rustc_hir_pretty;
29extern crate rustc_index; 30extern crate rustc_index;
30extern crate rustc_infer; 31extern crate rustc_infer;
31extern crate rustc_interface; 32extern crate rustc_interface;
diff --git a/src/librustdoc/test.rs b/src/librustdoc/test.rs
index c5aa467..93305a1 100644
--- a/src/librustdoc/test.rs
+++ b/src/librustdoc/test.rs
@@ -910,7 +910,7 @@ impl<'a, 'hir> intravisit::Visitor<'hir> for HirCollector<'a, 'hir> {
910 910
911 fn visit_item(&mut self, item: &'hir hir::Item) { 911 fn visit_item(&mut self, item: &'hir hir::Item) {
912 let name = if let hir::ItemKind::Impl { ref self_ty, .. } = item.kind { 912 let name = if let hir::ItemKind::Impl { ref self_ty, .. } = item.kind {
913 self.map.hir_to_pretty_string(self_ty.hir_id) 913 rustc_hir_pretty::id_to_string(&self.map, self_ty.hir_id)
914 } else { 914 } else {
915 item.ident.to_string() 915 item.ident.to_string()
916 }; 916 };
diff --git a/src/libstd/ffi/os_str.rs b/src/libstd/ffi/os_str.rs
index 77da972..0fbe8e5 100644
--- a/src/libstd/ffi/os_str.rs
+++ b/src/libstd/ffi/os_str.rs
@@ -379,6 +379,14 @@ impl ops::Index<ops::RangeFull> for OsString {
379 } 379 }
380} 380}
381 381
382#[stable(feature = "mut_osstr", since = "1.44.0")]
383impl ops::IndexMut<ops::RangeFull> for OsString {
384 #[inline]
385 fn index_mut(&mut self, _index: ops::RangeFull) -> &mut OsStr {
386 OsStr::from_inner_mut(self.inner.as_mut_slice())
387 }
388}
389
382#[stable(feature = "rust1", since = "1.0.0")] 390#[stable(feature = "rust1", since = "1.0.0")]
383impl ops::Deref for OsString { 391impl ops::Deref for OsString {
384 type Target = OsStr; 392 type Target = OsStr;
@@ -389,6 +397,14 @@ impl ops::Deref for OsString {
389 } 397 }
390} 398}
391 399
400#[stable(feature = "mut_osstr", since = "1.44.0")]
401impl ops::DerefMut for OsString {
402 #[inline]
403 fn deref_mut(&mut self) -> &mut OsStr {
404 &mut self[..]
405 }
406}
407
392#[stable(feature = "osstring_default", since = "1.9.0")] 408#[stable(feature = "osstring_default", since = "1.9.0")]
393impl Default for OsString { 409impl Default for OsString {
394 /// Constructs an empty `OsString`. 410 /// Constructs an empty `OsString`.
@@ -509,9 +525,20 @@ impl OsStr {
509 525
510 #[inline] 526 #[inline]
511 fn from_inner(inner: &Slice) -> &OsStr { 527 fn from_inner(inner: &Slice) -> &OsStr {
528 // Safety: OsStr is just a wrapper of Slice,
529 // therefore converting &Slice to &OsStr is safe.
512 unsafe { &*(inner as *const Slice as *const OsStr) } 530 unsafe { &*(inner as *const Slice as *const OsStr) }
513 } 531 }
514 532
533 #[inline]
534 fn from_inner_mut(inner: &mut Slice) -> &mut OsStr {
535 // Safety: OsStr is just a wrapper of Slice,
536 // therefore converting &mut Slice to &mut OsStr is safe.
537 // Any method that mutates OsStr must be careful not to
538 // break platform-specific encoding, in particular Wtf8 on Windows.
539 unsafe { &mut *(inner as *mut Slice as *mut OsStr) }
540 }
541
515 /// Yields a [`&str`] slice if the `OsStr` is valid Unicode. 542 /// Yields a [`&str`] slice if the `OsStr` is valid Unicode.
516 /// 543 ///
517 /// This conversion may entail doing a check for UTF-8 validity. 544 /// This conversion may entail doing a check for UTF-8 validity.
diff --git a/src/libstd/sys/windows/os_str.rs b/src/libstd/sys/windows/os_str.rs
index ef260f9..ff6885c 100644
--- a/src/libstd/sys/windows/os_str.rs
+++ b/src/libstd/sys/windows/os_str.rs
@@ -77,9 +77,21 @@ impl Buf {
77 } 77 }
78 78
79 pub fn as_slice(&self) -> &Slice { 79 pub fn as_slice(&self) -> &Slice {
80 // Safety: Slice is just a wrapper for Wtf8,
81 // and self.inner.as_slice() returns &Wtf8.
82 // Therefore, transmuting &Wtf8 to &Slice is safe.
80 unsafe { mem::transmute(self.inner.as_slice()) } 83 unsafe { mem::transmute(self.inner.as_slice()) }
81 } 84 }
82 85
86 pub fn as_mut_slice(&mut self) -> &mut Slice {
87 // Safety: Slice is just a wrapper for Wtf8,
88 // and self.inner.as_mut_slice() returns &mut Wtf8.
89 // Therefore, transmuting &mut Wtf8 to &mut Slice is safe.
90 // Additionally, care should be taken to ensure the slice
91 // is always valid Wtf8.
92 unsafe { mem::transmute(self.inner.as_mut_slice()) }
93 }
94
83 pub fn into_string(self) -> Result<String, Buf> { 95 pub fn into_string(self) -> Result<String, Buf> {
84 self.inner.into_string().map_err(|buf| Buf { inner: buf }) 96 self.inner.into_string().map_err(|buf| Buf { inner: buf })
85 } 97 }
diff --git a/src/libstd/sys_common/os_str_bytes.rs b/src/libstd/sys_common/os_str_bytes.rs
index e965ea7..c5d02fb 100644
--- a/src/libstd/sys_common/os_str_bytes.rs
+++ b/src/libstd/sys_common/os_str_bytes.rs
@@ -106,9 +106,20 @@ impl Buf {
106 106
107 #[inline] 107 #[inline]
108 pub fn as_slice(&self) -> &Slice { 108 pub fn as_slice(&self) -> &Slice {
109 // Safety: Slice just wraps [u8],
110 // and &*self.inner is &[u8], therefore
111 // transmuting &[u8] to &Slice is safe.
109 unsafe { mem::transmute(&*self.inner) } 112 unsafe { mem::transmute(&*self.inner) }
110 } 113 }
111 114
115 #[inline]
116 pub fn as_mut_slice(&mut self) -> &mut Slice {
117 // Safety: Slice just wraps [u8],
118 // and &mut *self.inner is &mut [u8], therefore
119 // transmuting &mut [u8] to &mut Slice is safe.
120 unsafe { mem::transmute(&mut *self.inner) }
121 }
122
112 pub fn into_string(self) -> Result<String, Buf> { 123 pub fn into_string(self) -> Result<String, Buf> {
113 String::from_utf8(self.inner).map_err(|p| Buf { inner: p.into_bytes() }) 124 String::from_utf8(self.inner).map_err(|p| Buf { inner: p.into_bytes() })
114 } 125 }
diff --git a/src/test/ui/issues/issue-66667-function-cmp-cycle.rs b/src/test/ui/issues/issue-66667-function-cmp-cycle.rs
new file mode 100644
index 0000000..7b025be
--- /dev/null
+++ b/src/test/ui/issues/issue-66667-function-cmp-cycle.rs
@@ -0,0 +1,16 @@
1fn first() {
2 second == 1 //~ ERROR binary operation
3 //~^ ERROR mismatched types
4}
5
6fn second() {
7 first == 1 //~ ERROR binary operation
8 //~^ ERROR mismatched types
9}
10
11fn bar() {
12 bar == 1 //~ ERROR binary operation
13 //~^ ERROR mismatched types
14}
15
16fn main() {}
diff --git a/src/test/ui/issues/issue-66667-function-cmp-cycle.stderr b/src/test/ui/issues/issue-66667-function-cmp-cycle.stderr
new file mode 100644
index 0000000..887699e
--- /dev/null
+++ b/src/test/ui/issues/issue-66667-function-cmp-cycle.stderr
@@ -0,0 +1,55 @@
1error[E0369]: binary operation `==` cannot be applied to type `fn() {second}`
2 --> $DIR/issue-66667-function-cmp-cycle.rs:2:12
3 |
4LL | second == 1
5 | ------ ^^ - {integer}
6 | |
7 | fn() {second}
8
9error[E0308]: mismatched types
10 --> $DIR/issue-66667-function-cmp-cycle.rs:2:15
11 |
12LL | second == 1
13 | ^ expected fn item, found integer
14 |
15 = note: expected fn item `fn() {second}`
16 found type `{integer}`
17
18error[E0369]: binary operation `==` cannot be applied to type `fn() {first}`
19 --> $DIR/issue-66667-function-cmp-cycle.rs:7:11
20 |
21LL | first == 1
22 | ----- ^^ - {integer}
23 | |
24 | fn() {first}
25
26error[E0308]: mismatched types
27 --> $DIR/issue-66667-function-cmp-cycle.rs:7:14
28 |
29LL | first == 1
30 | ^ expected fn item, found integer
31 |
32 = note: expected fn item `fn() {first}`
33 found type `{integer}`
34
35error[E0369]: binary operation `==` cannot be applied to type `fn() {bar}`
36 --> $DIR/issue-66667-function-cmp-cycle.rs:12:9
37 |
38LL | bar == 1
39 | --- ^^ - {integer}
40 | |
41 | fn() {bar}
42
43error[E0308]: mismatched types
44 --> $DIR/issue-66667-function-cmp-cycle.rs:12:12
45 |
46LL | bar == 1
47 | ^ expected fn item, found integer
48 |
49 = note: expected fn item `fn() {bar}`
50 found type `{integer}`
51
52error: aborting due to 6 previous errors
53
54Some errors have detailed explanations: E0308, E0369.
55For more information about an error, try `rustc --explain E0308`.
diff --git a/src/test/ui/issues/issue-66706.rs b/src/test/ui/issues/issue-66706.rs
new file mode 100644
index 0000000..5e64f63
--- /dev/null
+++ b/src/test/ui/issues/issue-66706.rs
@@ -0,0 +1,13 @@
1fn a() {
2 [0; [|_: _ &_| ()].len()]
3 //~^ ERROR expected `,`, found `&`
4 //~| ERROR type annotations needed
5 //~| ERROR mismatched types
6}
7
8fn b() {
9 [0; [|f @ &ref _| {} ; 0 ].len() ];
10 //~^ ERROR expected identifier, found reserved identifier `_`
11}
12
13fn main() {}
diff --git a/src/test/ui/issues/issue-66706.stderr b/src/test/ui/issues/issue-66706.stderr
new file mode 100644
index 0000000..6d290bc
--- /dev/null
+++ b/src/test/ui/issues/issue-66706.stderr
@@ -0,0 +1,32 @@
1error: expected `,`, found `&`
2 --> $DIR/issue-66706.rs:2:16
3 |
4LL | [0; [|_: _ &_| ()].len()]
5 | -^ expected `,`
6 | |
7 | help: missing `,`
8
9error: expected identifier, found reserved identifier `_`
10 --> $DIR/issue-66706.rs:9:20
11 |
12LL | [0; [|f @ &ref _| {} ; 0 ].len() ];
13 | ^ expected identifier, found reserved identifier
14
15error[E0282]: type annotations needed
16 --> $DIR/issue-66706.rs:2:11
17 |
18LL | [0; [|_: _ &_| ()].len()]
19 | ^ consider giving this closure parameter a type
20
21error[E0308]: mismatched types
22 --> $DIR/issue-66706.rs:2:5
23 |
24LL | fn a() {
25 | - help: try adding a return type: `-> [{integer}; _]`
26LL | [0; [|_: _ &_| ()].len()]
27 | ^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found array `[{integer}; _]`
28
29error: aborting due to 4 previous errors
30
31Some errors have detailed explanations: E0282, E0308.
32For more information about an error, try `rustc --explain E0282`.
diff --git a/src/test/ui/methods/method-path-in-pattern.stderr b/src/test/ui/methods/method-path-in-pattern.stderr
index 1d1bdb6..ed3c022 100644
--- a/src/test/ui/methods/method-path-in-pattern.stderr
+++ b/src/test/ui/methods/method-path-in-pattern.stderr
@@ -4,13 +4,13 @@ error[E0533]: expected unit struct, unit variant or constant, found associated f
4LL | Foo::bar => {} 4LL | Foo::bar => {}
5 | ^^^^^^^^ 5 | ^^^^^^^^
6 6
7error[E0533]: expected unit struct, unit variant or constant, found associated function `Foo::bar` 7error[E0533]: expected unit struct, unit variant or constant, found associated function `<Foo>::bar`
8 --> $DIR/method-path-in-pattern.rs:19:9 8 --> $DIR/method-path-in-pattern.rs:19:9
9 | 9 |
10LL | <Foo>::bar => {} 10LL | <Foo>::bar => {}
11 | ^^^^^^^^^^ 11 | ^^^^^^^^^^
12 12
13error[E0533]: expected unit struct, unit variant or constant, found associated function `Foo::trait_bar` 13error[E0533]: expected unit struct, unit variant or constant, found associated function `<Foo>::trait_bar`
14 --> $DIR/method-path-in-pattern.rs:23:9 14 --> $DIR/method-path-in-pattern.rs:23:9
15 | 15 |
16LL | <Foo>::trait_bar => {} 16LL | <Foo>::trait_bar => {}
@@ -22,7 +22,7 @@ error[E0533]: expected unit struct, unit variant or constant, found associated f
22LL | if let Foo::bar = 0u32 {} 22LL | if let Foo::bar = 0u32 {}
23 | ^^^^^^^^ 23 | ^^^^^^^^
24 24
25error[E0533]: expected unit struct, unit variant or constant, found associated function `Foo::bar` 25error[E0533]: expected unit struct, unit variant or constant, found associated function `<Foo>::bar`
26 --> $DIR/method-path-in-pattern.rs:28:12 26 --> $DIR/method-path-in-pattern.rs:28:12
27 | 27 |
28LL | if let <Foo>::bar = 0u32 {} 28LL | if let <Foo>::bar = 0u32 {}
diff --git a/src/test/ui/privacy/associated-item-privacy-trait.rs b/src/test/ui/privacy/associated-item-privacy-trait.rs
index 03347d5..b1482bc 100644
--- a/src/test/ui/privacy/associated-item-privacy-trait.rs
+++ b/src/test/ui/privacy/associated-item-privacy-trait.rs
@@ -21,9 +21,9 @@ mod priv_trait {
21 Pub.method(); 21 Pub.method();
22 //~^ ERROR type `for<'r> fn(&'r Self) {<Self as priv_trait::PrivTr>::method}` is private 22 //~^ ERROR type `for<'r> fn(&'r Self) {<Self as priv_trait::PrivTr>::method}` is private
23 <Pub as PrivTr>::CONST; 23 <Pub as PrivTr>::CONST;
24 //~^ ERROR associated constant `PrivTr::CONST` is private 24 //~^ ERROR associated constant `<Pub as PrivTr>::CONST` is private
25 let _: <Pub as PrivTr>::AssocTy; 25 let _: <Pub as PrivTr>::AssocTy;
26 //~^ ERROR associated type `PrivTr::AssocTy` is private 26 //~^ ERROR associated type `<Pub as PrivTr>::AssocTy` is private
27 pub type InSignatureTy = <Pub as PrivTr>::AssocTy; 27 pub type InSignatureTy = <Pub as PrivTr>::AssocTy;
28 //~^ ERROR trait `priv_trait::PrivTr` is private 28 //~^ ERROR trait `priv_trait::PrivTr` is private
29 pub trait InSignatureTr: PrivTr {} 29 pub trait InSignatureTr: PrivTr {}
@@ -115,7 +115,7 @@ mod priv_parent_substs {
115 <Priv as PubTr<_>>::CONST; 115 <Priv as PubTr<_>>::CONST;
116 //~^ ERROR type `priv_parent_substs::Priv` is private 116 //~^ ERROR type `priv_parent_substs::Priv` is private
117 117
118 let _: <Pub as PubTr>::AssocTy; // FIXME no longer an error?! 118 let _: <Pub as PubTr>::AssocTy; // FIXME no longer an error?!
119 let _: <Pub as PubTr<_>>::AssocTy; 119 let _: <Pub as PubTr<_>>::AssocTy;
120 //~^ ERROR type `priv_parent_substs::Priv` is private 120 //~^ ERROR type `priv_parent_substs::Priv` is private
121 let _: <Priv as PubTr<_>>::AssocTy; 121 let _: <Priv as PubTr<_>>::AssocTy;
diff --git a/src/test/ui/privacy/associated-item-privacy-trait.stderr b/src/test/ui/privacy/associated-item-privacy-trait.stderr
index c30cc94..b9f3e35 100644
--- a/src/test/ui/privacy/associated-item-privacy-trait.stderr
+++ b/src/test/ui/privacy/associated-item-privacy-trait.stderr
@@ -31,7 +31,7 @@ LL | priv_trait::mac!();
31 | 31 |
32 = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) 32 = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
33 33
34error: associated constant `PrivTr::CONST` is private 34error: associated constant `<Pub as PrivTr>::CONST` is private
35 --> $DIR/associated-item-privacy-trait.rs:23:9 35 --> $DIR/associated-item-privacy-trait.rs:23:9
36 | 36 |
37LL | <Pub as PrivTr>::CONST; 37LL | <Pub as PrivTr>::CONST;
@@ -42,7 +42,7 @@ LL | priv_trait::mac!();
42 | 42 |
43 = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) 43 = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
44 44
45error: associated type `PrivTr::AssocTy` is private 45error: associated type `<Pub as PrivTr>::AssocTy` is private
46 --> $DIR/associated-item-privacy-trait.rs:25:16 46 --> $DIR/associated-item-privacy-trait.rs:25:16
47 | 47 |
48LL | let _: <Pub as PrivTr>::AssocTy; 48LL | let _: <Pub as PrivTr>::AssocTy;
diff --git a/src/test/ui/qualified/qualified-path-params.stderr b/src/test/ui/qualified/qualified-path-params.stderr
index 7ff43f4..4214e25 100644
--- a/src/test/ui/qualified/qualified-path-params.stderr
+++ b/src/test/ui/qualified/qualified-path-params.stderr
@@ -1,4 +1,4 @@
1error[E0533]: expected unit struct, unit variant or constant, found associated function `<<S as Tr>::A>::f<u8>` 1error[E0533]: expected unit struct, unit variant or constant, found associated function `<S as Tr>::A::f::<u8>`
2 --> $DIR/qualified-path-params.rs:20:9 2 --> $DIR/qualified-path-params.rs:20:9
3 | 3 |
4LL | <S as Tr>::A::f::<u8> => {} 4LL | <S as Tr>::A::f::<u8> => {}