internal/refactor/inline/analyzer: export only cross-package facts

Export only those constant facts that can be used on other packages:
the constant to be inlined must begin with an uppercase letter and live
at package scope.

For golang/go#32816.

Change-Id: I5da443cab65cadd90f557fa6d1892d85b83e2f2c
Reviewed-on: https://go-review.googlesource.com/c/tools/+/645955
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
diff --git a/internal/refactor/inline/analyzer/analyzer.go b/internal/refactor/inline/analyzer/analyzer.go
index a0d3bc9..97bb43e 100644
--- a/internal/refactor/inline/analyzer/analyzer.go
+++ b/internal/refactor/inline/analyzer/analyzer.go
@@ -17,6 +17,7 @@
 	"golang.org/x/tools/go/types/typeutil"
 	"golang.org/x/tools/internal/diff"
 	"golang.org/x/tools/internal/refactor/inline"
+	"golang.org/x/tools/internal/typesinternal"
 )
 
 const Doc = `inline calls to functions with "//go:fix inline" doc comment`
@@ -106,8 +107,13 @@
 								RHSName:    rhs.Name(),
 								RHSPkgPath: rhs.Pkg().Path(),
 							}
-							pass.ExportObjectFact(lhs, con)
 							inlinableConsts[lhs] = con
+							// Create a fact only if the LHS is exported and defined at top level.
+							// We create a fact even if the RHS is non-exported,
+							// so we can warn about uses in other packages.
+							if lhs.Exported() && typesinternal.IsPackageLevel(lhs) {
+								pass.ExportObjectFact(lhs, con)
+							}
 						}
 					}
 				}
diff --git a/internal/refactor/inline/analyzer/testdata/src/a/a.go b/internal/refactor/inline/analyzer/testdata/src/a/a.go
index 20e1c1e..2e3843e 100644
--- a/internal/refactor/inline/analyzer/testdata/src/a/a.go
+++ b/internal/refactor/inline/analyzer/testdata/src/a/a.go
@@ -20,26 +20,28 @@
 
 // Constants.
 
+const Uno = 1
+
 //go:fix inline
-const in1 = one // want in1: `goFixInline const "a".one`
+const In1 = Uno // want In1: `goFixInline const "a".Uno`
 
 const (
 	no1 = one
 
 	//go:fix inline
-	in2 = one // want in2: `goFixInline const "a".one`
+	In2 = one // want In2: `goFixInline const "a".one`
 )
 
 //go:fix inline
 const (
-	in3  = one // want in3: `goFixInline const "a".one`
-	in4  = one // want in4: `goFixInline const "a".one`
-	bad1 = 1   // want `invalid //go:fix inline directive: const value is not the name of another constant`
+	in3  = one
+	in4  = one
+	bad1 = 1 // want `invalid //go:fix inline directive: const value is not the name of another constant`
 )
 
 //go:fix inline
-const in5, // want in5: `goFixInline const "a".one`
-	in6, // want in6: `goFixInline const "a".one`
+const in5,
+	in6,
 	bad2 = one, one,
 	one + 1 // want `invalid //go:fix inline directive: const value is not the name of another constant`
 
@@ -49,12 +51,12 @@
 const (
 	a = iota
 	b
-	in7 = one // want in7: `goFixInline const "a".one`
+	in7 = one
 )
 
 func _() {
-	x := in1 // want `Constant in1 should be inlined`
-	x = in2  // want `Constant in2 should be inlined`
+	x := In1 // want `Constant In1 should be inlined`
+	x = In2  // want `Constant In2 should be inlined`
 	x = in3  // want `Constant in3 should be inlined`
 	x = in4  // want `Constant in4 should be inlined`
 	x = in5  // want `Constant in5 should be inlined`
diff --git a/internal/refactor/inline/analyzer/testdata/src/a/a.go.golden b/internal/refactor/inline/analyzer/testdata/src/a/a.go.golden
index 0a6d240..ea38dd0 100644
--- a/internal/refactor/inline/analyzer/testdata/src/a/a.go.golden
+++ b/internal/refactor/inline/analyzer/testdata/src/a/a.go.golden
@@ -20,26 +20,28 @@
 
 // Constants.
 
+const Uno = 1
+
 //go:fix inline
-const in1 = one // want in1: `goFixInline const "a".one`
+const In1 = Uno // want In1: `goFixInline const "a".Uno`
 
 const (
 	no1 = one
 
 	//go:fix inline
-	in2 = one // want in2: `goFixInline const "a".one`
+	In2 = one // want In2: `goFixInline const "a".one`
 )
 
 //go:fix inline
 const (
-	in3  = one // want in3: `goFixInline const "a".one`
-	in4  = one // want in4: `goFixInline const "a".one`
-	bad1 = 1   // want `invalid //go:fix inline directive: const value is not the name of another constant`
+	in3  = one
+	in4  = one
+	bad1 = 1 // want `invalid //go:fix inline directive: const value is not the name of another constant`
 )
 
 //go:fix inline
-const in5, // want in5: `goFixInline const "a".one`
-	in6, // want in6: `goFixInline const "a".one`
+const in5,
+	in6,
 	bad2 = one, one,
 	one + 1 // want `invalid //go:fix inline directive: const value is not the name of another constant`
 
@@ -49,12 +51,12 @@
 const (
 	a = iota
 	b
-	in7 = one // want in7: `goFixInline const "a".one`
+	in7 = one
 )
 
 func _() {
-	x := one // want `Constant in1 should be inlined`
-	x = one  // want `Constant in2 should be inlined`
+	x := Uno // want `Constant In1 should be inlined`
+	x = one  // want `Constant In2 should be inlined`
 	x = one  // want `Constant in3 should be inlined`
 	x = one  // want `Constant in4 should be inlined`
 	x = one  // want `Constant in5 should be inlined`