@@ -1086,12 +1086,31 @@ func (p *Package) load(stk *ImportStack, bp *build.Package, err error) {
1086
1086
// code; see issue #16050).
1087
1087
}
1088
1088
1089
- // The gc toolchain only permits C source files with cgo.
1090
- if len (p .CFiles ) > 0 && ! p .UsesCgo () && ! p .UsesSwig () && cfg .BuildContext .Compiler == "gc" {
1089
+ setError := func (msg string ) {
1091
1090
p .Error = & PackageError {
1092
1091
ImportStack : stk .Copy (),
1093
- Err : fmt . Sprintf ( "C source files not allowed when not using cgo or SWIG: %s" , strings . Join ( p . CFiles , " " )) ,
1092
+ Err : msg ,
1094
1093
}
1094
+ }
1095
+
1096
+ // The gc toolchain only permits C source files with cgo or SWIG.
1097
+ if len (p .CFiles ) > 0 && ! p .UsesCgo () && ! p .UsesSwig () && cfg .BuildContext .Compiler == "gc" {
1098
+ setError (fmt .Sprintf ("C source files not allowed when not using cgo or SWIG: %s" , strings .Join (p .CFiles , " " )))
1099
+ return
1100
+ }
1101
+
1102
+ // C++, Objective-C, and Fortran source files are permitted only with cgo or SWIG,
1103
+ // regardless of toolchain.
1104
+ if len (p .CXXFiles ) > 0 && ! p .UsesCgo () && ! p .UsesSwig () {
1105
+ setError (fmt .Sprintf ("C++ source files not allowed when not using cgo or SWIG: %s" , strings .Join (p .CXXFiles , " " )))
1106
+ return
1107
+ }
1108
+ if len (p .MFiles ) > 0 && ! p .UsesCgo () && ! p .UsesSwig () {
1109
+ setError (fmt .Sprintf ("Objective-C source files not allowed when not using cgo or SWIG: %s" , strings .Join (p .MFiles , " " )))
1110
+ return
1111
+ }
1112
+ if len (p .FFiles ) > 0 && ! p .UsesCgo () && ! p .UsesSwig () {
1113
+ setError (fmt .Sprintf ("Fortran source files not allowed when not using cgo or SWIG: %s" , strings .Join (p .FFiles , " " )))
1095
1114
return
1096
1115
}
1097
1116
@@ -1100,10 +1119,7 @@ func (p *Package) load(stk *ImportStack, bp *build.Package, err error) {
1100
1119
if other := foldPath [fold ]; other == "" {
1101
1120
foldPath [fold ] = p .ImportPath
1102
1121
} else if other != p .ImportPath {
1103
- p .Error = & PackageError {
1104
- ImportStack : stk .Copy (),
1105
- Err : fmt .Sprintf ("case-insensitive import collision: %q and %q" , p .ImportPath , other ),
1106
- }
1122
+ setError (fmt .Sprintf ("case-insensitive import collision: %q and %q" , p .ImportPath , other ))
1107
1123
return
1108
1124
}
1109
1125
0 commit comments